Strings Not Being Initialized


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Strings Not Being Initialized

  1. #1
    Kevin Baker Guest

    Strings Not Being Initialized


    Try this simple code:
    Dim sString As String
    Dim nLen = sString.Length()

    You get an exception. Will that be fixed or do I have to initialize every
    string to avoid this risk, like this:

    Dim sString As String = ""
    Dim nLen = sString.Length()

    It would seem that VB would initialize to empty string whereas I imagine
    C# might not. Is it a bug or a feature?

  2. #2
    Gregor R. Peisker Guest

    Re: Strings Not Being Initialized

    Hi Kevin,

    > It would seem that VB would initialize to empty string whereas I imagine
    > C# might not. Is it a bug or a feature?


    VB initializes the a string to Nothing (System.String is a reference type). I
    think that's the right thing to do since it's consistent with other reference
    types.

    However, things are a little more convenient in VB.NET than in C# in that
    comparing a Nothing string and an empty string (using the = and <> operators,
    not the Compare method!) returns True.

    Gregor



  3. #3
    Bill Storage Guest

    Re: Strings Not Being Initialized

    In your example, the VB Len function will tolerate Len(sString). The string
    class's .Length function (framework function) requires a valid string
    variable.

    Bill


    "Kevin Baker" <Kevin@KevinJBaker.com> wrote in message
    news:3baa13e2$1@news.devx.com...
    >
    > Try this simple code:
    > Dim sString As String
    > Dim nLen = sString.Length()
    >
    > You get an exception. Will that be fixed or do I have to initialize

    every
    > string to avoid this risk, like this:
    >
    > Dim sString As String = ""
    > Dim nLen = sString.Length()
    >
    > It would seem that VB would initialize to empty string whereas I imagine
    > C# might not. Is it a bug or a feature?




  4. #4
    nick symmonds Guest

    Re: Strings Not Being Initialized


    You can access the classic VB6 "len" function in both VB.NET and C# and Python.NET
    and COBAL.NET, etc. Use the Microsoft.Visualbasic.Strings namespace. Use
    an alias if you do not want to conflict with other namespaces.

    Example.

    Imports MS = Microsoft.Visualbasic.strings
    ...

    l = MS.Len(sString)


    "Bill Storage" <storage@youknow.com> wrote:
    >In your example, the VB Len function will tolerate Len(sString). The string
    >class's .Length function (framework function) requires a valid string
    >variable.
    >
    >Bill
    >
    >
    >"Kevin Baker" <Kevin@KevinJBaker.com> wrote in message
    >news:3baa13e2$1@news.devx.com...
    >>
    >> Try this simple code:
    >> Dim sString As String
    >> Dim nLen = sString.Length()
    >>
    >> You get an exception. Will that be fixed or do I have to initialize

    >every
    >> string to avoid this risk, like this:
    >>
    >> Dim sString As String = ""
    >> Dim nLen = sString.Length()
    >>
    >> It would seem that VB would initialize to empty string whereas I imagine
    >> C# might not. Is it a bug or a feature?

    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center