473,320 Members | 1,978 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,320 software developers and data experts.

A bit stringy

Nak
Hi there,

I have been wondering about something regarding strings in VB.NET. I
have seen the following all over the place,

if(mystring = nothing) then do something

or

if(mystring = string.empty) then do something

I thought strings were a primative data type and were not referenced
data types. Meaning that a string cannot technically be nothing, it will
just contain no data,

if(mystring = "") then do something

of

if(mystring.length = 0) then do something

Does this make more sence? I don't doubt that asking if the string is
equal to string.empty is correct, but the main point is

string.empty = ""

Hmmmmmm, all of these are valid methods,but has anyone else got any
views on these? Id rather check against "", it's quicker to type!!!

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Nov 20 '05 #1
33 1339
Hi Nick,
According to my understanding, if you declare a string
variable like:
Dim mystr as string
and if you don't assign any specific value to it, the
default value of mystr is nothing.

-----Original Message-----
Hi there,

I have been wondering about something regarding strings in VB.NET. Ihave seen the following all over the place,

if(mystring = nothing) then do something

or

if(mystring = string.empty) then do something

I thought strings were a primative data type and were not referenceddata types. Meaning that a string cannot technically be nothing, it willjust contain no data,

if(mystring = "") then do something

of

if(mystring.length = 0) then do something

Does this make more sence? I don't doubt that asking if the string isequal to string.empty is correct, but the main point is

string.empty = ""

Hmmmmmm, all of these are valid methods,but has anyone else got anyviews on these? Id rather check against "", it's quicker to type!!!
Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ \/\/\/\/\/\/\/\/\/\"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ \/\/\/\/\/\/\/\/\/\

.

Nov 20 '05 #2
Nothing is nothing, so if you declared a string like this...

Dim a as String
and then tried If a.Length, you'd get a null reference exception.

On the other hand, if you use Dim a as String = string.Empty
and did they same, you'd be fine. Basically it's a more effiicient way of
setting a string to "" because it doesn't have to create a second string for
the "".

HTH,

Bill
"Nak" <a@a.com> wrote in message
news:OD**************@TK2MSFTNGP11.phx.gbl...
Hi there,

I have been wondering about something regarding strings in VB.NET. I
have seen the following all over the place,

if(mystring = nothing) then do something

or

if(mystring = string.empty) then do something

I thought strings were a primative data type and were not referenced
data types. Meaning that a string cannot technically be nothing, it will
just contain no data,

if(mystring = "") then do something

of

if(mystring.length = 0) then do something

Does this make more sence? I don't doubt that asking if the string is
equal to string.empty is correct, but the main point is

string.empty = ""

Hmmmmmm, all of these are valid methods,but has anyone else got any
views on these? Id rather check against "", it's quicker to type!!!

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ "No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

Nov 20 '05 #3
I believe that since everything is an object in .NET (strings included), they can certainly now be
nothing.
"Nak" <a@a.com> wrote in message news:OD**************@TK2MSFTNGP11.phx.gbl...
Hi there,

I have been wondering about something regarding strings in VB.NET. I
have seen the following all over the place,

if(mystring = nothing) then do something

or

if(mystring = string.empty) then do something

I thought strings were a primative data type and were not referenced
data types. Meaning that a string cannot technically be nothing, it will
just contain no data,

if(mystring = "") then do something

of

if(mystring.length = 0) then do something

Does this make more sence? I don't doubt that asking if the string is
equal to string.empty is correct, but the main point is

string.empty = ""

Hmmmmmm, all of these are valid methods,but has anyone else got any
views on these? Id rather check against "", it's quicker to type!!!

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

Nov 20 '05 #4
"Nak" <a@a.com> schrieb
Hi there,

I have been wondering about something regarding strings in
VB.NET. I
have seen the following all over the place,

if(mystring = nothing) then do something

or

if(mystring = string.empty) then do something

I thought strings were a primative data type and were not
referenced
data types. Meaning that a string cannot technically be nothing, it
will just contain no data,

if(mystring = "") then do something

of

if(mystring.length = 0) then do something

Does this make more sence? I don't doubt that asking if the
string is
equal to string.empty is correct, but the main point is

string.empty = ""

Hmmmmmm, all of these are valid methods,but has anyone else got
any
views on these? Id rather check against "", it's quicker to
type!!!

Strings are reference types, therefore they can be Nothing (not the String
itself, but the variable that can hold a reference). An empty string means
that there is a string but it's length is 0. String.Empty is a zero
length string. For me, the meaning of Nothing is _only_ "no reference". As
references are compared by the Is operator, I never use the "=" operator
with Nothing.
--
Armin

Nov 20 '05 #5
In article <OD**************@TK2MSFTNGP11.phx.gbl>, Nak wrote:
Hi there,

I have been wondering about something regarding strings in VB.NET. I
have seen the following all over the place,

if(mystring = nothing) then do something

or

if(mystring = string.empty) then do something

Strings are reference types. They behave sort of like value types,
because they are immutable - in other words, all of the string methods
that may modify a string, return a new string.

That's why you can compare a string to nothing. Now, it should be noted
that the VB.NET compiler, treats the above two statements the same,
event though they are different. String.Empty evaluates to "", Nothing
is a null reference.
I thought strings were a primative data type and were not referenced
data types. Meaning that a string cannot technically be nothing, it will
just contain no data,

if(mystring = "") then do something

of

if(mystring.length = 0) then do something

Does this make more sence? I don't doubt that asking if the string is
equal to string.empty is correct, but the main point is

string.empty = ""

Hmmmmmm, all of these are valid methods,but has anyone else got any
views on these? Id rather check against "", it's quicker to type!!!

Nick.


The problem with using the mystring.Length = 0, is that if mystring is not
assigned, as in:

Dim mystring As String

If mystring.Length = 0 Then
MessageBox.Show("No String")
End If

You will get a NullReferenceException. That's why the first two tests
are more common. From a performance perspective, I would think the
reference check, would be the fastest - except that if s is String.Empty
("") then the test will fail. So, basically - the easiest way is to
test for "" or string.Empty.

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #6
Cor
Hi Armin,

I did want to show this to you all some days.

I find it so funny and I real need it.

\\\
If Not drXML.Item("gebdat") Is Nothing Then
If drXML.Item("gebdat") <> Nothing Then
'do something
end if
end if
///

It are XML elements that can be not there, can be have empty values or can
be with some data.

(I did use =nothing while I normaly use ="", but in this case I did it this
way)

Cor
Nov 20 '05 #7
Howdy Nick,

You're working bloody hard, mate - good on you!! ;-))

The VB.NET reference defines a 'primitive type' as one which gets its own
keyword. That's pretty much the prime factor.

Type
Dim I As Integer
Dim I As Int32
Dim S As String
Dim S As System.String

The Integer and String will be in blue (or whatever colour Private, Form
etc is in) because they are keywords.
Int32, the same underlying type of Integer, and Sysytem.String, (the
under.....) will be in black because they are not keywords.

Another aspect of the primitive types is that they can have literals which
is a sort of shorthand for the full-on instance construction.
1234 is <effectively> shorthand for New Integer (1234)
"Hi!!" is <effectively> shorthand for New String ("Hi!!")
I say 'effectively' because there's an obvious circularity in those
declarations!

Getting back to String, then. It is a primitive types but it's also a
reference type - due to its variable lengthed nature. A (shortish) fixed
length string would be better as a ValueType.

Like you, I don't see much point in String.Empty - a lot of typing to
create something that looks like is should <be> something rather than nothing,
if you get my drift! [Cor likes these sentences too ;-)]

With checking S.Length, it'll fail if S is Nothing so you'd need to check
that first. Nah!

|| I'd rather check against "", it's quicker to type!!!

Me too, and it works whether the string is "" or Nothing.

This piece of code:
S = Nothing
If S = "" Then
MsgBox ("Empty")
Else
MsgBox ("Null")
End If
will give the "Empty" message not the "Null". So, within a condition,
Nothing is equal to "" - which is pretty handy, if you ask me. It's primitive,
but it ain't stoopid. ;-)

Regards,
Fergus

Nov 20 '05 #8
Nak
Hi Tracey,
According to my understanding, if you declare a string
variable like:
Dim mystr as string
and if you don't assign any specific value to it, the
default value of mystr is nothing.


This is quite interesting, as what I have read in a book recently is that
all types are initialized with their default values, i.e.

integer = 0
string = ""
object = nothing

So if string is being initialized as nothing, it must be a referenced data
type. Which I am quite suprised about, I knew that all classes were derived
from object, but I thought these had exceptions and that is why you needn't
use the new keyword to initialize a type that is non referenced!

Well, I have certainly been put straight on this one, lots of replies :-)

Thanks for the help.

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Nov 20 '05 #9
Nak
Hi there,
Nothing is nothing, so if you declared a string like this...

Dim a as String
and then tried If a.Length, you'd get a null reference exception.

On the other hand, if you use Dim a as String = string.Empty
and did they same, you'd be fine. Basically it's a more effiicient way of
setting a string to "" because it doesn't have to create a second string for the "".


This is making slightly more sence now. I understood that if you changed
the value of a string it simply created a whole new object, but I suppose it
didn't really sink in enough, so even though a string is a referenced type,
so to speak, you cant use the new keyword? you need to actually set it to
another object,

So basically, it's a referenced type, as simple as that, but a simple
referenced type?

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Nov 20 '05 #10
Nak
Hi Rick,
I believe that since everything is an object in .NET (strings included), they can certainly now be nothing.
I'm not sure if that is totally true, as integers (simple data types) cannot
be nothing, they are set to their default values, for example,

Dim pop As Integer = Nothing
MessageBox.Show(pop.ToString)

this should result in an exception, but it doesn't, "0" is displayed. I
think the material I have read recently was slightly wrong by saying that
strings were simple types, they are reference types. Thanks for your help
:-)

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"Rick Mogstad" <ri**@NOSPAM.computetosuit.com> wrote in message
news:e0**************@tk2msftngp13.phx.gbl...


"Nak" <a@a.com> wrote in message

news:OD**************@TK2MSFTNGP11.phx.gbl...
Hi there,

I have been wondering about something regarding strings in VB.NET. I have seen the following all over the place,

if(mystring = nothing) then do something

or

if(mystring = string.empty) then do something

I thought strings were a primative data type and were not referenced
data types. Meaning that a string cannot technically be nothing, it will just contain no data,

if(mystring = "") then do something

of

if(mystring.length = 0) then do something

Does this make more sence? I don't doubt that asking if the string is equal to string.empty is correct, but the main point is

string.empty = ""

Hmmmmmm, all of these are valid methods,but has anyone else got any
views on these? Id rather check against "", it's quicker to type!!!

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ "No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\


Nov 20 '05 #11
Hi Rick,

The ValueTypes are derived from Object but they aren't reference types.
These (numericals, bytes, chars, DateTime and other structures) only have zero
available to them. No Nothings, unless the structure contains a member which
is a reference type.

Regards,
Fergus
Nov 20 '05 #12
Nak
Hi Armin,
Strings are reference types, therefore they can be Nothing (not the String
itself, but the variable that can hold a reference). An empty string means
that there is a string but it's length is 0. String.Empty is a zero
length string. For me, the meaning of Nothing is _only_ "no reference". As
references are compared by the Is operator, I never use the "=" operator
with Nothing.


That answers it all, I read that strings weren't reference types. It all
makes much more sence now to me thinking that they are now. I had wondered
about this because I had seen different representations written all over the
place and was slightly confused, so what are the simple data types? just the
numerical ones? Thanks for your help Armin.

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Nov 20 '05 #13
Nak
Hi Tom,
You will get a NullReferenceException. That's why the first two tests
are more common. From a performance perspective, I would think the
reference check, would be the fastest - except that if s is String.Empty
("") then the test will fail. So, basically - the easiest way is to
test for "" or string.Empty.


Yeah, it is all clear now, thanks for your help. Come to think of it, I'm
suprised I have come across these exceptions, I must have always given my
strings an empty string reference in some way or another without actually
noticing it. Thanks again :-)

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Nov 20 '05 #14
Nak
Hi Fergus,
The ValueTypes are derived from Object but they aren't reference types. These (numericals, bytes, chars, DateTime and other structures) only have zero available to them. No Nothings, unless the structure contains a member which is a reference type.


Aah, even more clearer, Value Types, I was missing that word :-) So I
suppose an enum is a value type and so are all the data types, all is clear
again. I feel like I'm back at school :-)

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Nov 20 '05 #15
Cor
Hi Nick,

Some threads below Jay B gives very good examples of this, very simple and
explains all

Cor

Nov 20 '05 #16
Nak,
As Armin pointed out String is a Reference Type, it inherits from
System.Object directly.
I thought strings were a primative data type and were not referenced
data types. Meaning that a string cannot technically be nothing, it will
just contain no data, Primative types can be either value types or reference types. It just
happens all primative types except String are value types.
if(mystring = nothing) then do something
or
if(mystring = string.empty) then do something
Because String is a primative type, the VB.NET compiler is "helping you out"
by allowing the comparison of a string reference that is Nothing to be
compared as true with a string reference to a zero length string.

Hope this helps
Jay

"Nak" <a@a.com> wrote in message
news:OD**************@TK2MSFTNGP11.phx.gbl... Hi there,

I have been wondering about something regarding strings in VB.NET. I
have seen the following all over the place,

if(mystring = nothing) then do something

or

if(mystring = string.empty) then do something

I thought strings were a primative data type and were not referenced
data types. Meaning that a string cannot technically be nothing, it will
just contain no data,

if(mystring = "") then do something

of

if(mystring.length = 0) then do something

Does this make more sence? I don't doubt that asking if the string is
equal to string.empty is correct, but the main point is

string.empty = ""

Hmmmmmm, all of these are valid methods,but has anyone else got any
views on these? Id rather check against "", it's quicker to type!!!

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ "No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

Nov 20 '05 #17
Yep, i am definitely wrong. I am a .NET novice still. A great thread though, ive learned a lot!
"Nak" <a@a.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
Hi Rick,
I believe that since everything is an object in .NET (strings included),

they can certainly now be
nothing.


I'm not sure if that is totally true, as integers (simple data types) cannot
be nothing, they are set to their default values, for example,

Dim pop As Integer = Nothing
MessageBox.Show(pop.ToString)

this should result in an exception, but it doesn't, "0" is displayed. I
think the material I have read recently was slightly wrong by saying that
strings were simple types, they are reference types. Thanks for your help
:-)

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"Rick Mogstad" <ri**@NOSPAM.computetosuit.com> wrote in message
news:e0**************@tk2msftngp13.phx.gbl...


"Nak" <a@a.com> wrote in message

news:OD**************@TK2MSFTNGP11.phx.gbl...
Hi there,

I have been wondering about something regarding strings in VB.NET. I have seen the following all over the place,

if(mystring = nothing) then do something

or

if(mystring = string.empty) then do something

I thought strings were a primative data type and were not referenced
data types. Meaning that a string cannot technically be nothing, it will just contain no data,

if(mystring = "") then do something

of

if(mystring.length = 0) then do something

Does this make more sence? I don't doubt that asking if the string is equal to string.empty is correct, but the main point is

string.empty = ""

Hmmmmmm, all of these are valid methods,but has anyone else got any
views on these? Id rather check against "", it's quicker to type!!!

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ "No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\



Nov 20 '05 #18
Nak
Hi Fergus,
You're working bloody hard, mate - good on you!! ;-))
Yeah, you know, ducking and diving, getting generally confused whilst
attempting to rush software development!
The VB.NET reference defines a 'primitive type' as one which gets its own keyword. That's pretty much the prime factor.
Aah, that is handy to remember
With checking S.Length, it'll fail if S is Nothing so you'd need to check that first. Nah!
Yeah, I had never come across this though, which is pretty lucky! Strange
how somethings miss you? No doubt if I started selling my app tommorrow I
would get thousands of bug reports due to this!! (Just kidding, well I hope
so anyway :-) )
This piece of code:
S = Nothing
If S = "" Then
MsgBox ("Empty")
Else
MsgBox ("Null")
End If
will give the "Empty" message not the "Null". So, within a condition,
Nothing is equal to "" - which is pretty handy, if you ask me. It's primitive, but it ain't stoopid. ;-)


See that is what initially confused me, because if "" is equal to nothing
then surely a string should be initialized with an empty string!!!! It
doesn't occupy any space in memory as a new instance is created each time
it's value changes, so why is a string a referenced type? Will you ever
need to use a string which has no reference? Check this one out,

Dim pop As String
MessageBox.Show(String.Compare(pop, ""))
MessageBox.Show(String.Compare(pop, Nothing))

Why does it return -1 for the first check even though pop does equal ""?
How weird. Anyway, I'm probably complicating things too much now. Cheers
everyone for their help, it is all *much* clearer now, even though it might
not seem it, I think :-\

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Nov 20 '05 #19
Nak
Hi there,
Because String is a primative type, the VB.NET compiler is "helping you out" by allowing the comparison of a string reference that is Nothing to be
compared as true with a string reference to a zero length string.


Ooooh, so this is a lump of blu tac on the compiler to make things a little
less awkward? Now that *does* make much more sence!! Thanks Jay! :-)

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Nov 20 '05 #20
Nak
Hi Rick,
Yep, i am definitely wrong. I am a .NET novice still. A great thread

though, ive learned a lot!

Yeah, but I'm the thick twat who asked it :-o

;-)

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Nov 20 '05 #21
Nak
MessageBox.Show(String.Concat(Nothing, Nothing).Length)

^ :-)

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Nov 20 '05 #22
Hi Nick,

|| String.Concat(Nothing, Nothing).Length
|| String.Concat("", Nothing).Length
|| String.Concat(Nothing, "").Length

I predict zero, now to check it.
[...]
Zero.

Because Concat will always return an initialised string. The first one
would actually call the Concat (Object, Object) overload.

|| Dim pop As String 'Nothing
|| String.Compare(pop, "") '-1 (Less than)
|| String.Compare(pop, Nothing) '0 (Same as)
|| String.Equals(pop, "") 'False
|| String.Equals(pop, Nothing) 'True

Because these functions don't use the blue tack!

And now I reckon you're sick enough of strings!! Spaghetti for dinner? -
No!! [turns green] I mean, no, thanks anyway - I'll just have the side salad
tonight.

No need to post a reply. I'll catch you later anyway ;-)

Regards,
Fergus
Nov 20 '05 #23
Nak,
In addition to Fergus's comments.

String.Concat is defined to replace Nothing with an Empty string
(String.Empty or ""), so it is the runtime itself doing this one.

I suspect to simplify calling String.Concat.

http://msdn.microsoft.com/library/de...ncatTopic4.asp

http://msdn.microsoft.com/library/de...ncatTopic5.asp

Hope this helps
Jay

"Nak" <a@a.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
MessageBox.Show(String.Concat(Nothing, Nothing).Length)

^ :-)

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ "No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\

Nov 20 '05 #24
"Nak" <a@a.com> schrieb
Strings are reference types, therefore they can be Nothing (not the
String itself, but the variable that can hold a reference). An
empty string means that there is a string but it's length is 0.
String.Empty is a zero length string. For me, the meaning of
Nothing is _only_ "no reference". As references are compared by the
Is operator, I never use the "=" operator with Nothing.


That answers it all, I read that strings weren't reference types. It
all makes much more sence now to me thinking that they are now. I
had wondered about this because I had seen different representations
written all over the place and was slightly confused, so what are the
simple data types? just the numerical ones? Thanks for your help
Armin.


I don't know what simple data types are. I only distinguish between
reference types and value types. Some are less, some are more complex. ;-)
--
Armin

Nov 20 '05 #25
Hi Nick,

The primitive types are identified through keywords, which are aliases for
predefined types in the System namespace. A primitive type is completely
indistinguishable from the type it aliases. A primitive type can be a
reference type or a value type. String is a reference type.

For more information, please refer to the following link:

http://msdn.microsoft.com/library/de...us/vbls7/html/
vblrfvbspec6_2.asp

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| From: "Nak" <a@a.com>
| Subject: A bit stringy
| Date: Fri, 17 Oct 2003 21:51:02 +0100
| Lines: 39
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <OD**************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.vb
| NNTP-Posting-Host: dsl213-218-228-203.as15444.net 213.218.228.203
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:147838
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| Hi there,
|
| I have been wondering about something regarding strings in VB.NET. I
| have seen the following all over the place,
|
| if(mystring = nothing) then do something
|
| or
|
| if(mystring = string.empty) then do something
|
| I thought strings were a primative data type and were not referenced
| data types. Meaning that a string cannot technically be nothing, it will
| just contain no data,
|
| if(mystring = "") then do something
|
| of
|
| if(mystring.length = 0) then do something
|
| Does this make more sence? I don't doubt that asking if the string is
| equal to string.empty is correct, but the main point is
|
| string.empty = ""
|
| Hmmmmmm, all of these are valid methods,but has anyone else got any
| views on these? Id rather check against "", it's quicker to type!!!
|
| Nick.
|
| --
|
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
| "No matter. Whatever the outcome, you are changed."
|
| Fergus - September 5th 2003
|
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
|
|
|

Nov 20 '05 #26
Hello All,

The series of "Dr. GUI .NET" has a special part on discussing strings in
.NET framework. I feel it explains it quite well and should be a good
reference in this topic.

"Strings in the .NET Framework"
http://msdn.microsoft.com/library/en...5_update.asp?f
rame=true

Does that answer your question, Nick? :)

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!From: "Nak" <a@a.com>
!Subject: A bit stringy
!Date: Fri, 17 Oct 2003 21:51:02 +0100
!Lines: 39
!X-Priority: 3
!X-MSMail-Priority: Normal
!X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!Message-ID: <OD**************@TK2MSFTNGP11.phx.gbl>
!Newsgroups: microsoft.public.dotnet.languages.vb
!NNTP-Posting-Host: dsl213-218-228-203.as15444.net 213.218.228.203
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:147838
!X-Tomcat-NG: microsoft.public.dotnet.languages.vb
!
!Hi there,
!
! I have been wondering about something regarding strings in VB.NET. I
!have seen the following all over the place,
!
! if(mystring = nothing) then do something
!
! or
!
! if(mystring = string.empty) then do something
!
! I thought strings were a primative data type and were not referenced
!data types. Meaning that a string cannot technically be nothing, it will
!just contain no data,
!
! if(mystring = "") then do something
!
! of
!
! if(mystring.length = 0) then do something
!
! Does this make more sence? I don't doubt that asking if the string is
!equal to string.empty is correct, but the main point is
!
! string.empty = ""
!
! Hmmmmmm, all of these are valid methods,but has anyone else got any
!views on these? Id rather check against "", it's quicker to type!!!
!
!Nick.
!
!--
!/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
\
!"No matter. Whatever the outcome, you are changed."
!
!Fergus - September 5th 2003
!/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
\
!
!
!

Nov 20 '05 #27
Hi Nick,

I'd like to know if this has been resolved? Is there anything that I can
help on this? I'm still monitoring on it.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| From: "Nak" <a@a.com>
| Subject: A bit stringy
| Date: Fri, 17 Oct 2003 21:51:02 +0100
| Lines: 39
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <OD**************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.vb
| NNTP-Posting-Host: dsl213-218-228-203.as15444.net 213.218.228.203
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:147838
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| Hi there,
|
| I have been wondering about something regarding strings in VB.NET. I
| have seen the following all over the place,
|
| if(mystring = nothing) then do something
|
| or
|
| if(mystring = string.empty) then do something
|
| I thought strings were a primative data type and were not referenced
| data types. Meaning that a string cannot technically be nothing, it will
| just contain no data,
|
| if(mystring = "") then do something
|
| of
|
| if(mystring.length = 0) then do something
|
| Does this make more sence? I don't doubt that asking if the string is
| equal to string.empty is correct, but the main point is
|
| string.empty = ""
|
| Hmmmmmm, all of these are valid methods,but has anyone else got any
| views on these? Id rather check against "", it's quicker to type!!!
|
| Nick.
|
| --
|
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
| "No matter. Whatever the outcome, you are changed."
|
| Fergus - September 5th 2003
|
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
|
|
|

Nov 20 '05 #28
Nak
Hi Kevin,
I'd like to know if this has been resolved? Is there anything that I can
help on this? I'm still monitoring on it.


Yup, tis all been resolved, I have had many responses explaining everything.
I couldn't reply to everyone because I would be saying the same thing over
and over, that is basically "thank you very much, I understand it a little
more now". Cheers for your help :-)

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Nov 20 '05 #29
ROFL

Beaten to death, I'd say.
Nov 20 '05 #30
Nak
> Beaten to death, I'd say.

LOL, like a dead badger being flogged with a pastrami??

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Nov 20 '05 #31
Hi Nick,

ROFL.

Now there's an image to warm the heart - and a tasty stew afterwards.;-)

Regards,
Fergus
Nov 20 '05 #32
dim s as string = nothing
if (s=string.Empty) then
do something
end if

s = string.Empty returns true

but in C#:
string s = null;
if (s == string.Empty)
dosomething();

s == string.Empty returns FALSE!!!!

What the hell is wrong with .Net?

-----Original Message-----
Hi there,

I have been wondering about something regarding strings in VB.NET. Ihave seen the following all over the place,

if(mystring = nothing) then do something

or

if(mystring = string.empty) then do something

I thought strings were a primative data type and were not referenceddata types. Meaning that a string cannot technically be nothing, it willjust contain no data,

if(mystring = "") then do something

of

if(mystring.length = 0) then do something

Does this make more sence? I don't doubt that asking if the string isequal to string.empty is correct, but the main point is

string.empty = ""

Hmmmmmm, all of these are valid methods,but has anyone else got anyviews on these? Id rather check against "", it's quicker to type!!!
Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ \/\/\/\/\/\/\/\/\/\"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ \/\/\/\/\/\/\/\/\/\

.

Nov 20 '05 #33
On 2003-11-06, Calvin <sz*****@hotmail.com> wrote:
dim s as string = nothing
if (s=string.Empty) then
do something
end if

s = string.Empty returns true

but in C#:
string s = null;
if (s == string.Empty)
dosomething();

s == string.Empty returns FALSE!!!!

What the hell is wrong with .Net?


There's Nothing wrong with .NET. VB.NET and C# are different languages
and the compilers emmit different code. It just happens that VB.NET
treats a null string reference equal to "". I actually don't
particularly care for this behavior, but I think it is an attempt at
compatability with VB6 code. It essentially behaved in a similar
manner.

Dim s As String
Dim t As String

t = ""

MsgBox StrPtr(s) ' shows zero, because it is a null reference
MsgBox StrPtr(t) ' shows non-zero, because it points to something
If s = t Then
MsgBox "Hi"
End If

HTH
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #34

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

Similar topics

7
by: John Swan | last post by:
I am starting to really hate managed C++. Ok. I am trying to add a newline character to a std::string and it won't do it. Whenever you put in << endl or + endl it wont compile because...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.