Solved

convert string to object reference?

Posted on 2003-12-06
9
1,311 Views
Last Modified: 2010-08-05
I’m working in VB .NET in a windows form

I have a string that I have concatenated from 2 other values, this string now contains a reference to a label, how do I now use string to change the properties of the object it references, i.e. I wanna change the text so I would type strMyStrings.text = “whatever”, but this generates an error as the string doesn’t have a text value obviously, seems that I need to change the string into an object reference or something, but how!?!?!?!

Please help!

Cheers
0
Comment
Question by:cp30
  • 4
  • 4
9 Comments
 

Expert Comment

by:ROTRTechTeam
Comment Utility
I'm not understanding what you are trying to do.  Are you trying to set the value of a label equal to this string that you've concatenated from 2 other values?  If so, you would do this:

lblMyLabel.Text = strMyStrings

If not, what exactly are you trying to do and I'm sure I can help you figure it out.

Mike
0
 

Author Comment

by:cp30
Comment Utility
just read my question and it might not be getting my point across, here's an example....


my Form contains two labels one named LABEL1 and one named LABEL2

I run some code and then refrence an int (which is one at this point) value by creating a string which is detailed below...

dim strLabelRef as String

strLabelRef = "LABEL" & intMyValue

strLabelRef then equals LABEL1, which is what I want, but I then need to perform the following operation

strLabelRef.text = "whatever"

this is where I'm stuck!

hope this makes thing clearer, thanks!
0
 

Expert Comment

by:ROTRTechTeam
Comment Utility
I'm sorry, but you are still not clear to me.  Could you post the code you are using?  

What I'm not clear aboutis what you are trying to accomplish with strLabelRef?

It seems that you are trying to refer to a particular label (either LABEL1 or LABEL2) depending upon what integer value comes back in the intMyValue variable.  This is where you lose me.  If you are trying to now set the caption of the particular label based upon the value that is now in strLabelRef, my other code will work.  If you are trying to do something else, I don't see it from your question.  If you could post the code you are using I'm sure I can help.

Mike
0
 

Author Comment

by:cp30
Comment Utility
OK, here it is.........

 Dim intRowcount As Integer

        intRowcount = DS.Tables(0).Rows.Count()

        strHomeTeam = DS.Tables(0).Rows(0).Item(0)
        strAwayTeam = DS.Tables(0).Rows(0).Item(1)
        lblHomeTeam0.Text = strHomeTeam
        lblAwayTeam0.Text = strAwayTeam

        intFixture = intFixture + 1

        Do While intFixture < intRowcount

           'This gets the HomeTeam from the DataSet for the current row

           strHomeTeam = DS.Tables(0).Rows(intFixture).Item(0)  

           'This gets the AwayTeam from the DataSet for the current row            

            strAwayTeam = DS.Tables(0).Rows(intFixture).Item(1)
       
            Dim strAwayLbl As String
            strAwayLbl = "lblAwayTeam" & intFixture  'Should now reference the Away Team label for the current row

            Dim strHomeLbl As String
            strHomeLbl = "lblHomeTeam" & intFixture  'Should now reference the Home Team label for the current row
           

****** This is where I now want to enter the home team in row 1 into lblHomeTeam1 and The Away Team in lblAwayTeam1, then it will loop and for the next row it will enter the teams into lblHomeTeam2 and lblAwayTeam2 .  I have generated the object references in the strHomeLbl & strAwayLbl strings, but I cannot use these to change the a value (the text value in this case) to what I want it to be.  I was hoping to type this

strHomeLbl.text = strHomeTeam
strAwayLbl.text = strAwayTeam

But that won't work. ******

            intFixture = intFixture + 1

        Loop



==== End of Code


Thanks
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
try something like this (AIR-CODE Warning I do not have ,NET installed on this PC)

for each ctrl as Control in Controls
   if typeof ctrl is Label AndAlso ctrl.Name = strHomeLbl then
      ' you have the one you want
   end if
Next
0
 

Expert Comment

by:ROTRTechTeam
Comment Utility
OK, now that makes more sense.  What you should really be doing here is using control arrays for the labels.  That would be the best way to do it.  That way you would have lblHomeTeam(0), lblHomeTeam(1), etc. and lblAwayTeam(0), lblAwayTeam(1), etc.  Arthur_Wood's method will work with the way you currently have things, but it will be slow because it needs to loop through every control on your form.  My way would be faster for the following reason:

Let's say you change those labels to a control array.  Now we change your code as follows:

Dim intRowcount As Integer

        intRowcount = DS.Tables(0).Rows.Count()

        strHomeTeam = DS.Tables(0).Rows(0).Item(0)
        strAwayTeam = DS.Tables(0).Rows(0).Item(1)
        lblHomeTeam(0).Text = strHomeTeam
        lblAwayTeam(0).Text = strAwayTeam

        intFixture = intFixture + 1

        Do While intFixture < intRowcount

           'This gets the HomeTeam from the DataSet for the current row

           strHomeTeam = DS.Tables(0).Rows(intFixture).Item(0)  

           'This gets the AwayTeam from the DataSet for the current row            

            strAwayTeam = DS.Tables(0).Rows(intFixture).Item(1)
       
           lblHomeTeam(intFixture) = strHomeTeam ' This sets the home team to the label in the control array it refers to
           lblAwayTeam(intFixture) = strAwayTeam ' This sets the away team to the label in the control array it refers to


            intFixture = intFixture + 1

        Loop



==== End of Code

Hope that helps!
0
 

Author Comment

by:cp30
Comment Utility
That sounds great ROTRTechTeam, but I thought that control arrays were dropped in VB .NET, am I wrong, if so how should I go about creating an array of labels?(have to excuse me slightly, am self-taught and very new to VB).

Thanks again.
0
 

Accepted Solution

by:
ROTRTechTeam earned 150 total points
Comment Utility
Whoops!  My mistake.  You are right, VB.NET does not support control arrays, but here is a way around the problem.

First let me show you some sample code I just wrote:

I set up a form with 4 labels.  I named these labels lblHomeTeam1, lblHomeTeam2, lblAwayTeam1, lblAwayTeam2.

Now, in my form load event I just added the following code:

        Dim i As Integer
         Dim Home(1) As Object
        Dim Away(1) As Object
        Dim lblHomeLabelControl As Label
        Dim lblAwayLabelControl As Label
        Home(0) = lblHomeTeam1
        Home(1) = lblHomeTeam2
        Away(0) = lblAwayTeam1
        Away(1) = lblAwayTeam2

        For i = 0 To 1
            lblHomeLabelControl = Home(i)
            lblHomeLabelControl.Text = "HomeTeam" & i
            lblAwayLabelControl = Away(i)
            lblAwayLabelControl.Text = "AwayTeam" & i
        Next

What happens is that I've created two object arrays and stored my items in there.  Now I dim two label controls.  I then assign the label control to one of the items in my object arrays and I set the text property.  How would that be implemented in your situation?  Let's take a look:

' First create the object arrays

Dim Home(1) as Object ' This only allows for two labels for home.  If you want this dynamic, let me know.
Dim Away(1) as Object  ' Same thing.  Only two labels. Let me know if you want this dynamic.

' Now create your two labels to be used for assignment

Dim lblHomeLabelControl as Label
Dim lblAwayLabelControl as Label

' Now put your labels in the object arrays

Home(0) = lblHomeTeam0
Home(1) = lblHomeTeam1
Away(0) = lblAwayTeam0
Away(1) = lblAwayTeam1

' Now on to the rest of your existing code

 Dim intRowcount As Integer

        intRowcount = DS.Tables(0).Rows.Count()

        strHomeTeam = DS.Tables(0).Rows(0).Item(0)
        strAwayTeam = DS.Tables(0).Rows(0).Item(1)
        lblHomeTeam0.Text = strHomeTeam
        lblAwayTeam0.Text = strAwayTeam

        intFixture = intFixture + 1

        Do While intFixture < intRowcount

           'This gets the HomeTeam from the DataSet for the current row

           strHomeTeam = DS.Tables(0).Rows(intFixture).Item(0)  

           'This gets the AwayTeam from the DataSet for the current row            

            strAwayTeam = DS.Tables(0).Rows(intFixture).Item(1)
       
            Dim strAwayLbl As String
            strAwayLbl = "lblAwayTeam" & intFixture  'Should now reference the Away Team label for the current row

            Dim strHomeLbl As String
            strHomeLbl = "lblHomeTeam" & intFixture  'Should now reference the Home Team label for the current row
           
          ' Now we use the technique described in my sample code to get at your label values
           lblHomeLabelControl = Home(intFixture - 1) ' Must decrease intFixture by 1 to meet the zero based array
           lblHomeLabelControl.Text = strHomeLbl
           lblAwayLabelControl = Away(intFixture - 1) ' Must decrease intFixture by 1 to meet the zero based array
           lblAwayLabelControl.Text = strAwayLbl

            intFixture = intFixture + 1

        Loop



==== End of Code

This is a workaround to the missing control arrays.  This should work just fine for you.  If you wanted it to be dynamic it would be pretty simple to accomplish that, especially since you know how many rows there are, so I assume there are the same number of labels.

I hope that helps.

Mike
0
 

Author Comment

by:cp30
Comment Utility
That's excellent Mike, you truly are a star!! It worked brilliantly.

Thanks for taking all that time with the code examples, they were near-on perfect!

Cheers

cp

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Vb.net Listview 9 76
Access 2016 VB code 9 85
VB 6.0 printer how to align 6 49
Paint/Redraw window while dragging 16 56
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now