Results 1 to 6 of 6

Thread: an array object

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    20

    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:
    1. Dim slotname(5) As Object
    or
    VB Code:
    1. Dim slotname(5) As Label

    then in one of my scripts i run this code
    VB Code:
    1. slotname(0) = lbl_slot1 'names of my labels in vb
    2. slotname(1) = lbl_slot2
    3. slotname(2) = lbl_slot3
    4. slotname(3) = lbl_slot4
    5. slotname(4) = lbl_slot5

    in another script i run this code
    VB Code:
    1. While colour_setup < 4
    2.         colourslot_rand(colour_setup) = Int(Rnd * 5)
    3.         colourslot(colour_setup) = colourslot_rand(colour_setup)
    4.         slotname(colour_setup).BackColor = colourslot(colour_setup)
    5.         colour_setup = colour_setup + 1
    6.     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

    VB Code:
    1. slotname(0) = lbl_slot1


    can someone please help me to gain a better understanding of how vb works in this sence?

  2. #2
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Re: an array object

    use the set keyword.
    VB Code:
    1. set slotname(0)=label1

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    20

    Re: an array object

    ty for that...

    may i just ask? why does it require another syntax?

    why does it need set?

  4. #4
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    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:
    1. dim rs as adodb.recordset
    2. 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.

  5. #5
    Addicted Member
    Join Date
    Feb 2006
    Location
    The Sea of Tranquility
    Posts
    252

    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:
    1. label1 = "Hello"
    2.  
    3. or
    4.  
    5. 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

  6. #6
    Addicted Member BlueRose's Avatar
    Join Date
    Jan 2002
    Location
    ISTANBUL
    Posts
    245

    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:
    1. While colour_setup < 4
    2.         colourslot_rand(colour_setup) = Int(Rnd * 5)
    3.         colourslot(colour_setup) = colourslot_rand(colour_setup)
    4.         [COLOR=Red]lbl_slot[/COLOR](colour_setup).BackColor = colourslot(colour_setup)
    5.         colour_setup = colour_setup + 1
    6.     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
  •  



Click Here to Expand Forum to Full Width