Solved

convert string to object reference?

Posted on 2003-12-06
9
1,364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 

Expert Comment

by:ROTRTechTeam
ID: 9889584
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
ID: 9889588
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
ID: 9889771
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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:cp30
ID: 9889853
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9890035
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
ID: 9890143
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
ID: 9890281
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
ID: 9890593
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
ID: 9891685
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses
Course of the Month6 days, 12 hours left to enroll

623 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