• 0

Need Help With Functions


Question

How do I call a function in Visual Basic?

I have this code

Select Case Index

Case 0

Result# = Value1# * Value2#

Case 1

Result# = Value1# / Value2#

Case 2

Result# = Value1# + Value2#

Case 3

Result# = Value1# - Value2#

End Select

And I want to put that into one function [Not so sure, but maybe a sub?] so that I can call it in multiple areas of the program.

So hard to find on Google too "/ Anyone know any nice VB sites where I can study better?

Thanks

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

The function would be as follows

Function ReturnResult(byval Index as Integer, byval lValue1 as Long, byval lValue2 as Long) as Long

Select Case Index

Case 0

Result# = Value1# * Value2#

Case 1

Result# = Value1# / Value2#

Case 2

Result# = Value1# + Value2#

Case 3

Result# = Value1# - Value2#

End Select

End Function

and you would call it like:

Dim lResult as Long

lResult = ReturnResult(1, 4, 5)

MsgBox lResult

Link to comment
Share on other sites

  • 0

Sorry, should have explained more.

Heres the whole code for an operation:

Private Sub cmd_Divide_Click()

If Click = False Then

Value1# = Val(txt_Screen.Text)

ElseIf Click = True Then

Value2# = Val(txt_Screen.Text)

Select Case Index

Case 0

Result# = Value1# * Value2#

Case 1

Result# = Value1# / Value2#

Case 2

Result# = Value1# + Value2#

Case 3

Result# = Value1# - Value2#

End Select

txt_Screen.Text = Result#

txt_Screen.SetFocus

Value1# = Result#

End If

Index = 1

OpFlag = True

Click = True

End Sub

I use that in several functions such as add, minus, multiply, and divide. I want to reduce that Case Code into a smaller function so that in the end I get Result#.

BTW, what does ReturnResult(1, 4, 5) do? That caught me off guard.

Thanks

Link to comment
Share on other sites

  • 0

SaLiVa,

You asked for a function that included all of your code so you could call it from anywhere. The function I provided does that. You pass in the INDEX value, as well as the two VALUES (1 and 2) and it returns the result value.

I know you're new at this, and I knew being new can be frustrating as all can be (understatement I'm sure) however please take the time to review what is given, step through the code and watch what it does. It will be of much help to you.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.