472,119 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Meaning of "[string]"

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,

--
----------------------------------------------------
This mailbox protected from junk email by MailFrontier Desktop
from MailFrontier, Inc. http://info.mailfrontier.com
Nov 21 '05 #1
8 4685
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

"Peter Merwood" <pe*****@circle-consulting.co.nz> wrote in message
news:ey**************@TK2MSFTNGP09.phx.gbl...
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,

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

Nov 21 '05 #2
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
"Peter Merwood" <pe*****@circle-consulting.co.nz>

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,

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

Nov 21 '05 #3
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" <no************@planet.nl> wrote in message
news:ez*************@TK2MSFTNGP15.phx.gbl...
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
"Peter Merwood" <pe*****@circle-consulting.co.nz>

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,

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


Nov 21 '05 #4
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" <no************@planet.nl> wrote in message
news:ez*************@TK2MSFTNGP15.phx.gbl...
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
"Peter Merwood" <pe*****@circle-consulting.co.nz>

> 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,
>
> --
>
>
> ----------------------------------------------------
> This mailbox protected from junk email by MailFrontier Desktop
> from MailFrontier, Inc. http://info.mailfrontier.com
>
>



Nov 21 '05 #5
"Peter Merwood" <pe*****@circle-consulting.co.nz> schrieb:
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".


The square brackets are documented here:

<URL:http://msdn.microsoft.com/library/en-us/vbls7/html/vblrfVBSpec2_2.asp>

In the sample code you showed above, square brackets don't make much sense.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #6
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" <pe*****@circle-consulting.co.nz> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... 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" <no************@planet.nl> wrote in message
news:ez*************@TK2MSFTNGP15.phx.gbl...
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
"Peter Merwood" <pe*****@circle-consulting.co.nz>

> 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,
>
> --
>
>
> ----------------------------------------------------
> This mailbox protected from junk email by MailFrontier Desktop
> from MailFrontier, Inc. http://info.mailfrontier.com
>
>



Nov 21 '05 #7
Jay,

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb:
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.


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'.
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #8
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
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uD**************@TK2MSFTNGP09.phx.gbl... Jay,

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb:
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.


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'.
///

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

Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by ondekoza | last post: by
35 posts views Thread by pinkfloydhomer | last post: by
10 posts views Thread by lovecreatesbea... | last post: by

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.