Cannot access an variable from an array instance of a button

E

esteban40oz

I have 9 buttons on my form. Button1, Button2, Button3, etc

I want to create an array to access the value of each of them.

Dim buttonArray() As Button = {Button1, Button2, Button3, Button4,
button5, Button6, Button7, Button8, Button9}

Now, I awnt to create 3 buttons using the array.

Dim b1 As Button = New Button
b1 = buttonArray(0)

Dim b2 As Button = New Button
b2 = buttonArray(1)

Dim b3 As Button = New Button
b3 = buttonArray(2)


When I try to access b1.Text I get an error 'error: cannot obtain
value' from the vs debugger.

Any ideas on what I might need to do to gain access to this value.
 
J

Jack Jackson

I have 9 buttons on my form. Button1, Button2, Button3, etc

I want to create an array to access the value of each of them.

Dim buttonArray() As Button = {Button1, Button2, Button3, Button4,
button5, Button6, Button7, Button8, Button9}

Now, I awnt to create 3 buttons using the array.

Dim b1 As Button = New Button
b1 = buttonArray(0)

Dim b2 As Button = New Button
b2 = buttonArray(1)

Dim b3 As Button = New Button
b3 = buttonArray(2)


When I try to access b1.Text I get an error 'error: cannot obtain
value' from the vs debugger.

Any ideas on what I might need to do to gain access to this value.

First, this code doesn't do what you expect:
Dim b1 As Button = New Button
b1 = buttonArray(0)

This creates a variable named b1, creates a new Button and places a
reference to the new button in b1. The next line replaces the
reference to the new button with a reference to Button1, effectively
throwing away the new button. Change those two lines to:
Dim b1 As Button = buttonArray(0)

Where is the "Dim b1" statement? If it is in a method, the variable
will cease to exist (go out of scope) when the method ends. Maybe you
are trying to see the value of it when it no longer exists.

Maybe you want the definition of b1 to be a form variable, in which
case you need to put it between the Class statement and the first
method so that b1 will have scope to all methods in the class. If so
you should change Dim to Private.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top