DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4
  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?

    >
    >



Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links