Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,072 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,791 people online right now. Registration is fast and FREE... Join Now!




variable used before assigned value warning

 
Reply to this topicStart new topic

variable used before assigned value warning

JasonDurso
19 Apr, 2008 - 11:23 AM
Post #1

New D.I.C Head
*

Joined: 29 Mar, 2008
Posts: 19



Thanked: 1 times
My Contributions
This program is for advertising ads and prices are determined by which radiobutton is selected.This program uses objects created from a class . I am getting a waring message that Variable 'ad' is used before it has been assigned a value. A null reference exception could result at runtime. the warning is at ad.size = 225. So how do I get the variable filled before used if it depends on which radiobutton is checked?


CODE

Public Class Form1
      
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub reserveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles reserveButton.Click
        Dim ad As Ad_to_project

        ' Radiobutton restrictions set

        If halfRadioButton.Checked = True Then
            outsidebackRadioButton.Enabled = False

        End If

        If quaterRadioButton.Checked = True Then
            outsidebackRadioButton.Enabled = False
        End If

        If BuisnessRadioButton.Checked = True Then
            insidefrontRadioButton.Enabled = False
            insidebackRadioButton.Enabled = False
            outsidebackRadioButton.Enabled = False
        End If

        ' Make sure all Textbox's have been filled out

        If companyTextBox.Text = String.Empty AndAlso TelephoneTextBox.Text = String.Empty AndAlso dateTextBox.Text = String.Empty Then
            MessageBox.Show(" You must enter all information", "Buzz Advertising", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

        'Determine base price based on checked ad size radiobutton

        If fullRadioButton.Checked = True Then
            ad.Size = 225.0
        End If

        If halfRadioButton.Checked = True Then
            ad.Size = 225.0 * 0.65
        End If

        If quaterRadioButton.Checked = True Then
            ad.Size = 225.0 * 0.4
        End If

        If BuisnessRadioButton.Checked = True Then
            ad.Size = 35.0
        End If

        ' Determine markup based upon checked location radiobutton

        If outsidebackRadioButton.Checked = True Then
            ad.Location = 1.4
        End If

        If insidebackRadioButton.Checked = True Then
            ad.Location = 1.15
        End If

        If insidefrontRadioButton.Checked = True Then
            ad.Location = 1.2
        End If

        If interiorRadioButton.Checked = True Then
            ad.Location = 1.0
        End If

        'Display total price
        priceLabel.Text = ad.Price.ToString("C2")



    End Sub

    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
        Me.Close()
    End Sub
End Class


class code

CODE

Public Class Ad_to_project
    Public Company As String
    Public Telephone As String
    Public addate As String
    Public Size As Decimal
    Public Location As Decimal
    Public Price As Decimal = ("C2")

    Public Sub New()
        Price = Size * Location
    End Sub


End Class

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Variable Used Before Assigned Value Warning
19 Apr, 2008 - 12:02 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You declare your object but you never instantiate it, try this


vb

Dim ad As New Ad_to_project

User is offlineProfile CardPM
+Quote Post

JasonDurso
RE: Variable Used Before Assigned Value Warning
19 Apr, 2008 - 02:53 PM
Post #3

New D.I.C Head
*

Joined: 29 Mar, 2008
Posts: 19



Thanked: 1 times
My Contributions
ok figured out and got rid of the error but still have a problem everything runs correctly except for displaying in the pricelabel it just display's $0.00 which if I understand correctly this still means my variables are not getting filled?

CODE

Private Sub reserveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles reserveButton.Click

        Dim ad As New Ad_to_project

        'If no location radiobutton checked then display message

        If outsidebackRadioButton.Checked = False AndAlso insidebackRadioButton.Checked = False AndAlso insidefrontRadioButton.Checked = False AndAlso interiorRadioButton.Checked = False Then
            MessageBox.Show("You must choose a location", "Buzz Advertising", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub
        End If

        ' Make sure all Textbox's have been filled out

        If companyTextBox.Text = String.Empty AndAlso TelephoneTextBox.Text = String.Empty AndAlso dateTextBox.Text = String.Empty Then
            MessageBox.Show(" You must enter all information", "Buzz Advertising", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub
        End If

        'Determine base price based on checked ad size radiobutton


        If fullRadioButton.Checked = True Then
            ad.Size = 225.0
        End If

        If halfRadioButton.Checked = True Then
            ad.Size = 225.0 * 0.65

        End If
        If quaterRadioButton.Checked = True Then
            ad.Size = 225.0 * 0.4
        End If

        If BuisnessRadioButton.Checked = True Then
            ad.Size = 35.0
        End If

        ' Determine markup based upon checked location radiobutton

        If outsidebackRadioButton.Checked = True Then
            ad.Location = 1.4


        ElseIf insidebackRadioButton.Checked = True Then
            ad.Location = 1.15


        ElseIf insidefrontRadioButton.Checked = True Then
            ad.Location = 1.2


        ElseIf interiorRadioButton.Checked = True Then
            ad.Location = 1.0
        End If

        'Display total price
        priceLabel.Text = ad.Price.ToString("C2")


class code
CODE

Public Class Ad_to_project
    Public Company As String
    Public Telephone As String
    Public addate As String
    Public Size As Decimal
    Public Location As Decimal
    Public Price As Decimal

    Public Sub New()
        Price = Size * Location
    End Sub


End Class


User is offlineProfile CardPM
+Quote Post

Jayman
RE: Variable Used Before Assigned Value Warning
19 Apr, 2008 - 04:49 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,319



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Would I be correct in assuming that you are making sure that you have a radio button selected before you are clicking the button?

Secondly if you are going to create a class to contain your property values then, you should do it the correct way.

The global variables should be private to help promote encapsulation. You should create public properties that allow access to those private variables. Next inside the New subroutine you should only be initializing the values of the variables. See the following example of your code.
Example:
CODE

Public Class Ad_to_project
    Private m_Company As String
    Private m_Telephone As String
    Private m_addate As String
    Private m_Size As Decimal
    Private m_Location As Decimal
    Private m_Price As Decimal

    Public Sub New()
        m_Company = ""
        m_Telephone = ""
        m_addate = ""
        m_Size = 0.0
        m_Location = 0.0
        m_Price = 0.0
    End Sub

    Public Property Company() As String
        Get
            Return m_Company
        End Get
        Set(ByVal value As String)
            m_Company = value
        End Set
    End Property

    Public Property Telephone() As String
        Get
            Return m_Telephone
        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public Property AdDate() As String
        Get
            Return m_addate
        End Get
        Set(ByVal value As String)
            m_addate = value
        End Set
    End Property

    Public Property Size() As Decimal
        Get
            Return m_Size
        End Get
        Set(ByVal value As Decimal)
            m_Size = value
        End Set
    End Property

    Public Property Location() As Decimal
        Get
            Return m_Location
        End Get
        Set(ByVal value As Decimal)
            m_Location = value
        End Set
    End Property

    Public Property Price() As Decimal
        Get
            Return m_Price
        End Get
        Set(ByVal value As Decimal)
            m_Price = value
        End Set
    End Property
End Class


Now when you instantiate an object of this class all the variables are correctly initialized to default values. And you can use the properties in the same manner that you currently are.
User is offlineProfile CardPM
+Quote Post

JasonDurso
RE: Variable Used Before Assigned Value Warning
20 Apr, 2008 - 12:53 PM
Post #5

New D.I.C Head
*

Joined: 29 Mar, 2008
Posts: 19



Thanked: 1 times
My Contributions
Thanks for your help guys. Got it all working now.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Variable Used Before Assigned Value Warning
20 Apr, 2008 - 01:01 PM
Post #6

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Glad we could help smile.gif Now back to the dungeon with you! tongue.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:04PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month