Strings won't initialise

My program has been working for weeks.

Today, two things happened:

1. The .sln solution won't open into VB.NET Express, so I had to re-associate the .sln extension with the program.

2. The program will not initialise strings to an empty string.

For example:

Private mystring as string

Msgbox(mystring)

This throws an error, saying the string is not initialised. But it works when I change it to:

Private mystring as string = ""

Why is it suddenly doing this



Answer this question

Strings won't initialise

  • Adema123

    Paul is correct, this is a difference between VB Classic and VB.NET. In classic VB, Strings were initialized to a zero length string. Strings are different in .NET.

    When learning things like this the VB2005 IDE is extremely helpful. Consider:

    Private Function ReturnAString() As String

    End Function

    You will find that End Function has a green squiggle under it. Hovering over this will bring up a tool tip:

    Function 'ReturnAString' does not return a value on all code paths. A null reference exception could ocurr at runtime when the result is used.

    This tells us that there is something odd going on, and would be a good idea to look up help on string data types.



  • Ralphp_Ai2x

    ur first problem happens with me....

    but for the 2nd problem....as strings r "reference" type , u must assign them a empty value...

    U CANT ASSIGN A VALUE TO A TYPE UNTIL THEY ARE PROPERLY INITIALISED

  • Russ Dev

    HI,

    My best guess would be that the default value of variables is Nothing in VB. So you can check it by using the if var is Nothing routine. You need to initialize your variables it to have values...

    cheers,

    Paul June A. Domag



  • Per Dunberg

    Paul is correct, this is a difference between VB Classic and VB.NET. In classic VB, Strings were initialized to a zero length string. Strings are different in .NET.

    When trying to understand and learn things like this, the VB2005 IDE is extremely helpful. Consider:

    Private Function ReturnAString() As String

    End Function

    You will find that End Function has a green squiggle under it. Hovering over this will bring up a tool tip:

    Function 'ReturnAString' does not return a value on all code paths. A null reference exception could ocurr at runtime when the result is used.

    This tells us that there is something odd going on, and would be a good idea to look up help on String data types.

    Additionally, from the origional post, I would deduce that the code posted was origionally VB6 code, through the use of the MsgBox() function. .NET has the Messagebox.show() function (specifically, the Show method of the Messagebox object). While using either is acceptable, I would recommend using the newer function, if only to get into the habit of using the .NET objects and framework.



  • Jerry Sully

    1. I Don't Know....
    2. The String Thing is Normal By Language Definition.....


  • Strings won't initialise