944,208 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 6829
  • VB.NET RSS
Nov 7th, 2006
0

SubRoutine/Function Code Split

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
geo039 is offline Offline
8 posts
since Nov 2006
Nov 7th, 2006
0

Re: SubRoutine/Function Code Split

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:

VB.NET Syntax (Toggle Plain Text)
  1.  
  2. ' This is a simple, byval function. We'll call it in the mySub method.
  3. private function AddOne(BYVAL AnInteger as INTEGER) as INTEGER
  4. dim intPlusOne as INTEGER
  5. intPlusOne = AnInteger + 1
  6. return intPlusOne
  7. end function
  8.  
  9. ' This is a BYREF function.
  10. private SUB AddOneByRef(BYREF AnInteger as INTEGER)
  11. AnInteger = AnInteger + 1
  12. end sub
  13.  
  14. ' Now we call the methods above.
  15. private sub mySub()
  16.  
  17. ' intX starts out as a 5
  18. dim intX as INTEGER = 5
  19. ' intX = 5
  20.  
  21. intX is passed ByVal to AddOne. intX does not change.
  22. dim intY as INTEGER = AddOne(intX)
  23. ' intX = 5, intY=6
  24.  
  25. AddOneByRef(intX)
  26. ' intX=5 before the call, intX=6 after the call. No RETURN is needed.
  27. ' We updated the variable 'intX' within the subroutine.
  28.  
  29. 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
Reputation Points: 10
Solved Threads: 1
Light Poster
NedFrankly is offline Offline
26 posts
since Nov 2006
Nov 7th, 2006
0

Re: SubRoutine/Function Code Split

Hi any good vb book would explain functions in depth.

Try writing a very trivial function if you are confused.

Such as a function which takes a number and then returns that number multiplied by two.

Break it down, that's the key.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Replace a subitem in listview with another
Next Thread in VB.NET Forum Timeline: Excel from VB.NET





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC