| | |
SubRoutine/Function Code Split
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 8
Reputation:
Solved Threads: 0
I am trying to seperate the code below into one sub routine and one function. Specifically a boolean function that does the number check. I am still learning how to use sub procedures and functions and am not sure where to do the seperation. Any advice would be helpful. I've tried and when I break it up, the functionality goes crazy.
If IsNumeric(txtLower.Text) And IsNumeric(txtUpper.Text) Then
If CInt(txtLower.Text) < CInt(txtUpper.Text) Then
Dim intLower As Integer ' lower bound input
Dim intUpper As Integer ' upper bound input
Dim intNumber As Integer ' used to check for prime
Dim intPrimeCounter As Integer ' counter
intLower = txtLower.Text
intUpper = txtUpper.Text
Do While intLower <= intUpper
intNumber = 2
intPrimeCounter = 0
Do While intNumber < intLower
If (intLower Mod intNumber) = 0 Then
intPrimeCounter += 1
End If
intNumber += 1
Loop
If intPrimeCounter = 0 Then
txtResults.AppendText(intLower & Environment.NewLine)
End If
intLower += 1
Loop
Else
MsgBox("Upper Bound Must be Greater than Lower Bound ")
End If
Else
MessageBox.Show(Text:="Enter a Positive Numeric Value", _
caption:="Input error - Prime Numbers")
End If
End Sub
If IsNumeric(txtLower.Text) And IsNumeric(txtUpper.Text) Then
If CInt(txtLower.Text) < CInt(txtUpper.Text) Then
Dim intLower As Integer ' lower bound input
Dim intUpper As Integer ' upper bound input
Dim intNumber As Integer ' used to check for prime
Dim intPrimeCounter As Integer ' counter
intLower = txtLower.Text
intUpper = txtUpper.Text
Do While intLower <= intUpper
intNumber = 2
intPrimeCounter = 0
Do While intNumber < intLower
If (intLower Mod intNumber) = 0 Then
intPrimeCounter += 1
End If
intNumber += 1
Loop
If intPrimeCounter = 0 Then
txtResults.AppendText(intLower & Environment.NewLine)
End If
intLower += 1
Loop
Else
MsgBox("Upper Bound Must be Greater than Lower Bound ")
End If
Else
MessageBox.Show(Text:="Enter a Positive Numeric Value", _
caption:="Input error - Prime Numbers")
End If
End Sub
•
•
Join Date: Nov 2006
Posts: 26
Reputation:
Solved Threads: 1
It's hard to tell what code you want to move to a Function. A couple of things to consider:
If you pass objects to a method BYVAL, you are passing a COPY of the object (i.e. your integer). If you pass an object to a method BYREF you are NOT passing a value, you are passing a pointer to the original variable (and can generally update it from your subroutine). Here's an example:
To start, figure out what you want to make repeatable (that's the whole point of creating a method like this). Once you establish that, look at the code to be split out and determine every piece of data that it needs - every variable, every fixed value. Look at the scope of your variables - does the entire Class have access to them, or were they all declared local to the subroutine snippet you provided?
To successfully convert procedural code to a function or sub, you have to know what you're going to pass and how. Creating class-scoped variables (meaning that you declare them outside of any subroutines, such as at the top of your class immediately after the class and inherits declarations) means that you can use them from within any method without concern for passing them. Don't arbitrarily scope high, though. You, as the developer, have to know where to scope your variables to get maximum efficiency and minimal memory requirements. Remember that if you scope at the Class level, that variable will not be subject to Garbage Collection until that class is unloaded.
The code you gave as an example is pretty short, and I would not glance twice at it as far as trying to split out functions and subroutines (it's not that complex). However, it appears that you're trying to get your feet wet with understanding Functions and Subs, so you've picked a good starting point.
See what you can do with what I've provided, and if you want more specific help, identify the lines that you see as a candidate for a function or sub.
Happy Coding!
Ned
If you pass objects to a method BYVAL, you are passing a COPY of the object (i.e. your integer). If you pass an object to a method BYREF you are NOT passing a value, you are passing a pointer to the original variable (and can generally update it from your subroutine). Here's an example:
VB.NET Syntax (Toggle Plain Text)
' This is a simple, byval function. We'll call it in the mySub method. private function AddOne(BYVAL AnInteger as INTEGER) as INTEGER dim intPlusOne as INTEGER intPlusOne = AnInteger + 1 return intPlusOne end function ' This is a BYREF function. private SUB AddOneByRef(BYREF AnInteger as INTEGER) AnInteger = AnInteger + 1 end sub ' Now we call the methods above. private sub mySub() ' intX starts out as a 5 dim intX as INTEGER = 5 ' intX = 5 intX is passed ByVal to AddOne. intX does not change. dim intY as INTEGER = AddOne(intX) ' intX = 5, intY=6 AddOneByRef(intX) ' intX=5 before the call, intX=6 after the call. No RETURN is needed. ' We updated the variable 'intX' within the subroutine. end sub
To start, figure out what you want to make repeatable (that's the whole point of creating a method like this). Once you establish that, look at the code to be split out and determine every piece of data that it needs - every variable, every fixed value. Look at the scope of your variables - does the entire Class have access to them, or were they all declared local to the subroutine snippet you provided?
To successfully convert procedural code to a function or sub, you have to know what you're going to pass and how. Creating class-scoped variables (meaning that you declare them outside of any subroutines, such as at the top of your class immediately after the class and inherits declarations) means that you can use them from within any method without concern for passing them. Don't arbitrarily scope high, though. You, as the developer, have to know where to scope your variables to get maximum efficiency and minimal memory requirements. Remember that if you scope at the Class level, that variable will not be subject to Garbage Collection until that class is unloaded.
The code you gave as an example is pretty short, and I would not glance twice at it as far as trying to split out functions and subroutines (it's not that complex). However, it appears that you're trying to get your feet wet with understanding Functions and Subs, so you've picked a good starting point.
See what you can do with what I've provided, and if you want more specific help, identify the lines that you see as a candidate for a function or sub.
Happy Coding!
Ned
![]() |
Similar Threads
- I'm looking for words counter code (VB.NET)
- creating our own C++ strcat() function, code reqd (C++)
- I want the code of a certain function (C)
Other Threads in the VB.NET Forum
- Previous Thread: Replace a subitem in listview with another
- Next Thread: Excel from VB.NET
| Thread Tools | Search this Thread |
.net .net2008 2008 access account advanced application array basic beginner browser button buttons center click code combo cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic excel exists fade filter forms ftp generatetags html images input insert intel internet listview map mobile module monitor msaccess net number objects open panel passingparameters pdf picturebox picturebox2 port position print printing problem regex right-to-left save search searchvb.net select serial settings shutdown socket sqldatbase sqlserver survey table temperature textbox timer timespan transparency txttoxmlconverter update user usercontol vb vb.net vb.netformclosing()eventpictureboxmessagebox vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web winforms wpf wrapingcode xml year






