Functions

  • Thread starter Thread starter cmdolcet69
  • Start date Start date
C

cmdolcet69

If a function get passed some variables by val and by ref. how can i
call that function again in the same class.

For example: Private Function Number (byval x as integer, byval y as
integer) as integer
return some value
end Function


If I call the Number () inside another sub what do i put inside the
(.......) ?
 
Hello 69,

You would call it as a normal function.

For Example:

Private Class MyClass
Private Function Number (ByVal x As Integer, ByVal y As Integer) as _
Integer

....

Return SomeValue

End Function

Private Sub TestMe()
Dim x As Integer

x = Number(5,1)

Dim a As Integer = 5
Dim b As Integer = 1
Dim y As Integer

y = Number(a,b)

Const c As Integer = 5
Const d As Integer = 1
Dim z As Integer

z = Number(c,d)

End Sub

End Class


As You can see, the function call is always the same. No matter if you
use numbers, constants (numbers are also constants) or variables.

Best regards,

Martin
 
Hello 69,

You would call it as a normal function.

For Example:

Private Class MyClass
Private Function Number (ByVal x As Integer, ByVal y As Integer) as _
Integer

...

Return SomeValue

End Function

Private Sub TestMe()
Dim x As Integer

x = Number(5,1)

Dim a As Integer = 5
Dim b As Integer = 1
Dim y As Integer

y = Number(a,b)

Const c As Integer = 5
Const d As Integer = 1
Dim z As Integer

z = Number(c,d)

End Sub

End Class

As You can see, the function call is always the same. No matter if you
use numbers, constants (numbers are also constants) or variables.

Best regards,

Martin





- Show quoted text -

Ok i undstand that however in my example the cd are declared anywhere
does that mean you need to delcare those somewhere in the class?
 
cmdolcet69 said:
Ok i undstand that however in my example the cd are declared anywhere
does that mean you need to delcare those somewhere in the class?

You should really grab a good book and get into variable scope and OOP
basics.


Armin
 
Hello 69,

You would call it as a normal function.

For Example:

Private Class MyClass
Private Function Number (ByVal x As Integer, ByVal y As Integer) as _
Integer

...

Return SomeValue

End Function

Private Sub TestMe()
Dim x As Integer

x = Number(5,1)

Dim a As Integer = 5
Dim b As Integer = 1
Dim y As Integer

y = Number(a,b)

Const c As Integer = 5
Const d As Integer = 1
Dim z As Integer

z = Number(c,d)

End Sub

End Class

As You can see, the function call is always the same. No matter if you
use numbers, constants (numbers are also constants) or variables.

Best regards,

Martin





- Show quoted text -

If i have Private Function Test (ByVal mux as muxanalogvalues) as
image

how do I declare the test () function inside another sub.

if i go test (.....I need to pass the value of the mux but how do i
know what to pass)
 
If i have Private Function Test (ByVal mux as muxanalogvalues) as
image

how do I declare the test () function inside another sub.

if i go test (.....I need to pass the value of the mux but how do i
know what to pass)- Hide quoted text -

- Show quoted text -

As Armin suggested - a good book on Scope and OOP would be helpful.

Variables/Objects have a certain lifetime. They exists as long as the
context in which they were created in exists.

Meaning - If I have a sub, and declare some value X, that value is
only in scope as long as the sub is executing. Once the sub finishes
execution, this variable X goes out of scope.

Hence if I have:

Private sub Test()

Dim X as Integer = 10

Messagebox.Show(X)

End Sub

Every time we enter this sub, the variable X is created and has it's
value set. Since it was created within the context of the sub Test, it
only exists as long as sub Test is executing. Each time we enter the
sub Test, the variable X has no memory of it's value from previous
calls. When Test is done - variable X goes poof.

In your case - you might want to declare a value at the class level,
which will retain it's value as long as the instance of the class is
in memory. If you are writing code in a form, if you have:

Public Class Form1

private mMyValue as Integer = 1

Private Sub Test1()

Messagebox.show(mMyValue)

End Sub

End Class

When Form1 becomes instantiated, it initializes the variable mMyValue
and sets the value = 1. Hence this variable is declared at the class
level (the form in this case). As long as the form remains loaded,
mMyValue retains it's value - until the form is closed and disposed.
Since this value is declared at the class level, it is accessible
anywhere from within the class.

You asked how do you declare the test() function inside another sub.
You only declare a function once, but can call it from any other sub
in the class. Now, if your "mux" value is declared at the class level,
you really don't need to pass it to a function. As an example, I'll
assume these functions are on a form such as:

Public Class Form1

private mux as new muxanalogvalues

Private function Test() as image

'do whatever code here
'we have access to the object ref
'mux since it's declared at the class level

End Function
'now, if we want to run the code in Test() from another sub, we
simply call test()

Private sub Test2()
Test()
End Sub
End Class
 
Back
Top