Results 1 to 9 of 9

Thread: [RESOLVED] the problem with variable in "if....end"

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    17

    Resolved [RESOLVED] the problem with variable in "if....end"

    codes like below:

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim n As Integer = 3
    Dim b As Integer = 7
    Dim c As integer
    Dim k As String
    Dim x As Integer

    If n > 1 Then
    If x = 5 Then
    k = "23"
    End If

    If x = 6 Then
    k = "33"
    End If
    Else
    k = "99"
    End If

    If b > 5 Then
    c = b + k
    Else
    c = b - k
    End If
    textbox1.text = c
    End Sub


    when I run the codes above, error message indicated:
    Variable k is used before it has been assigned a value.A null reference exception could result at runtime

    pls,thanks

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: the problem with variable in "if....end"

    It isn't an error, its a warning - there is a difference : with an error your code won't compile, with a warning it will compile but there is a good chance it will raise an exception when run.

    All its saying is that by the time you reach this line :

    Code:
    If b > 5 Then
        c = b + k
    Else
        c = b - k
    End If
    K does not necessarily contain anything, ie if n isn't >1 then k will not get set.

    You can remedy this by changing the declaration to :

    Dim K as string = ""

    Although I don't know why it is a string anyway - you are doing purely numerical operations with it as far as I can see.

  3. #3
    Addicted Member
    Join Date
    Mar 2007
    Posts
    163

    Re: the problem with variable in "if....end"

    If you have "option strict" enabled then all warnings will be treated as errors otherwise as Paul said you can continue with your program despite the warning.

    Cheers,

    --Stav.

  4. #4
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: the problem with variable in "if....end"

    Turn Option Strict On. Fix the errors.

    You are mixing data types, which will cause you a runtime error: that is, your program may compile but it will crash when someone tries to run it. (perhaps not in this instance, but the laziness will catch up with you in the future).

    For example:

    dim x as Integer
    x = 5.23 + "fred"


    What is the value of x?
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

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

    Re: the problem with variable in "if....end"

    Quote Originally Posted by stavris View Post
    If you have "option strict" enabled then all warnings will be treated as errors otherwise as Paul said you can continue with your program despite the warning.

    Cheers,

    --Stav.
    That's not true. Having Option Strict On has nothing to do with treating warnings as errors. Open the Compile tab of the project properties and look at the Warning Configurations list. With Option Strict Off the first three are None, i.e. not Warnings or Errors. With Option Strict On those first three are Errors. The setting to treat Warnings as Errors is a completely separate box below that list.
    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

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2008
    Posts
    17

    Re: the problem with variable in "if....end"

    Quote Originally Posted by keystone_paul View Post
    It isn't an error, its a warning - there is a difference : with an error your code won't compile, with a warning it will compile but there is a good chance it will raise an exception when run.

    All its saying is that by the time you reach this line :

    Code:
    If b > 5 Then
        c = b + k
    Else
        c = b - k
    End If
    K does not necessarily contain anything, ie if n isn't >1 then k will not get set.

    You can remedy this by changing the declaration to :

    Dim K as string = ""

    Although I don't know why it is a string anyway - you are doing purely numerical operations with it as far as I can see.
    thanks everyone, especially for paul, one more question:

    paul has mentioned "if n isn't >1 then k will not get set." but actually I set the value of K to "99" in the condition of "n isn't >1" my codes as below:


    If n > 1 Then
    If x = 5 Then
    k = "23"
    End If

    If x = 6 Then
    k = "33"
    End If
    Else
    k = "99"

    End If

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: the problem with variable in "if....end"

    That's a good reason to use the code tags. Without the tags, reading that can get a bit confusing.

    The problem still stands. For K to remain as nothing, all that is necessary is that n be greater than 1, while x is neither 5 nor 6.

    I would also argue for Option Strict ON.
    My usual boring signature: Nothing

  8. #8
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: the problem with variable in "if....end"

    Quote Originally Posted by vbsunst View Post
    thanks everyone, especially for paul, one more question:

    paul has mentioned "if n isn't >1 then k will not get set." but actually I set the value of K to "99" in the condition of "n isn't >1" my codes as below:


    If n > 1 Then
    If x = 5 Then
    k = "23"
    End If

    If x = 6 Then
    k = "33"
    End If
    Else
    k = "99"

    End If
    Incorrect. If k would always get set, then you wouldn't get the warning (VB is smart like that). Step through the EXACT code you have posted and you'll see.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  9. #9
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: [RESOLVED] the problem with variable in "if....end"

    paul has mentioned "if n isn't >1 then k will not get set." but actually I set the value of K to "99" in the condition of "n isn't >1" my codes as below
    Sorry - my mistake

    As ShaggyHiker pointed out, the CODE tags help make your code more readable (its a bugger to work out the maching IFs and END IFs when there's no indentation!) and there are still circumstances in which k doesn't get set.

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