VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

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

Join 307,078 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,252 people online right now. Registration is fast and FREE... Join Now!




Warnings on operational sltn

 

Warnings on operational sltn

Gjobsin

2 Sep, 2009 - 07:17 PM
Post #1

New D.I.C Head
*

Joined: 23 Feb, 2009
Posts: 37

So my project works exactly as I want it to. But I have these 2 similar warnings when I Build. I would like to understand and get rid of if possible. Any input would be appreciated.

Warning 1 Variable 'EndMileage' is used before it has been assigned a value. A null reference exception could result at runtime. C:\Users\Owner\Documents\Visual Studio 2008\Projects\Assignment2\Assignment2\CarRental.vb 30 22 Assignment2
Warning 2 Variable 'DaysUsed' is used before it has been assigned a value. A null reference exception could result at runtime. C:\Users\Owner\Documents\Visual Studio 2008\Projects\Assignment2\Assignment2\CarRental.vb 31 36 Assignment2

CODE
Public Class CarRentalCalculator
    'delcare constants
    Const PerMileRate As Decimal = 0.12D
    Const DailyRate As Integer = 15D



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcButton.Click
        'declare variables
        Dim StartMileage As Integer, EndMileage, DaysUsed, TotalMiles
        Dim TotalCharge As Decimal


        'Convert input to values of variables

        Try

            StartMileage = Integer.Parse(StartMileageBox.Text)
            EndMileage = Integer.Parse(EndMileageBox.Text)
            DaysUsed = Integer.Parse(DaysUsedBox.Text)

        Catch ex As FormatException

            MessageBox.Show("Error in input.")

        End Try


        'Calculate totals
        TotalMiles = EndMileage - StartMileage
        TotalCharge = (DailyRate * DaysUsed) + (TotalMiles * PerMileRate)

        'Display Totals

        MilesDrivenBox.Text = TotalMiles.ToString
        TotalChargeBox.Text = TotalCharge.ToString

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintButton.Click

        'print form
        PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
        PrintForm1.Print()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click

        'clear fields
        NameBox.Clear()
        AdressBox.Clear()
        StateBox.Clear()
        CityBox.Clear()
        ZipBox.Clear()
        StartMileageBox.Clear()
        EndMileageBox.Clear()
        DaysUsedBox.Clear()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        Me.Close()

    End Sub

End Class


User is offlineProfile CardPM
+Quote Post


mark.bottomley

RE: Warnings On Operational Sltn

2 Sep, 2009 - 08:00 PM
Post #2

D.I.C Addict
****

Joined: 22 Apr, 2009
Posts: 868



Thanked: 148 times
My Contributions
Because the possible code paths include leaving the try block at any statement, it is possible to get to the catch block and continue into th calculations without having set those 2 variables.

Solutions are to either initialize those variables with default values e.g. 0 or put all the calculation code inside the try block so that it only gets executed in the inputs parse correctly. The second choice is the correct version, although both will work.
User is offlineProfile CardPM
+Quote Post

Martyr2

RE: Warnings On Operational Sltn

2 Sep, 2009 - 08:02 PM
Post #3

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,307



Thanked: 837 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
You must consider all paths of execution in your functions and make sure no variable could be potentially used without first being given a value. For instance, in your function consider that your try catch fails, you skip over the try and land in your catch where you print a message to the user. Fine. But then it continues and you go on to using Endmileage as if the try had succeeded. But if it went to the catch statement, EndMileage has no value.

You can fix this problem by trying to assign a default value to all your variables at the beginning of the method. So by setting EndMileage to a value first (don't rely on the compiler to do this for you) you can guarantee that if the try catch fails and EndMileage isn't set, that it will still work ok down in the calculation using a default value. It is the same problem for DaysUsed.

Always make sure that all paths of execution (one where the try catch works ok and one where it fails) still keeps all variables in some kind of "valid" state if you are going to then use it later on in calculations and such.

Hope this helps!

"At DIC we be path execution checking code ninjas... but for us there is only one path to execution, and that path is in our way." decap.gif
User is online!Profile CardPM
+Quote Post

Gjobsin

RE: Warnings On Operational Sltn

2 Sep, 2009 - 08:37 PM
Post #4

New D.I.C Head
*

Joined: 23 Feb, 2009
Posts: 37

great thorough answers, thank you very much
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 10:28AM

Live VB.NET Help!

Be Social

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

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month