473,669 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ChequeWritingUt ility
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.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'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.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
Me.outputLabel = New System.Windows. Forms.Label
Me.convertButto n = New System.Windows. Forms.Button
Me.Label1 = New System.Windows. Forms.Label
Me.numberTextBo x = New System.Windows. Forms.TextBox
Me.SuspendLayou t()
'
'outputLabel
'
Me.outputLabel. Location = New System.Drawing. Point(32, 46)
Me.outputLabel. Name = "outputLabe l"
Me.outputLabel. Size = New System.Drawing. Size(296, 58)
Me.outputLabel. TabIndex = 7
'
'convertButton
'
Me.convertButto n.DialogResult = System.Windows. Forms.DialogRes ult.OK
Me.convertButto n.Location = New System.Drawing. Point(248, 120)
Me.convertButto n.Name = "convertBut ton"
Me.convertButto n.Size = New System.Drawing. Size(80, 24)
Me.convertButto n.TabIndex = 6
Me.convertButto n.Text = "Convert"
'
'Label1
'
Me.Label1.Locat ion = New System.Drawing. Point(24, 14)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing. Size(96, 16)
Me.Label1.TabIn dex = 5
Me.Label1.Text = "Enter Number"
'
'numberTextBox
'
Me.numberTextBo x.Location = New System.Drawing. Point(128, 14)
Me.numberTextBo x.Name = "numberText Box"
Me.numberTextBo x.Size = New System.Drawing. Size(200, 20)
Me.numberTextBo x.TabIndex = 4
Me.numberTextBo x.Text = ""
'
'ChequeWritingU tility
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(336, 157)
Me.Controls.Add (Me.outputLabel )
Me.Controls.Add (Me.convertButt on)
Me.Controls.Add (Me.Label1)
Me.Controls.Add (Me.numberTextB ox)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "ChequeWritingU tility"
Me.Text = "ChequeWritingU tility"
Me.ResumeLayout (False)
End Sub
#End Region
'The approach I have chosen provides for extensibility for adding higher
denominations.
Private Sub OnConvertButton Clicked(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles convertButton.C lick
Dim number As String = numberTextBox.T ext.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.T ext)
If (decimalPos > 0) Then
cents = GetTens(Microso ft.VisualBasic. Left(Microsoft. VisualBasic.Mid (number,
decimalPos + 1) & "00", 2))
number = (Microsoft.Visu alBasic.Left(nu mber, decimalPos - 1))
cents = " and " & cents & " cents"
End If
'determines the dollar amounts.
If (number.Length >= 10) Then
outputLabel.Tex t = "Enter less than 10 digits"
Return
End If
Do While number <> ""
words = GetHundreds(Mic rosoft.VisualBa sic.Right(numbe r, 3))
If words <> "" Then dollars = words & position(cntr) & dollars
If (number.Length > 3) Then
number = Microsoft.Visua lBasic.Left(num ber, Len(number) - 3)
Else
number = ""
End If
cntr = cntr + 1
Loop
dollars = dollars & " dollars"
outputLabel.Tex t = dollars + cents
End Sub

Private Function GetHundreds(ByV al number As String) As String
Dim result As String
'calculates the hundreds.
number = Microsoft.Visua lBasic.Right("0 00" & number, 3)
If Mid(number, 1, 1) <> "0" Then
result = GetOnes(Mid(num ber, 1, 1)) & " Hundred And "
End If
If Mid(number, 2, 1) <> "0" Then
'calculates the tens.
result = result & GetTens(Mid(num ber, 2))
Else
'calculates the ones.
result = result & GetOnes(Mid(num ber, 3))
End If
Return result
End Function
Private Function GetTens(ByVal number As String) As String
Dim result As String
If (Microsoft.Visu alBasic.Left(nu mber, 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.Visua lBasic.Left(num ber, 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(Microso ft.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 1755
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.Visua lBasic

Imports VB = Microsoft.Visua lBasic
Public Class ChequeWritingUt ility
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******** ******@TK2MSFTN GP12.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 ChequeWritingUt ility
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.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'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.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent() Me.outputLabel = New System.Windows. Forms.Label
Me.convertButto n = New System.Windows. Forms.Button
Me.Label1 = New System.Windows. Forms.Label
Me.numberTextBo x = New System.Windows. Forms.TextBox
Me.SuspendLayou t()
'
'outputLabel
'
Me.outputLabel. Location = New System.Drawing. Point(32, 46)
Me.outputLabel. Name = "outputLabe l"
Me.outputLabel. Size = New System.Drawing. Size(296, 58)
Me.outputLabel. TabIndex = 7
'
'convertButton
'
Me.convertButto n.DialogResult = System.Windows. Forms.DialogRes ult.OK
Me.convertButto n.Location = New System.Drawing. Point(248, 120)
Me.convertButto n.Name = "convertBut ton"
Me.convertButto n.Size = New System.Drawing. Size(80, 24)
Me.convertButto n.TabIndex = 6
Me.convertButto n.Text = "Convert"
'
'Label1
'
Me.Label1.Locat ion = New System.Drawing. Point(24, 14)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing. Size(96, 16)
Me.Label1.TabIn dex = 5
Me.Label1.Text = "Enter Number"
'
'numberTextBox
'
Me.numberTextBo x.Location = New System.Drawing. Point(128, 14)
Me.numberTextBo x.Name = "numberText Box"
Me.numberTextBo x.Size = New System.Drawing. Size(200, 20)
Me.numberTextBo x.TabIndex = 4
Me.numberTextBo x.Text = ""
'
'ChequeWritingU tility
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(336, 157)
Me.Controls.Add (Me.outputLabel )
Me.Controls.Add (Me.convertButt on)
Me.Controls.Add (Me.Label1)
Me.Controls.Add (Me.numberTextB ox)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "ChequeWritingU tility"
Me.Text = "ChequeWritingU tility"
Me.ResumeLayout (False)
End Sub
#End Region
'The approach I have chosen provides for extensibility for adding higher
denominations.
Private Sub OnConvertButton Clicked(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles convertButton.C lick
Dim number As String = numberTextBox.T ext.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.T ext)
If (decimalPos > 0) Then
cents = GetTens(Microso ft.VisualBasic. Left(Microsoft. VisualBasic.Mid (number, decimalPos + 1) & "00", 2))
number = (Microsoft.Visu alBasic.Left(nu mber, decimalPos - 1))
cents = " and " & cents & " cents"
End If
'determines the dollar amounts.
If (number.Length >= 10) Then
outputLabel.Tex t = "Enter less than 10 digits"
Return
End If
Do While number <> ""
words = GetHundreds(Mic rosoft.VisualBa sic.Right(numbe r, 3))
If words <> "" Then dollars = words & position(cntr) & dollars
If (number.Length > 3) Then
number = Microsoft.Visua lBasic.Left(num ber, Len(number) - 3)
Else
number = ""
End If
cntr = cntr + 1
Loop
dollars = dollars & " dollars"
outputLabel.Tex t = dollars + cents
End Sub

Private Function GetHundreds(ByV al number As String) As String
Dim result As String
'calculates the hundreds.
number = Microsoft.Visua lBasic.Right("0 00" & number, 3)
If Mid(number, 1, 1) <> "0" Then
result = GetOnes(Mid(num ber, 1, 1)) & " Hundred And "
End If
If Mid(number, 2, 1) <> "0" Then
'calculates the tens.
result = result & GetTens(Mid(num ber, 2))
Else
'calculates the ones.
result = result & GetOnes(Mid(num ber, 3))
End If
Return result
End Function
Private Function GetTens(ByVal number As String) As String
Dim result As String
If (Microsoft.Visu alBasic.Left(nu mber, 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.Visua lBasic.Left(num ber, 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(Microso ft.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
1789
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 "optimize table", but (seemingly) the server version 4.0.16 requires INSERT as well. Is the INSERT privelege necessary for performing optimize in mysql
9
2392
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'; $sentence = "$insert or not $insert. That is the question."; or
5
1780
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 the query? "SELECT AL3.LAB, AL3.DEPARTMENT, AL6.NAME, Count (AL4.ITEM), AL5.SALES_TERRIOTARY, AL1.DATE_SERVICE FROM YR2005.REQUESTX AL1, YR2005.RESULTP AL2, YR2005.PANEL AL3, YR2005.RESULTV AL4, YR2005.DOCTOR AL5, YR2005.DEPT AL6 WHERE...
0
2005
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 option in there to optimize the code, has anyone had any expeiance / problems with this option and were there any significant gains in performance by turning on this setting?
6
1528
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 0. If it is run with optimize code set to false, len is evaluated as 256 (what i want). If I add an extra line of code after declaring len, like:
3
2820
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 there any way I can optimize the speed. Any sample code would be great. Thanks, Reddy
15
2517
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( j=0; j<i-1; j++ ) {
14
2523
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 - I don't want to have more #defines than code)? IOCCC winners can really help me with this :P
4
1176
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? (sample 2, http://msdn2.microsoft.com/en-us/library/ms364057(vs.80).aspx) #include <stdio.h> class A {
2
2081
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 some sample source code to optimize system performance. Thanks pavani
0
8465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8383
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8895
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2797
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.