Click here to Skip to main content
15,887,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there! I have created an application (in Windows Form App. Net) where the user inputs information and it comes out in a second form in a list box. However, when the app runs and the user goes to the second form, the information does not appear anywhere. I am getting errors saying that my "lbResult" and my "Result" are being used before being assigned a value. I am not sure how to assign a value to a string, and my textbook does help me much on this particular matter. Any help on this would be greatly appreciated. Thanks in advance!

VB
Dim lbResults As ListBox
Dim Results As String
Dim strName As String 'declare Hello and txtName as string in order to simplify code
strName = "Hello " & txtName.Text

lbResults.Items.Add(Results)
If radCompliment.Checked = True Then 'if/then branch statement to make message appear depending on what user chooses. 
Results = strName & "You are FABULOUS"

Else
MessageBox.Show("Please input information")

End If

If radMotivation.Checked = True Then

Results = strName & "Stay focused and never give up!"
Else
MessageBox.Show("Please put in information")
End If

If radGreeting.Checked = True Then
Results = strName & "How are you today?  "

Else
MessageBox.Show("Please put in information")
End If

If radJoke.Checked = True Then
Results = strName & "What did the computer do at lunch time? Had a byte!"
Else
MessageBox.Show("Please put in information")
End If

If radFact.Checked = True Then
Results = strName & "Did you know that Banannas are curved because when they grow they gravitate towards the sun."
Else
MessageBox.Show("Please put in information")
End If

End Sub


What I have tried:

I tried at first without the Dim Results as String bit, but found I needed to put all of my User input in one place before trying to put it in the list box, if that makes sense. I have looked online and in my textbook/notes for help on this but have not had much luck. I understand most coding when it is with numbers, but I think making my application with text instead of numbers has made it confusing.
Posted
Updated 23-Apr-19 23:11pm
v2

Look at your code:
Dim lbResults As ListBox
Dim Results As String
Dim strName As String 'declare Hello and txtName as string in order to simplify code
strName = "Hello " & txtName.Text

lbResults.Items.Add(Results)
You declare variables lbResults and Results, but at no point do you give them a value. So when you get to this line:
lbResults.Items.Add(Results)
both lbResults and Results are Nothing and you get a compilation error that you are using an uninitialised value. That's a good thing - because if you didn't then when you ran the code you would get a "null reference error" because there is no ListBox in lbResults to have an Items property!

Probably, you don't want to declare lbResults in that method at all - it will mask the one that is on your form so your user will not see any results anyway. So remove the declaration line and see if that error goes away. If it is replaced by "undefined variable" errors, then you need to put it back, create an instance, and manually add it to your form in order for the user to see anything.

You can also need to add each Results value to your ListBox when you create the Results string - or it won't be seen either.
Like this:
If radCompliment.Checked = True Then 
   Results = strName & "You are FABULOUS"
   lbResults.Items.Add(Results)
Else
   MessageBox.Show("Please input information")
End If
(but you'll need it in every condition as well).
 
Share this answer
 
Comments
Member 14326286 24-Apr-19 6:38am    
Thank you, this helped a lot!
OriginalGriff 24-Apr-19 6:50am    
You're welcome!
Hard to see if this code is inside a method? Either way you talk about two forms. Your forms can't see each other's variables. Shared data needs to live in a data store
 
Share this answer
 
At first :
I prefer that you provide the code how it is really used by you.

2nd:
I suppose that you are using severall Checkboxes. In this case you should call your method (do you have one ?) with every CheckedChange-Event from each Checkbox.

Basicly to give you specific help you have to give much more information (and a better code).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900