Solved

VB refering to Variables

Posted on 2003-11-16
9
352 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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
 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

770 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