Results 1 to 13 of 13

Thread: String cannot be converted to label?

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    45

    String cannot be converted to label?

    I have an array of numbers of which I want displayed in a text box but when I call for the numbers in my array, I get a compiler error "Type "String" cannot be converted to label".

    How can I get my array value printed onto my text box? Where am I going wrong? Here is the code:
    Code:
    Public Class frmBaseExponent
        Dim LabelArray(8) As Label
        Private Sub frmBaseExponent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
           
    
        End Sub
    
        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
            Dim intExponent, intBase As Integer
    
            If Integer.TryParse(txtExponentInput.Text, intExponent) AndAlso Integer.TryParse(txtBaseInput.Text, intBase) Then
                SetControlArray()
                For i As Int16 = 0 To 8
    
                    LabelArray(i).Text = (CInt(intBase) ^ (CInt(intExponent) + i)).ToString
    
                Next
            Else
                MessageBox.Show("Please enter a valid numeric value", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtExponentInput.SelectAll()
                txtExponentInput.Focus()
            End If
    
    
    
        End Sub
    
        Sub SetControlArray()
    
            LabelArray(0) = txtAnswer1.Text
    
            LabelArray(1) = txtAnswer2.Text
    
            LabelArray(2) = txtAnswer3.Text
    
            LabelArray(3) = txtAnswer4.Text
    
            LabelArray(4) = txtAnswer5.Text
    
            LabelArray(5) = txtAnswer6.Text
    
            LabelArray(6) = txtAnswer7.Text
    
            LabelArray(7) = txtAnswer8.Text
    
            LabelArray(8) = txtAnswer9.Text
    
        End Sub
    End Class
    Last edited by hoodborn; Jan 30th, 2010 at 11:57 AM.

  2. #2

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    45

    Re: String cannot be converted to label?

    Quote Originally Posted by cicatrix View Post
    Have you considered doing this:

    LabelArray(0).Text = txtAnswer1.Text

    ?
    yeah, i tried that, that didn't solve the problem:\

    Thanks for the reply though.

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: String cannot be converted to label?

    What type if your LabelArray?

    If it's Dim LabelArray() As Label then you need to assign String to its members' Text property.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: String cannot be converted to label?

    Actually, I'm impressed you aren't getting NULL REference errors... you've defined the array, but it's empty.... (why is the array an array of labels anyways).


    Code:
    Dim LabelArray(8) As Label = {new Label, new Label, new Label, new Label, new Label, new Label, new Label, new Label}
    NOW you can use the array of labels (why is it labels again?)

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: String cannot be converted to label?

    Quote Originally Posted by techgnome View Post
    Actually, I'm impressed you aren't getting NULL REference errors... you've defined the array, but it's empty.... (why is the array an array of labels anyways).


    Code:
    Dim LabelArray(8) As Label = {new Label, new Label, new Label, new Label, new Label, new Label, new Label, new Label}
    NOW you can use the array of labels (why is it labels again?)

    -tg
    Actually, he (tries to) set the array values in the SetControlArray method.

    OP:
    I think you want this:
    Code:
        Sub SetControlArray()
            LabelArray(0) = txtAnswer1
            LabelArray(1) = txtAnswer2
            LabelArray(2) = txtAnswer3
            'etc
        End Sub
    (Notice the lack of the Text property!)
    That is assuming that 'txtAnswer..' are Label controls and not TextBoxes as their name might imply.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: String cannot be converted to label?

    Aaaaah... okay... I see what you are saying Nick.... now it starts to make sense... And yes, if that's the intent, then the removal of .Text should work, provided, as Nick noted, they are labels and not text boxes.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    45

    Re: String cannot be converted to label?

    Quote Originally Posted by NickThissen View Post
    I think you want this:
    Code:
        Sub SetControlArray()
            LabelArray(0) = txtAnswer1
            LabelArray(1) = txtAnswer2
            LabelArray(2) = txtAnswer3
            'etc
        End Sub
    (Notice the lack of the Text property!)
    That is assuming that 'txtAnswer..' are Label controls and not TextBoxes as their name might imply.
    If I want to put the array results in a text box, all I would need is to add the ".text" property, correct?

    ie:
    Code:
    Public Class frmBaseExponent
        Dim LabelArray(8) As Label
        Private Sub frmBaseExponent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
           
    
        End Sub
    
        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
            Dim intExponent, intBase As Integer
    
            If Integer.TryParse(txtExponentInput.Text, intExponent) AndAlso Integer.TryParse(txtBaseInput.Text, intBase) Then
                SetControlArray()
                For i As Int16 = 0 To 8
    
                    LabelArray(i).Text = (CInt(intBase) ^ (CInt(intExponent) + i)).ToString
    
                Next
            Else
                MessageBox.Show("Please enter a valid numeric value", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtExponentInput.SelectAll()
                txtExponentInput.Focus()
            End If
    
    
    
        End Sub
    
        Sub SetControlArray()
    
            LabelArray(0).Text = txtAnswer1.Text
    
            LabelArray(1).Text = txtAnswer2.Text
    
            LabelArray(2).Text = txtAnswer3.Text
    
            LabelArray(3).Text = txtAnswer4.Text
    
    
        End Sub
    End Class
    Why do I get a Null reference error when I run this though?

  9. #9

    Re: String cannot be converted to label?

    You declared it as an Array() of Labels, but they were never initialized individually....

    Code:
    LabelArray(0) = New Label
    Do that, or assign the array some value:
    Code:
    LabelArray(0) = someLabelName
    Then you can edit the text of it.

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: String cannot be converted to label?

    This is cool, I'm doing my part for the environment... I'm recycling.... from another post a few moments ago:

    before you can use an object you first have to create it. When you created your array, what you get is essentially a carton of eggs... only it's empty. Before you can use any of the eggs, they have to be created.

    Code:
    Dim carton(12) as egg
    
    egg(0).shell = broken 'this will generat an error... the egg doesn't exist yet
    
    ...
    
    Dim carton2(12) as egg
    carton(0) = new egg   'Here we've created a new egg
    carton(0).shell = broken 'NOW we can use it
    Neat thing about .NET, you can create your carton and fill it at the same time:
    Code:
    Dim carton(12) as egg = {new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg]
    
    carton(0).shell = broken 'woot! this should work
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: String cannot be converted to label?

    I believe you can also do:
    Code:
    Dim carton(12) as egg = {new egg With { .Shell = Broken }, new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg, new egg}
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: String cannot be converted to label?

    Yep... you can do that too...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    45

    Re: String cannot be converted to label?

    Quote Originally Posted by formlesstree4 View Post
    You declared it as an Array() of Labels, but they were never initialized individually....

    Code:
    LabelArray(0) = New Label
    Do that, or assign the array some value:
    Code:
    LabelArray(0) = someLabelName
    Then you can edit the text of it.
    Oh ok, thanks. But assuming I follow a naming convention for my text boxes, how could I make a loop that places "1-9" in text boxes 1-9? How can I incorporate a variable into my own text box name within a loop?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width