CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2013

    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


    Toto, I have a feeling we're not in GW-BASIC anymore.

  2. #2
    Join Date
    Sep 2013

    Re: Trying to use string for object

    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!


  3. #3
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008

    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.

  4. #4
    Join Date
    Sep 2013

    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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.