• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 363
  • Last Modified:

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 =)
0
craigpike
Asked:
craigpike
  • 3
  • 3
  • 2
  • +1
1 Solution
 
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
 
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now