Meaning of "[string]"

P

Peter Merwood

I'm using some sample code from MSDN. The code includes the following
statement:

Dim content as [String] = [String].Empty

I'm not familiar with the use of the square brackets. Can someone please
explain to me what the difference between "Dim content as [String]" and
"Dim content as String". Thanks.

Peter,
 
G

Greg Burns

I've seen square brackets used to allow variable names that match keywords,
but your example is definately odd.

Dim [String] As String = String.Empty

Greg
 
C

Cor Ligthert

Peter,

In the beginning of VBNet where most samples on MSDN translated C# or
translated VB classic ones. This becomes better and better, however there
are still some odd ones. And as Greg stated already this is one of them.
(You are not the first one who ask this question about that sample)
Dim content as [String] = [String].Empty

This sample would imply that you have created your own class with the name
[String] and because there is already a String class, that would be
ambigious and you use the brackets to tell to use your own class.

As well it tells that you have created somewhere your own shared
String.Empty methode and for that obtains the same rules as above.

Only a lunatic will do that.

I hope this explains it something?

Cor


I'm using some sample code from MSDN. The code includes the following
statement:

Dim content as [String] = [String].Empty

I'm not familiar with the use of the square brackets. Can someone please
explain to me what the difference between "Dim content as [String]" and
"Dim content as String". Thanks.

Peter,
 
P

Peter Merwood

Cor

Thanks for the explanation. It all becomes clear now.

I'm not a "lunatic" (!) and I don't have any objects in my project named
"String". Therefore I'll change "Dim content as [String] = [String].Empty"
to "Dim content as String".

The use of the String.Empty initial value by MS is also a bit dumb - aren't
all strings empty by default when they're initialised?

Thanks again.

Peter
--


----------------------------------------------------
This mailbox protected from junk email by MailFrontier Desktop
from MailFrontier, Inc. http://info.mailfrontier.com

Cor Ligthert said:
Peter,

In the beginning of VBNet where most samples on MSDN translated C# or
translated VB classic ones. This becomes better and better, however there
are still some odd ones. And as Greg stated already this is one of them.
(You are not the first one who ask this question about that sample)
Dim content as [String] = [String].Empty

This sample would imply that you have created your own class with the name
[String] and because there is already a String class, that would be
ambigious and you use the brackets to tell to use your own class.

As well it tells that you have created somewhere your own shared
String.Empty methode and for that obtains the same rules as above.

Only a lunatic will do that.

I hope this explains it something?

Cor


I'm using some sample code from MSDN. The code includes the following
statement:

Dim content as [String] = [String].Empty

I'm not familiar with the use of the square brackets. Can someone please
explain to me what the difference between "Dim content as [String]" and
"Dim content as String". Thanks.

Peter,
 
C

Cor Ligthert

Peter,
The use of the String.Empty initial value by MS is also a bit dumb -
aren't
all strings empty by default when they're initialised?

As you said, however there is a slight difference the String is an forever
confusing value what can be an object.

With the String you have this
\\\
dim content as string
///
Gives with
If content Is Nothing then messagebox.show("true")
true
While
dim content as string = "" (what is a constant) does not give that.

They both give
If content = Nothing then messagebox.show("true")
true

There is a lot written in this newsgroup about this, so maybe you can google
this newsgroup for that when you want to know more.

However, I hope this gives some idea's?

Cor

"Peter Merwood"
Cor

Thanks for the explanation. It all becomes clear now.

I'm not a "lunatic" (!) and I don't have any objects in my project named
"String". Therefore I'll change "Dim content as [String] =
[String].Empty"
to "Dim content as String".

The use of the String.Empty initial value by MS is also a bit dumb -
aren't
all strings empty by default when they're initialised?

Thanks again.

Peter
--


----------------------------------------------------
This mailbox protected from junk email by MailFrontier Desktop
from MailFrontier, Inc. http://info.mailfrontier.com

Cor Ligthert said:
Peter,

In the beginning of VBNet where most samples on MSDN translated C# or
translated VB classic ones. This becomes better and better, however there
are still some odd ones. And as Greg stated already this is one of them.
(You are not the first one who ask this question about that sample)
Dim content as [String] = [String].Empty

This sample would imply that you have created your own class with the
name
[String] and because there is already a String class, that would be
ambigious and you use the brackets to tell to use your own class.

As well it tells that you have created somewhere your own shared
String.Empty methode and for that obtains the same rules as above.

Only a lunatic will do that.

I hope this explains it something?

Cor


I'm using some sample code from MSDN. The code includes the following
statement:

Dim content as [String] = [String].Empty

I'm not familiar with the use of the square brackets. Can someone please
explain to me what the difference between "Dim content as [String]"
and
"Dim content as String". Thanks.

Peter,
 
J

Jay B. Harlow [MVP - Outlook]

Peter,
The use of the String.Empty initial value by MS is also a bit dumb -
aren't
all strings empty by default when they're initialised?
No, all strings are Nothing by default when they're initialized.

In addition to Cor's example. Try the following:

Dim content As String
If content.Length = 0 Then
' Never gets here
' as an NullReferenceException was raised!
End IF

VB considers a String variable that is Nothing and a String variable that
has String.Empty in it as equal.

Hope this helps
Jay

Peter Merwood said:
Cor

Thanks for the explanation. It all becomes clear now.

I'm not a "lunatic" (!) and I don't have any objects in my project named
"String". Therefore I'll change "Dim content as [String] =
[String].Empty"
to "Dim content as String".

The use of the String.Empty initial value by MS is also a bit dumb -
aren't
all strings empty by default when they're initialised?

Thanks again.

Peter
--


----------------------------------------------------
This mailbox protected from junk email by MailFrontier Desktop
from MailFrontier, Inc. http://info.mailfrontier.com

Cor Ligthert said:
Peter,

In the beginning of VBNet where most samples on MSDN translated C# or
translated VB classic ones. This becomes better and better, however there
are still some odd ones. And as Greg stated already this is one of them.
(You are not the first one who ask this question about that sample)
Dim content as [String] = [String].Empty

This sample would imply that you have created your own class with the
name
[String] and because there is already a String class, that would be
ambigious and you use the brackets to tell to use your own class.

As well it tells that you have created somewhere your own shared
String.Empty methode and for that obtains the same rules as above.

Only a lunatic will do that.

I hope this explains it something?

Cor


I'm using some sample code from MSDN. The code includes the following
statement:

Dim content as [String] = [String].Empty

I'm not familiar with the use of the square brackets. Can someone please
explain to me what the difference between "Dim content as [String]"
and
"Dim content as String". Thanks.

Peter,
 
H

Herfried K. Wagner [MVP]

Jay,

Jay B. Harlow said:
No, all strings are Nothing by default when they're initialized.

That's true. Strings are initialized with 'Nothing', not 'String.Empty'.
In addition to Cor's example. Try the following:

Dim content As String
If content.Length = 0 Then
' Never gets here
' as an NullReferenceException was raised!
End IF

VB considers a String variable that is Nothing and a String variable that
has String.Empty in it as equal.

Just to make sure that this is not misread: The code above won't throw an
exception if 'String.Empty' was assigned to 'content'. If this was the
case, 'content.Length' would return 0.

What VB does is considering a string variable that points to 'Nothing' and a
string variable that has 'String.Empty' in it as equal when comparing these
string variables using '=':

\\\
Dim s1 As String
Dim s2 As String = String.Empty
MsgBox(CStr(s1 = s2)) ' 'True'.
///

.... and...

\\\
Dim s1 As String
Dim s2 As String = String.Empty
MsgBox(CStr(s1 Is s2)) ' 'False'.
///
 
J

Jay B. Harlow [MVP - Outlook]

Herfried,
Just to make sure that this is not misread: The code above won't throw an
exception if 'String.Empty' was assigned to 'content'. If this was the
case, 'content.Length' would return 0.
Correct, I intentionally left off the initialization of the variable
"content", so it was initialized to its "default" value of Nothing.

Thanks for the additional.

Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top