Get values of an internal variable of a function (1 Viewer)

aqif

Registered User.
Local time
Today, 08:50
Joined
Jul 9, 2001
Messages
158
I have a simple problem in which I want to get value of variables that run internally to a function. for example look at a simple function which takes MyValue as an input and multiplies it with a random number to return a value

Code:
Public Function RandomValue(MyValue As Integer) As Long

Dim intRandom
intRandom = Int(Rnd() * 100)
RandomValue = intRandom * MyValue

End Function

Is there anyway I can get and use the value of intRandom as well? In this case I am looking for something like RandomValue.intRandom


In the above example I can easily calculate value of intRandom by dividing the result with myValue but remember this is just an example to illustrate my problem. My actual function is much more complex. I hope I am making sense here.
 
Last edited:

MStef

Registered User.
Local time
Today, 08:50
Joined
Oct 28, 2004
Messages
2,251
Try this.
RandomValue = intRandom
 

CarlM1975

Registered User.
Local time
Today, 08:50
Joined
Oct 29, 2008
Messages
24
how about:

Public Function RandomValue(MyValue As Integer) As Long

Dim intRandom

Dim intrandomvalue as double
intRandom = Int(Rnd() * 100)

intrandom=intrandomvalue
RandomValue = intRandom * MyValue

End Function


You should now be able to use me.intrandomvalue to bring through the value of intrandom
 

chergh

blah
Local time
Today, 08:50
Joined
Jun 15, 2004
Messages
1,414
Why not just divide the value returned by the value you pass to the function?
 

DCrake

Remembered
Local time
Today, 08:50
Joined
Jun 8, 2005
Messages
8,626
You need 2 functions

Code:
Function RandomInt() As double
  RandomInt = Int(Rnd() * 100)
End Function

Code:
Function RandomValue(rInt As Double, MyValue As Integer) As Long
    RandomValue =  rInt * MyValue
End Funtion


Then you can call

X = RandomInt()
Y = 3 ' MyValue
Z = RandomValue(X,Y)

However you could simply do

Z = RandomInt() * Y
 

aqif

Registered User.
Local time
Today, 08:50
Joined
Jul 9, 2001
Messages
158
Why not just divide the value returned by the value you pass to the function?

I have already explained that this is an example and I can easily calculate the Random value based on the output. My real question is that is there anyway to output value of an internal variable running in a function. The only way I can think of right now is to create a temp global variable ans use it to store value of variables that I want to. Only thing is that it is a bit tedious.

I wish there could be a better way
 

petehilljnr

Registered User.
Local time
Today, 00:50
Joined
Feb 13, 2007
Messages
192
Not sure if this is more or less efficient than using a global variable but:

Code:
Public Type MyRandoms
    intRandom As Integer
    RandomValue As Long
End Type
 
Sub test()
Dim MyInput As MyRandoms
 
MyInput = RandomValue(3) ' any number
 
Debug.Print MyInput.intRandom
Debug.Print MyInput.RandomValue
End Sub
 
Public Function RandomValue(MyValue As Integer) As MyRandoms
Dim MyTest As MyRandoms
 
MyTest.intRandom = Int(Rnd() * 100)
MyTest.RandomValue = MyTest.intRandom * MyValue
 
RandomValue = MyTest
End Function
 

petehilljnr

Registered User.
Local time
Today, 00:50
Joined
Feb 13, 2007
Messages
192
In fact no - I see what you mean now (after posting) about setting up for every variable you use ...

You might find something you can use in there though..

Pete
 

Users who are viewing this thread

Top Bottom