Help!! 2 Days to finish app and having problems with objectsVB 2008

I have wasted many days on errors related to objects.
I really need someone that has a lot of experience to respond to this question.  I only have two days left to finish this app... (They are using it on Saturday)

I am not sure what is going on, but I have been having some real problems with my app and objects.  A declaration will work in one area but not another.
For example.. in another procedure in the same form I have declared...
 Dim oID As Object
        oID = "txtID" & Me.TabRacing.SelectedTab.Tag.ToString
        Dim txt As TextBox = CType(oID, TextBox)

and it converts just fine.
Now in another procedure I have ...
Dim oID As Object = "txtID" & t
        Dim txt As TextBox = CType(oID, TextBox)

Where t is a string = "0"

In my app, I have multiple tabs and need to keep track of all these tabs so I append numbers to the end of the control according to the index of the tab they are working on.

I also had no problem setting the text and tag of a tabpage, but can't retrieve the values from them.  I know the values are there, but I get an error saying Object reference not set...
These already exist on the form.
Dim oID As Object = "txtID" & t
        Dim txt As TextBox = CType(oID, TextBox)
dim s as string = Me.TabRacing.TabPages(t).Text

Open in new window

Who is Participating?
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Your first snippet can't possibly be working correctly!

        Dim oID As Object
        oID = "txtID" & Me.TabRacing.SelectedTab.Tag.ToString
        Dim txt As TextBox = CType(oID, TextBox)

What you are doing is placing a STRING into "oID" and then attempt to convert it to a TextBox.  A String cannot be converted to a TextBox...

I think what you probably want, though, is to find the ACTUAL TextBox on your Form that has the NAME contained in oID right?

That could be accomplished with code like this:

        Dim txt As TextBox = Nothing
        Dim oID As String = "txtID" & Me.TabRacing.SelectedTab.Tag.ToString
        Dim matches() As Control = Me.Controls.Find(oID, True)
        If matches.Length > 0 Then
            If TypeOf (matches(0)) Is TextBox Then
                txt = DirectCast(matches(0), TextBox)
            End If
        End If
        If Not IsNothing(txt) Then
            MessageBox.Show("Text: " & txt.Text, "Contents of " & oID, MessageBoxButtons.OK, MessageBoxIcon.Information)
            MessageBox.Show("TextBox not found: " & oID, "Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

Open in new window

SheritlwAuthor Commented:
Thank you, Thank you, Thank you.
I have been working on this program for months and what a learning curve between VB 6 and 2008.
I really appreciate your help.  Your solution worked for all kinds of things I was trying to do.
Thank you again!
SheritlwAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.