473,395 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

optimize code

hi, I have written a cheque writing utility in vb.net and would like to
invite comments on the code. can this be done in a better / faster / shorter
way?
thnx
Public Class ChequeWritingUtility
Inherits System.Windows.Forms.Form

' converts numbers to words
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents outputLabel As System.Windows.Forms.Label
Friend WithEvents convertButton As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents numberTextBox As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.outputLabel = New System.Windows.Forms.Label
Me.convertButton = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.numberTextBox = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'outputLabel
'
Me.outputLabel.Location = New System.Drawing.Point(32, 46)
Me.outputLabel.Name = "outputLabel"
Me.outputLabel.Size = New System.Drawing.Size(296, 58)
Me.outputLabel.TabIndex = 7
'
'convertButton
'
Me.convertButton.DialogResult = System.Windows.Forms.DialogResult.OK
Me.convertButton.Location = New System.Drawing.Point(248, 120)
Me.convertButton.Name = "convertButton"
Me.convertButton.Size = New System.Drawing.Size(80, 24)
Me.convertButton.TabIndex = 6
Me.convertButton.Text = "Convert"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 14)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(96, 16)
Me.Label1.TabIndex = 5
Me.Label1.Text = "Enter Number"
'
'numberTextBox
'
Me.numberTextBox.Location = New System.Drawing.Point(128, 14)
Me.numberTextBox.Name = "numberTextBox"
Me.numberTextBox.Size = New System.Drawing.Size(200, 20)
Me.numberTextBox.TabIndex = 4
Me.numberTextBox.Text = ""
'
'ChequeWritingUtility
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(336, 157)
Me.Controls.Add(Me.outputLabel)
Me.Controls.Add(Me.convertButton)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.numberTextBox)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "ChequeWritingUtility"
Me.Text = "ChequeWritingUtility"
Me.ResumeLayout(False)
End Sub
#End Region
'The approach I have chosen provides for extensibility for adding higher
denominations.
Private Sub OnConvertButtonClicked(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles convertButton.Click
Dim number As String = numberTextBox.Text.Trim()
Dim dollars As String
Dim words As String
Dim position(2) As String 'for adding higher denominations increase the
array size.
Dim cents As String
Dim cntr As Int32
Dim decimalPos As Int32
If (numberTextBox.Text.Trim() = "") Then
Return
End If
position(1) = " Thousand "
position(2) = " Million "
'can add higher denominations here. this allows for easy extensibility.
'currently the program can accomodate upto million dollars.
'determines if the amount has cent values.
decimalPos = InStr(number, ".", CompareMethod.Text)
If (decimalPos > 0) Then
cents = GetTens(Microsoft.VisualBasic.Left(Microsoft.Visua lBasic.Mid(number,
decimalPos + 1) & "00", 2))
number = (Microsoft.VisualBasic.Left(number, decimalPos - 1))
cents = " and " & cents & " cents"
End If
'determines the dollar amounts.
If (number.Length >= 10) Then
outputLabel.Text = "Enter less than 10 digits"
Return
End If
Do While number <> ""
words = GetHundreds(Microsoft.VisualBasic.Right(number, 3))
If words <> "" Then dollars = words & position(cntr) & dollars
If (number.Length > 3) Then
number = Microsoft.VisualBasic.Left(number, Len(number) - 3)
Else
number = ""
End If
cntr = cntr + 1
Loop
dollars = dollars & " dollars"
outputLabel.Text = dollars + cents
End Sub

Private Function GetHundreds(ByVal number As String) As String
Dim result As String
'calculates the hundreds.
number = Microsoft.VisualBasic.Right("000" & number, 3)
If Mid(number, 1, 1) <> "0" Then
result = GetOnes(Mid(number, 1, 1)) & " Hundred And "
End If
If Mid(number, 2, 1) <> "0" Then
'calculates the tens.
result = result & GetTens(Mid(number, 2))
Else
'calculates the ones.
result = result & GetOnes(Mid(number, 3))
End If
Return result
End Function
Private Function GetTens(ByVal number As String) As String
Dim result As String
If (Microsoft.VisualBasic.Left(number, 1) = 1) Then
Select Case (number)
Case 10 : result = "Ten"
Case 11 : result = "Eleven"
Case 12 : result = "Twelve"
Case 13 : result = "Thirteen"
Case 14 : result = "Fourteen"
Case 15 : result = "Fifteen"
Case 16 : result = "Sixteen"
Case 17 : result = "Seventeen"
Case 18 : result = "Eighteen"
Case 19 : result = "Nineteen"
Case Else
End Select
Else
Select Case Microsoft.VisualBasic.Left(number, 1)
Case 2 : result = "Twenty-"
Case 3 : result = "Thirty-"
Case 4 : result = "Forty-"
Case 5 : result = "Fifty-"
Case 6 : result = "Sixty-"
Case 7 : result = "Seventy-"
Case 8 : result = "Eighty-"
Case 9 : result = "Ninety-"
End Select
result = result & GetOnes(Microsoft.VisualBasic.Right(number, 1))
End If
Return result
End Function
Private Function GetOnes(ByVal number As String) As String
Select Case (number)
Case 1 : GetOnes = "One"
Case 2 : GetOnes = "Two"
Case 3 : GetOnes = "Three"
Case 4 : GetOnes = "Four"
Case 5 : GetOnes = "Five"
Case 6 : GetOnes = "Six"
Case 7 : GetOnes = "Seven"
Case 8 : GetOnes = "Eight"
Case 9 : GetOnes = "Nine"
End Select
Return GetOnes
End Function
End Class
Nov 20 '05 #1
2 1741
Jusan,
First I would add "Option Strict On" and "Option Explicit On" to the top of
your source file, and fix all the implicit conversions.

With your functions, you do not need to set the function name & return the
function name. I would do one or the other.

' This is bad
Function GetOnes() As String
GetOnes = "one"
Return GetOnes
End Function

' This is good
Function GetOnes() As String
GetOnes = "one"
End Function

' This is also good, I normally use this one.
Function GetOnes() As String
Return "one"
End Function

I would consider using a StringBuilder (possible passing it as parameter)
instead of using String contention. Ideally I would probably create a
separate class that converts the value, using a StringBuilder as a member
field.

Just for consistency I would use either + or & for concatenation, not both.

To clean up the code I would use an Import alias for Microsoft.VisualBasic

Imports VB = Microsoft.VisualBasic
Public Class ChequeWritingUtility
Then where I needed to use Left & Right I would use VB.Left & VB.Right.

Instead of using a Select Case in GetOnes, GetTens, and GetHundreds I would
consider using an array, either Static to the routine or Shared in the
class.

Hope this helps
Jay
"Jusan" <Ju***@hotmail.com> wrote in message
news:uW**************@TK2MSFTNGP12.phx.gbl... hi, I have written a cheque writing utility in vb.net and would like to
invite comments on the code. can this be done in a better / faster / shorter way?
thnx
Public Class ChequeWritingUtility
Inherits System.Windows.Forms.Form

' converts numbers to words
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents outputLabel As System.Windows.Forms.Label
Friend WithEvents convertButton As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents numberTextBox As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.outputLabel = New System.Windows.Forms.Label
Me.convertButton = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.numberTextBox = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'outputLabel
'
Me.outputLabel.Location = New System.Drawing.Point(32, 46)
Me.outputLabel.Name = "outputLabel"
Me.outputLabel.Size = New System.Drawing.Size(296, 58)
Me.outputLabel.TabIndex = 7
'
'convertButton
'
Me.convertButton.DialogResult = System.Windows.Forms.DialogResult.OK
Me.convertButton.Location = New System.Drawing.Point(248, 120)
Me.convertButton.Name = "convertButton"
Me.convertButton.Size = New System.Drawing.Size(80, 24)
Me.convertButton.TabIndex = 6
Me.convertButton.Text = "Convert"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 14)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(96, 16)
Me.Label1.TabIndex = 5
Me.Label1.Text = "Enter Number"
'
'numberTextBox
'
Me.numberTextBox.Location = New System.Drawing.Point(128, 14)
Me.numberTextBox.Name = "numberTextBox"
Me.numberTextBox.Size = New System.Drawing.Size(200, 20)
Me.numberTextBox.TabIndex = 4
Me.numberTextBox.Text = ""
'
'ChequeWritingUtility
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(336, 157)
Me.Controls.Add(Me.outputLabel)
Me.Controls.Add(Me.convertButton)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.numberTextBox)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "ChequeWritingUtility"
Me.Text = "ChequeWritingUtility"
Me.ResumeLayout(False)
End Sub
#End Region
'The approach I have chosen provides for extensibility for adding higher
denominations.
Private Sub OnConvertButtonClicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles convertButton.Click
Dim number As String = numberTextBox.Text.Trim()
Dim dollars As String
Dim words As String
Dim position(2) As String 'for adding higher denominations increase the
array size.
Dim cents As String
Dim cntr As Int32
Dim decimalPos As Int32
If (numberTextBox.Text.Trim() = "") Then
Return
End If
position(1) = " Thousand "
position(2) = " Million "
'can add higher denominations here. this allows for easy extensibility.
'currently the program can accomodate upto million dollars.
'determines if the amount has cent values.
decimalPos = InStr(number, ".", CompareMethod.Text)
If (decimalPos > 0) Then
cents = GetTens(Microsoft.VisualBasic.Left(Microsoft.Visua lBasic.Mid(number, decimalPos + 1) & "00", 2))
number = (Microsoft.VisualBasic.Left(number, decimalPos - 1))
cents = " and " & cents & " cents"
End If
'determines the dollar amounts.
If (number.Length >= 10) Then
outputLabel.Text = "Enter less than 10 digits"
Return
End If
Do While number <> ""
words = GetHundreds(Microsoft.VisualBasic.Right(number, 3))
If words <> "" Then dollars = words & position(cntr) & dollars
If (number.Length > 3) Then
number = Microsoft.VisualBasic.Left(number, Len(number) - 3)
Else
number = ""
End If
cntr = cntr + 1
Loop
dollars = dollars & " dollars"
outputLabel.Text = dollars + cents
End Sub

Private Function GetHundreds(ByVal number As String) As String
Dim result As String
'calculates the hundreds.
number = Microsoft.VisualBasic.Right("000" & number, 3)
If Mid(number, 1, 1) <> "0" Then
result = GetOnes(Mid(number, 1, 1)) & " Hundred And "
End If
If Mid(number, 2, 1) <> "0" Then
'calculates the tens.
result = result & GetTens(Mid(number, 2))
Else
'calculates the ones.
result = result & GetOnes(Mid(number, 3))
End If
Return result
End Function
Private Function GetTens(ByVal number As String) As String
Dim result As String
If (Microsoft.VisualBasic.Left(number, 1) = 1) Then
Select Case (number)
Case 10 : result = "Ten"
Case 11 : result = "Eleven"
Case 12 : result = "Twelve"
Case 13 : result = "Thirteen"
Case 14 : result = "Fourteen"
Case 15 : result = "Fifteen"
Case 16 : result = "Sixteen"
Case 17 : result = "Seventeen"
Case 18 : result = "Eighteen"
Case 19 : result = "Nineteen"
Case Else
End Select
Else
Select Case Microsoft.VisualBasic.Left(number, 1)
Case 2 : result = "Twenty-"
Case 3 : result = "Thirty-"
Case 4 : result = "Forty-"
Case 5 : result = "Fifty-"
Case 6 : result = "Sixty-"
Case 7 : result = "Seventy-"
Case 8 : result = "Eighty-"
Case 9 : result = "Ninety-"
End Select
result = result & GetOnes(Microsoft.VisualBasic.Right(number, 1))
End If
Return result
End Function
Private Function GetOnes(ByVal number As String) As String
Select Case (number)
Case 1 : GetOnes = "One"
Case 2 : GetOnes = "Two"
Case 3 : GetOnes = "Three"
Case 4 : GetOnes = "Four"
Case 5 : GetOnes = "Five"
Case 6 : GetOnes = "Six"
Case 7 : GetOnes = "Seven"
Case 8 : GetOnes = "Eight"
Case 9 : GetOnes = "Nine"
End Select
Return GetOnes
End Function
End Class

Nov 20 '05 #2
Hi Jusan,

In addition to Jay I make as well some extra comments, however did you
know that the routine you are making exist on MSDN see for that this

\\\
<http://support.microsoft.com/?scid=h...om:80/support/
kb/articles/Q95/6/40.ASP>
///

My comments on your code (as said in addition to Jay).

When you want to exit a method set the exit as high as possible, the style
of first setting the declarations of the values is from the past.

In your code you use a Return in a Sub, I would use the Exit Sub because it
confuses. A Return implements for me a function with a return value wich is
explicit or implicit given.

For the rest I see no big structural errors (I am never using the
VisualBasic namespace methods for string handling so maybe I miss something,
however those additions above was what direct got my eye).

I hope this helps?

Cor

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andreas Falck | last post by:
Hi, I ran the code below on two different versions, 4.0.14 and 4.0.16 respectively, both running RH 7.3 on intel. In version mysql server version 4.0.14 the SELECT privelege suffices for...
9
by: Rune | last post by:
Is it best to use double quotes and let PHP expand variables inside strings, or is it faster to do the string manipulation yourself manually? Which is quicker? 1) $insert = 'To Be';...
5
by: xeqister | last post by:
Greetings all, We have a complicated statement in DB2 which takes long hour to complete and we have created most of the indexes. Does anybody knows how to tune the following statement to optimize...
0
by: Daniel | last post by:
Hi there, I recently came across an interesting option when right clicking on a project (Right click on the project -> properties -> Configuration Properties ->Build -> Optimize) There is an...
6
by: Silly | last post by:
byte Name = new byte; uint len = (uint)Name.Length; uint err = MyFunction(devID, out Name, out len); When this code is run in release build with optimize code set to true, len is evaluated to...
3
by: Reddy | last post by:
The sql query for my datagrid returns 100, 000 records. But the datagrid should display 20 records per page. I am using datagrid paging, but it is taking too much time for the page to load. Is...
15
by: kenneth | last post by:
I was trying to use multiple thread to optimize my following code, but met some problems, anyone can help me? k are initialized. int computePot() { int i, j; for( i=0; i<500; i++ ) { for(...
14
by: andreyvul | last post by:
I have this loop (all variables are pointers): for (foo = bar; foo baz; foo--) *(foo+1) = *foo; How do I optimize the pointer swap so that it uses -- and ++ or unary +- instead of +1 (if possible...
4
by: George2 | last post by:
Hello everyone, Why Visual Studio compiler can not optimize in this case? I think this case is almost the same as sample 1, why compiler can optimize sample 1 but can not optimze sample 2? ...
2
by: pavanip | last post by:
Hi, I have an application like Optimize System Performance by using Memory speed, cpu speed and Disk speed. How to optimize memory speed,disk optimization and CPU optimization. Please provide me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.