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

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:

``````' 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

' 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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.