Results 1 to 4 of 4

Thread: [RESOLVED] variable used before it has been assigned a value

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Resolved [RESOLVED] variable used before it has been assigned a value

    Hi folks.

    I have a button, textbox, and some radio buttons inside a groupbox. I enter text into the textbox, and select a radio button, and then click the button. The program stores the text that was entered, as well as the name of the radiobutton in a file. Why am I getting the "variable used before it has been assigned a value' for the freq variable? The value had already been assigned in the For Each loop?

    Thanks in advance.

    EDIT: I am also unsuccessfully trying to add a space between the text and the name of the radiobutton when the string is written to the file.

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim rButton As RadioButton
            Dim freq As String
            Dim NewEvent As String
            For Each rButton In GroupBox1.Controls.OfType(Of RadioButton)()
                If rButton.checked Then
                    freq = rButton.Name
                End If
            Next
            NewEvent = txtNewEventAdd.Text & freq
            My.Computer.FileSystem.WriteAllText("events.txt", NewEvent, True)
            txtEventDisplay.Text = My.Computer.FileSystem.ReadAllText("events.txt")
        End Sub
    
    End Class

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: variable used before it has been assigned a value

    Hi,

    Why am I getting the "variable used before it has been assigned a value' for the freq variable?
    You are getting this warning due to the way you have written your code. To explain, you declare your variable "freq" at the start of your routine, you then enter a For Loop to assign a value to this variable and then once the For Loop has completed, you assign the contents of the "freq" variable to a "NewEvent" variable.

    Now, what Visual Studio has detected is that the For Loop MAY NEVER FIND any RadioButton's in your GroupBox and therefore a RadioButton's Name property may never be assigned to the "freq" variable. Hence the warning about a possible Null reference exception occurring later in your code since no value was assigned to the "freq" variable.

    Knowing this, what you may say to yourself is that YOU KNOW that you do have RadioButton's in your GroupBox therefore you should NOT get this error but what you have to remember is that you can REMOVE controls at any time in your code which could well mean that there may be no RadioButtons in the GroupBox.

    The way to avoid this error is to assign an initial value to your "freq" variable at the time of its declaration. i.e:-

    Code:
    Dim freq As String = String.Empty
    I am also unsuccessfully trying to add a space between the text and the name of the radiobutton when the string is written to the file.
    All you need to do here is to prefix the contents of your "NewEvent" variable with a space character. i.e:-

    Code:
    NewEvent = " " & txtNewEventAdd.Text & freq
    Hope that helps.

    Cheers,

    Ian

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: variable used before it has been assigned a value

    Given that you're using a bit of LINQ anyway, i.e. OfType, you may as well throw in a bit more LINQ and get rid of the loop altogether. If you know for a fact that a RadioButton will be checked then do this:
    Code:
    Dim freq = GroupBox1.Controls.OfType(Of RadioButton)().Single(Function(rb) rb.Checked).Name
    otherwise do this:
    Code:
    Dim option = GroupBox1.Controls.OfType(Of RadioButton)().SingleOrDefault(Function(rb) rb.Checked)
    Dim freq = If(option Is Nothing, String.Empty, option.Name)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Re: [RESOLVED] variable used before it has been assigned a value

    Thanks for the helpful replies, all fixed. I didn't even know that I was using LINQ.

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