Author Topic: Basic VB(a) help; returning a value.  (Read 4151 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Basic VB(a) help; returning a value.
« 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
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Basic VB(a) help; returning a value.
« Reply #1 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.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

TR

  • Guest
Basic VB(a) help; returning a value.
« Reply #2 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

Bob Wahr

  • Guest
Basic VB(a) help; returning a value.
« Reply #3 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.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Basic VB(a) help; returning a value.
« Reply #4 on: September 02, 2005, 12:49:57 PM »
Bwaaa, great odyssey ref.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Bob Wahr

  • Guest
Basic VB(a) help; returning a value.
« Reply #5 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.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Basic VB(a) help; returning a value.
« Reply #6 on: September 02, 2005, 11:21:09 PM »
Bob, your having way to much fun at work
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)