TheSwamp

Code Red => VB(A) => Topic started by: JohnK on September 02, 2005, 11:48:58 AM

Title: Basic VB(a) help; returning a value.
Post by: JohnK on September 02, 2005, 11:48:58 AM
I made a simple helper procedure to help demonstrate my question.  please see quotes.

Code: [Select]
Public Function MakeTen(X As Integer)
 ' make an int at least ten.
   If X >= 10 Then
   ' how do I return the value?
      End
   Else
      X = 1 + X
      MakeTen (X)
   End If
End Function

Sub MainProc()
 ' How do I ask for a value?
 ' (dialog box is fine but how do I convert the string to an int?)
 ' ie Userform1.TextBox1.text ...
   MakeTen (...)
 ' now that I have an int at least ten how do I display it in a msgbox.
End Sub
Title: Basic VB(a) help; returning a value.
Post by: MP on September 02, 2005, 11:56:13 AM
Code: [Select]
Public Function Times10 ( Argument as Integer ) As Integer

    Times10  = 10 * Argument

End Function

If the return type is a class then you have to use the set statement.
Title: Basic VB(a) help; returning a value.
Post by: TR on September 02, 2005, 12:00:31 PM
This is according to your comments. Or from what I understood at least
Code: [Select]
Public Function MakeTen(X As Integer) As Integer
 ' make an int at least ten.
   If X >= 10 Then
    MakeTen = X
   Else
    MakeTen = 10
   End If
End Function

Sub MainProc()
Dim strValue As String
Dim i As Integer
strValue = InputBox("Please enter an Integer", "theswamp.org", 1)
i = MakeTen(CInt(strValue))
MsgBox i
End Sub
Title: Basic VB(a) help; returning a value.
Post by: Bob Wahr on September 02, 2005, 12:46:18 PM
you will also need to handle it if they type something other than an integer.
Code: [Select]
Sub MainProc()
Dim strValue As String
Dim i As Integer
On Error GoTo YouBwokedIt
strValue = InputBox("Please enter an Integer", "theswamp.org", 1)
i = MakeTen(CInt(strValue))
MsgBox i

LeavingOnAJetTrain:
  Exit Sub

YouBwokedIt:
  Select Case Err.Number
    Case 13
      Err.Clear
      strValue = InputBox("Hint: An Integer is a number" & vbCrLf & "Please enter an Integer", "theswamp.org", 1)
      Resume
    Case Else
      MsgBox "Oh my god...It's full of stars."
      GoTo LeavingOnAJetTrain
    End Select
End Sub
Do you really want an integer?  That would be whole numbers larger than  -32,768 and smaller than 32,767 only.  If you need numbers outside that range or decimals, you might want to go with doubles instead.
Title: Basic VB(a) help; returning a value.
Post by: MP on September 02, 2005, 12:49:57 PM
Bwaaa, great odyssey ref.
Title: Basic VB(a) help; returning a value.
Post by: Bob Wahr on September 02, 2005, 01:15:29 PM
I feel it's improtant to always be as descriptive as possible with error control.. It keeps users from getting confused and makes debugging a lot easier.
Title: Basic VB(a) help; returning a value.
Post by: David Hall on September 02, 2005, 11:21:09 PM
Bob, your having way to much fun at work