473,722 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

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

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
28570
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 flags = (BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly); //Iterate through the Assembly to find Class with a Public Interface.
4
7249
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 function in the world at birth. Defining a parameterless constructor allows it to be created in an unstable state. On the other hand
12
8988
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. Let me mention two cases where I wanted parameterless struct constructors but could not get them. 1st, I needed sortable identity stamps on certain classes of
0
1262
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 table Items. Since the dataset code is constructed by the IDE why am I getting this error and how do I correct it?
1
2646
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 shows the following error on the control in the designer: Error Creating Control - No parameterless constructor defined for this object I have defined four New methods. Although none of them are simply Public Sub New(), one of them has just one...
2
1656
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 constructor of a baseclass is also executed (before your own construcor executes). If you don't call one explicitly, the parameterless constructor is called automatically. I don't know what the VB syntax is. In C# it would be something like:
3
1583
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
3275
by: bednarz.thomas | last post by:
I have the following business Object(s): public class ParentObject { public ParentObject(string somestring) { ...- } ....
9
2693
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, relying on the compiler to do the right thing underneath." Does it mean that if we create a Value type with other constructors, we will still have a parameterless constructor provided by compiler?
0
8739
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9384
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4502
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2602
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.