|
-
Apr 8th, 2006, 04:46 AM
#1
Thread Starter
Junior Member
an array object
hi, for school we are to create a game in vb (master mind)
so anyway i want to make my code very condensed, now i have used a variety of different programming tools, but vb is new to me, i want to assign an array to my objects so i can make my code small
at the moment the object is a label, so i am not sure how to assign it
either
VB Code:
Dim slotname(5) As Object
or
then in one of my scripts i run this code
VB Code:
slotname(0) = lbl_slot1 'names of my labels in vb
slotname(1) = lbl_slot2
slotname(2) = lbl_slot3
slotname(3) = lbl_slot4
slotname(4) = lbl_slot5
in another script i run this code
VB Code:
While colour_setup < 4
colourslot_rand(colour_setup) = Int(Rnd * 5)
colourslot(colour_setup) = colourslot_rand(colour_setup)
slotname(colour_setup).BackColor = colourslot(colour_setup)
colour_setup = colour_setup + 1
Wend
Now in this code basically this will pick a random colour and assign it to the labels background colour
whenever i run this script i get the error
Run-time error '91'
Object variable or With block variable not set
and it then proceeds to go to this line of code
can someone please help me to gain a better understanding of how vb works in this sence?
-
Apr 8th, 2006, 05:08 AM
#2
Addicted Member
-
Apr 8th, 2006, 05:32 AM
#3
Thread Starter
Junior Member
Re: an array object
ty for that...
may i just ask? why does it require another syntax?
why does it need set?
-
Apr 8th, 2006, 05:41 AM
#4
Addicted Member
Re: an array object
I am new to VB myself. So the explanation might not be as accurate as you might want it to be.
For simple datatypes like integer,string etc, variables can be assigned values directly. Label is a type of object. Before using an object in the program, set has to be used. I have used ado a bit. I will try to give an example with that.
VB Code:
dim rs as adodb.recordset
set rs=new adodb.recordset
Before using the rs variable above has to be done. Otherwise, the same error message that you got will be displayed.
Hope this helps.
-
Apr 8th, 2006, 06:12 AM
#5
Addicted Member
Re: an array object
The different sytanx is mainly due to a quick fix by MS as VB went form 1 -6, all objects have default propeties. For example you can:
VB Code:
label1 = "Hello"
or
label1.caption = "Hello"
The default property for a label is the caption. Hence why you need to use the set method when creating new obejects and assigning them
Rich
-
Apr 8th, 2006, 09:51 AM
#6
Addicted Member
Re: an array object
instead of using individual label names, you can use object arrays.
create your lbl_slot with index
convert lbl_slot1 to lbl_slot(1), lbl_slot2 to lbl_slot(2) and so on
therefore you will not need slotname variable.
change your control code as follows:
VB Code:
While colour_setup < 4
colourslot_rand(colour_setup) = Int(Rnd * 5)
colourslot(colour_setup) = colourslot_rand(colour_setup)
[COLOR=Red]lbl_slot[/COLOR](colour_setup).BackColor = colourslot(colour_setup)
colour_setup = colour_setup + 1
Wend
You can do while you think that you can do
If you think my answer solve your question, please rate it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|