|
-
Jan 30th, 2010, 10:40 AM
#1
Thread Starter
Member
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.
-
Jan 30th, 2010, 10:57 AM
#2
Re: String cannot be converted to label?
Have you considered doing this:
LabelArray(0).Text = txtAnswer1.Text
?
-
Jan 30th, 2010, 11:57 AM
#3
Thread Starter
Member
Re: String cannot be converted to label?
 Originally Posted by cicatrix
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.
-
Jan 30th, 2010, 12:21 PM
#4
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.
-
Jan 30th, 2010, 01:17 PM
#5
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
-
Jan 30th, 2010, 01:22 PM
#6
Re: String cannot be converted to label?
 Originally Posted by techgnome
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.
-
Jan 30th, 2010, 02:00 PM
#7
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
-
Jan 30th, 2010, 09:24 PM
#8
Thread Starter
Member
Re: String cannot be converted to label?
 Originally Posted by NickThissen
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?
-
Jan 30th, 2010, 09:30 PM
#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.
-
Jan 30th, 2010, 10:24 PM
#10
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
-
Jan 30th, 2010, 11:01 PM
#11
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}
-
Jan 30th, 2010, 11:38 PM
#12
Re: String cannot be converted to label?
Yep... you can do that too...
-tg
-
Jan 30th, 2010, 11:39 PM
#13
Thread Starter
Member
Re: String cannot be converted to label?
 Originally Posted by formlesstree4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|