VB refering to Variables

Hello,

I am currently writing my first program in VB and i am stuck!!

I have got this line of code in a public sub...

Rand1 = Int(Rnd * 8) + 1

and i would like to call or refer to the variable in a different public sub.

this is the line of code in the other public sub...

Command3(1).Tag = Rand1

but it doesn't keep the value!!??

I'm sure there is an easy way of doing this? is its possible or is there something else i could do?

Please help =)
craigpikeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NBrownohCommented:
is Rand1 declared as a public variable?  You could use a function instead of a routine:

Public Function Rand1() As Long
     Rand1 = int(Rnd*8)+1
End Function

if you call that like this:

Command3(1).Tag = Rand1

then it will set the .Tag to the long value inserted into Rand1
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GPrentice00Commented:
Although the sub is public that contains the
Rand1 = xxxx  

code, is Rand1 itself public or private?
If you have

public sub SomeName
  dim Rand1 as integer
  rand1=int(rnd * 8) +1
  'other stuff
end sub

then any references to Rand1 are moot.

Also, to help avoid a lot of problems if you don't have it already, put
OPTION EXPLICIT  at the top of your forms, modules, etc (or check that in your settings)

It forces you to declare the scope and type of each variable, otherwise, VB makes a bunch of
variants all over the place and you'll have a hard time tracking whats going on.  It would show you if you are trying to reference a variable in one spot that really, it can't, so it won't bother.
0
qwertykeyboardCommented:
Ok....
Try this 1:

Dim Rnd As Integer

Rnd = Int(Rnd * 8) + 1
Command1.Tag = Rnd
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

qwertykeyboardCommented:
U would put that in the form load of your form, or in a timer, or in a click event!
0
craigpikeAuthor Commented:
Thanks for your response

How do i declare rand1 as a public variable? Sounds like a good idea!  I rekon that the public function would do the trick but i am using rand1 to rand16 so do i have to declare a public function for each one??
0
NBrownohCommented:
in the form or modules declaration section:

Public Rand1 as long

there ya go
0
GPrentice00Commented:
>>>trick but i am using rand1 to rand16 so do i have to declare a public function for each one??

Sounds like you're going to a lot of work - you want to set 16 variables to = a random number each, then transfer that value into the commandbutton array TAG for a second static storage?

Go with the public FUNCTION  NBrownoh had at first, then

commandbuttonname(indexidentifier).tag = rand1()

Or make an array, but if you have 16 to set randomly, make one random function, and initialize the values with that function.
0
GPrentice00Commented:
think arrays, control arrays, loops and indices (or indexes if you prefer...)
0
NBrownohCommented:
'In a module
Public Rand(0 To 15) ' Declares this as public, make sure its in the declaration part of a module

Public Function MakeRandom() As Long
    Randomize 'Initializes the randomization of the Rnd function
    MakeRandom = Int(Rnd * 8) + 1 'Fills the array with a random number
End Function

'In your form

Private Sub Command1_Click()
    For i = 0 To 15 'replace 15 with the number of things you want to fill
        Rand(i) = MakeRandom 'Fills the array
    Next
End Sub

'--
Now if you want to assign the tag of an object just do something like:

Command1.Tag = Rand(4)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.