473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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).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.

Oct 5 '07 #1
3 1614
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).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.
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.

Oct 5 '07 #2
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.

Oct 5 '07 #3
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

Oct 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: brian c | last post by:
************************************** //Load the Assembly Assembly a = Assembly.LoadFrom(sAssembly); //Get Types so we can Identify the Interface. Type mytypes = a.GetTypes(); BindingFlags...
4
by: Robert Zurer | last post by:
Hello All, Is it considered a best practice to always write a parameterless constructor for any object - just in case? I'm not sure. I want my object to have all it absolutely requires to...
12
by: Ole Nielsby | last post by:
Why is this? I've stumbled on this restriction more than once, and I'd like to know the philosophy behind it if there is one. I figure I'd be less prone to make this error if I knew the reason....
0
by: JymBoe | last post by:
I have built a Web Service with a multi-table dataset. When I attempt to discover the Web Service, I get an error saying "ItemsRow has no parameterless constructor" where ItemsRow is a row in the...
1
by: Nathan Sokalski | last post by:
I have created a custom control for ASP.NET using VB.NET. My control inherits from the System.Web.UI.WebControls.CompositeControl class, and is working fine. However, the Visual Studio .NET designer...
2
by: Hans Kesting | last post by:
on 23-9-2008, Julia B supposed : Usually that errormessage means that you derived a class from a baseclass that has no parameterless constructors. Always when you call a constructor, a...
3
by: =?Utf-8?B?SnVsaWEgQg==?= | last post by:
Solved it. I had to rewrite my classes in my business & datalayer changing their constructors. Julia "Julia B" wrote:
2
by: bednarz.thomas | last post by:
I have the following business Object(s): public class ParentObject { public ParentObject(string somestring) { ...- } ....
9
by: puzzlecracker | last post by:
"The C# specification states that all value types have a default parameterless constructor, and it uses the same syntax to call both explicitly declared constructors and the parameterless one,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.