-
September 26th, 2013, 08:59 AM
#1
Trying to use string for object
Hi again,
I still have the same problem, but I think I can now state it more clearly:
The key aspects of the program I am concerned about are
Dim Neuron(3, 3) As Integer
Dim NeurNameBase As String = "Neuron"
Dim NeurName As String
For row As Integer = 1 To 3 Step 1
For column As Integer = 1 To 3 Step 1
NeurName = NeurNameBase & row.ToString & "x" & column.ToString 'Assemble string which I want to use as Object – this works ok
'Then Various code included here, to decide whether Neuron should be made active (depends on inputs)
'If Neuron should be active, then carry out various actions, including changing colour of object (Neuron on form) to RED
NeurName.BackColor = Color.Red ‘THIS IS WHAT I CANNOT DO
Next column
Next row
My problem is using the string NeurName (Neuron1x1, Neuron1x2, Neuron1x3, Neuron2x1, …… Neuron3x3) in some way, to change the colour of the objects on the form, which have the same names.
From what has been said already (and some blundering around the net), it looks as though I need to call up one of the controls by matching it with the string.
I have tried
Me.Controls(“NeurName”) = Color.Red
But I get the error ‘NullReferenceException was unhandled’ with ‘Object reference not set to an instance of an object’
However, I do have the objects on the form, with the appropriate names – am I missing something?
Am I getting any closer?
A small example which works would be really useful.
Many thanks for any help/advice
Arthropod
PS
Toto, I have a feeling we're not in GW-BASIC anymore.
-
September 26th, 2013, 10:23 AM
#2
Re: Trying to use string for object
FWIW,
Think I have solved it using Me.Controls, as in:
Public Class Form1
Dim but As String = "Button1"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Controls(but).BackColor = Color.Red
End Sub
End Class
This works ok, so now just a question of seeing what is happening with my original code.
Many thanks to Craig for putting me onto the right track.
The textbook arrives tomorrow!
Arthropod
-
September 27th, 2013, 08:41 AM
#3
Re: Trying to use string for object
Me.Controls(“NeurName”) = Color.Red
When you use quotes you are telling it to use the literal text rather than the variable by that name
Always use [code][/code] tags when posting code.
-
September 27th, 2013, 04:38 PM
#4
Re: Trying to use string for object
Thanks DataMiser - as you can see, I am still at a very early level.
It is all starting to make sense though, and I am booting ahead with my project.
I personally learn best by using stuff, and learning to use things when needed. A lot of things do not seem to be well specified and it helps to get support like this.
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
|