Solved

VB refering to Variables

Posted on 2003-11-16
9
350 Views
Last Modified: 2010-05-01
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 =)
0
Comment
Question by:craigpike
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 3

Accepted Solution

by:
NBrownoh earned 50 total points
ID: 9759637
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
 
LVL 6

Expert Comment

by:GPrentice00
ID: 9759653
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
 

Expert Comment

by:qwertykeyboard
ID: 9759674
Ok....
Try this 1:

Dim Rnd As Integer

Rnd = Int(Rnd * 8) + 1
Command1.Tag = Rnd
0
 

Expert Comment

by:qwertykeyboard
ID: 9759682
U would put that in the form load of your form, or in a timer, or in a click event!
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:craigpike
ID: 9759701
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
 
LVL 3

Expert Comment

by:NBrownoh
ID: 9759712
in the form or modules declaration section:

Public Rand1 as long

there ya go
0
 
LVL 6

Expert Comment

by:GPrentice00
ID: 9759733
>>>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
 
LVL 6

Expert Comment

by:GPrentice00
ID: 9759740
think arrays, control arrays, loops and indices (or indexes if you prefer...)
0
 
LVL 3

Expert Comment

by:NBrownoh
ID: 9760975
'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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now