Results 1 to 4 of 4

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

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2004
    Location
    Berkshire, UK
    Posts
    41

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

    I have a general question about object declaration and initial instantiation. I regularly need to declare an object and then pass it byref / instantiate it in a Try Catch block. VS2005 always gives a warning:

    Variable '<object name>' is used before it has been assigned a value. A null reference exception could result at runtime.

    My question is do I ignore these warnings / can I explicitly switch them off / should I instead be declaring all objects used in this way like this:

    VB Code:
    1. Dim obj as Object = Nothing

    This gets rid of the warning messages but is the correct way to do things and does it carry a performance penalty?

    Thanks in advance for your comments.

  2. #2
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Variable used before it has been assigned a value

    As you said, it's just a warning. It is up to you to determine whether it could cause problems later. At least you are aware of the possibilities in case you need to debug later. However, you can turn this feature off if you want to.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2004
    Location
    Berkshire, UK
    Posts
    41

    Re: Variable used before it has been assigned a value

    Thanks for your response. Guess I was really after a best practice. Is there any difference between this:
    VB Code:
    1. Dim obj as Object
    and this
    VB Code:
    1. Dim obj as Object = Nothing
    and should you use the latter to suppress the warning messages or simply switch them off. Switching them off could mean that you miss a useful warning in situations where you should have initialised the object, but forgot to do so.

  4. #4
    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

    You shouldn't leave any warnings in a final Release. By explicitly assigning Nothing to the variable you are telling the compiler, and anyone else who may look at the code, that you know and specifically intend for the variable to be a null reference and it wasn't just an oversight. It is good practice to initialise all variables.
    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

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