Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2007

Tip: Looking for answers? Try searching our database.

Parameterless String constructor

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dippykdog@gmail.com - 05 Oct 2007 21:29 GMT
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).IsValueType Then
        Dim ci As ConstructorInfo =
GetType(T).GetConstructor(System.Type.EmptyTypes)
        Dim obj As T = DirectCast(ci.Invoke(Nothing), T)
        Me.SetValue(obj)
     End If
  End Sub

And when I instantiated one of these of type String, I got the famous
NullReferenceException 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 - 05 Oct 2007 22:02 GMT
On Oct 5, 4:29 pm, dippyk...@gmail.com wrote:
> Does anyone know why there isn't a parameterless constructor for the
> String type?
[quoted text clipped - 27 lines]
> 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.
dippykdog@gmail.com - 05 Oct 2007 22:53 GMT
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.
rowe_newsgroups - 06 Oct 2007 02:20 GMT
On Oct 5, 4:53 pm, dippyk...@gmail.com wrote:
> Thanks. You're a good man. (ha)
> Interestingly, whereas this doesn't work...
[quoted text clipped - 10 lines]
>
> There is *SO* much I don't understand.

I always use "Dim x As String = String.Empty"

Thanks,

Seth Rowe
Herfried K. Wagner [MVP] - 06 Oct 2007 20:35 GMT
<dippykdog@gmail.com> schrieb:
> Does anyone know why there isn't a parameterless constructor for the
> String type?
[quoted text clipped - 8 lines]
>
> to give me x pointing the an empty string.

Note that instances of 'String' are immutable.  An empty string can be
obtained using 'String.Empty'.  In addition, make sure you read the
documentation on the 'String' class and string interning.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.