Parameterless String constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dippykdog@gmail.com

    #1

    Parameterless String constructor

    Does anyone know why there isn't a parameterless constructor for the
    String type?

    Given that you can...

    Dim s as New String("Hello") ' inefficient, don't do this

    I guess I would have expected...

    Dim x as New String()

    to give me x pointing the an empty string.
    For what it's worth, the reason I came across this was because I was
    trying to do this in a generic-based class...

    Class XYZ(Of T)
    Public Sub New()
    If Not GetType(T).IsVa lueType Then
    Dim ci As ConstructorInfo =
    GetType(T).GetC onstructor(Syst em.Type.EmptyTy pes)
    Dim obj As T = DirectCast(ci.I nvoke(Nothing), T)
    Me.SetValue(obj )
    End If
    End Sub

    And when I instantiated one of these of type String, I got the famous
    NullReferenceEx ception because ci remained Nothing. The reason: String
    has no constructor. I've sinced created a new exception for this class
    to throw in such a case, but I still found it odd about String not
    having a constructor without any arguments. I'm sure there's probably
    a reason; I'm just not seeing it.

  • Charlie Brown

    #2
    Re: Parameterless String constructor

    On Oct 5, 4:29 pm, dippyk...@gmail .com wrote:
    Does anyone know why there isn't a parameterless constructor for the
    String type?
    >
    Given that you can...
    >
    Dim s as New String("Hello") ' inefficient, don't do this
    >
    I guess I would have expected...
    >
    Dim x as New String()
    >
    to give me x pointing the an empty string.
    For what it's worth, the reason I came across this was because I was
    trying to do this in a generic-based class...
    >
    Class XYZ(Of T)
    Public Sub New()
    If Not GetType(T).IsVa lueType Then
    Dim ci As ConstructorInfo =
    GetType(T).GetC onstructor(Syst em.Type.EmptyTy pes)
    Dim obj As T = DirectCast(ci.I nvoke(Nothing), T)
    Me.SetValue(obj )
    End If
    End Sub
    >
    And when I instantiated one of these of type String, I got the famous
    NullReferenceEx ception because ci remained Nothing. The reason: String
    has no constructor. I've sinced created a new exception for this class
    to throw in such a case, but I still found it odd about String not
    having a constructor without any arguments. I'm sure there's probably
    a reason; I'm just not seeing it.
    I believe it has to do with the String type being immutable, that is
    it acts more like a value type than a reference type. Each time you
    change a strings value, you actually create a new string, you don't
    change the value of the old one.

    Comment

    • dippykdog@gmail.com

      #3
      Re: Parameterless String constructor

      Thanks. You're a good man. (ha)
      Interestingly, whereas this doesn't work...

      Dim x As String
      x = New String()

      this does...

      Dim x as String
      x = New String(Nothing)

      and x becomes equal to "" (empty string)

      There is *SO* much I don't understand.

      Comment

      • rowe_newsgroups

        #4
        Re: Parameterless String constructor

        On Oct 5, 4:53 pm, dippyk...@gmail .com wrote:
        Thanks. You're a good man. (ha)
        Interestingly, whereas this doesn't work...
        >
        Dim x As String
        x = New String()
        >
        this does...
        >
        Dim x as String
        x = New String(Nothing)
        >
        and x becomes equal to "" (empty string)
        >
        There is *SO* much I don't understand.
        I always use "Dim x As String = String.Empty"

        Thanks,

        Seth Rowe

        Comment

        Working...