Warning and how to optimize code

Discussion in 'ASP .Net' started by Aaron, Jan 19, 2006.

  1. Aaron

    Aaron Guest

    Hello all,

    I have 5 warnings only but I want to know how to "optimize" my code so that
    it is "clean."

    The warning is this with all 5 the same
    Warning 1 Variable 'strxxx' is used before it has been assigned a value. A
    null reference exception could result at runtime.

    Here is the code

    Dim strxxxAs String

    .....

    .....

    If ckxxx.Checked = True Then

    strxxx = ckxxx.Text

    End If

    ....

    .....

    objMail.Body = ....

    strxxx& vbCrLf + _



    The warnings are in the objMail.Body part of the code.



    TIA

    Aaron
     
    Aaron, Jan 19, 2006
    #1
    1. Advertising

  2. Aaron

    Damien Guest

    Aaron wrote:
    > Hello all,
    >
    > I have 5 warnings only but I want to know how to "optimize" my code so that
    > it is "clean."
    >
    > The warning is this with all 5 the same
    > Warning 1 Variable 'strxxx' is used before it has been assigned a value. A
    > null reference exception could result at runtime.
    >
    > Here is the code
    >
    > Dim strxxxAs String
    >
    > ....
    >
    > ....
    >
    > If ckxxx.Checked = True Then
    >
    > strxxx = ckxxx.Text
    >
    > End If
    >
    > ...
    >
    > ....
    >
    > objMail.Body = ....
    >
    > strxxx& vbCrLf + _
    >
    >
    >
    > The warnings are in the objMail.Body part of the code.
    >

    Well, what if ckxxx.Checked is false? strxxx is equal to Nothing at
    this point, and that's what the compiler is warning you about. What
    should the string be if ckxxx is false? An empty string? Then assign it
    as such. You can do this in your Dim statement, as:

    Dim strxxx as String = ""

    or

    Dim strxxx as String = String.Empty

    or you could assign it later:

    If ckxxx.Checked Then 'Note - why compare it to True?
    strxxx = ckxxx.Text
    Else
    strxxx = ""
    End If

    So, in summary, there's plenty of ways to do this. Are you
    concatenating lots of options together in your objMail.Body line? If
    so, you might want to add the vbCrLf during the assignment to strxxx,
    rather than during concatenation. Otherwise (say, for instance, there
    are 6 options, and options 2 and 5 are selected), your body will be:

    -----(Start of Body)

    Option 2


    Option 5

    -----(End of Body)

    Whereas if you only add the CR/LF to the string if it contains
    something, you'll get:

    -----(Start of Body)
    Option 2
    Option 5
    -----(End of Body)

    Hope this has helped,

    Damien
     
    Damien, Jan 19, 2006
    #2
    1. Advertising

  3. Dear Aaron
    It should change the following code to
    Dim strxxxAs String
    to
    Dim strxxxAs String = ""

    Since you need to pre-define the variable once you declare it

    Hope this help you

    Regards,
    Joe Tsui




    "Aaron" wrote:

    > Hello all,
    >
    > I have 5 warnings only but I want to know how to "optimize" my code so that
    > it is "clean."
    >
    > The warning is this with all 5 the same
    > Warning 1 Variable 'strxxx' is used before it has been assigned a value. A
    > null reference exception could result at runtime.
    >
    > Here is the code
    >
    > Dim strxxxAs String
    >
    > .....
    >
    > .....
    >
    > If ckxxx.Checked = True Then
    >
    > strxxx = ckxxx.Text
    >
    > End If
    >
    > ....
    >
    > .....
    >
    > objMail.Body = ....
    >
    > strxxx& vbCrLf + _
    >
    >
    >
    > The warnings are in the objMail.Body part of the code.
    >
    >
    >
    > TIA
    >
    > Aaron
    >
    >
    >
     
    =?Utf-8?B?Sm9l?=, Jan 19, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pete Becker
    Replies:
    0
    Views:
    1,382
    Pete Becker
    Feb 10, 2005
  2. deepak
    Replies:
    1
    Views:
    290
    Daniel Pitts
    Jan 31, 2007
  3. gtippery
    Replies:
    40
    Views:
    1,001
  4. kenneth
    Replies:
    15
    Views:
    504
    White Wolf
    Dec 19, 2006
  5. TF
    Replies:
    10
    Views:
    736
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=
    Jun 28, 2007
Loading...

Share This Page