By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,080 Members | 1,855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,080 IT Pros & Developers. It's quick & easy.

How is String.Empty implemented?

P: n/a
Hi

Can someone tell me how to implement something similar to String.Empty ??

Given a class called myValue

Public class myValue
private id as integer
private value as string
end class

how do I make it possible to write something like:

dim val as myValue
if val=myValue.Empty then
....
end if

where the expression evaluated equal to:

if val.id=-1 then
....
end if

TIA

Søren
Nov 21 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Søren

I am not where you are after however

mystring.empty is the same as all these bellow
mystring.lenght = 0
mystring = Nothing
mystring = ""

Where the last has been mostly adviced in this newsgroup

There is as well
Mystring Is Nothing

However that gives false in this situation
dim mystring as String = "" and only True when there never is a value
assigned to the string.

I hope this helps?

Cor

M. Olesen" <sm******@hotmail.com> schreef in bericht
news:O6**************@tk2msftngp13.phx.gbl...
Hi

Can someone tell me how to implement something similar to String.Empty ??

Given a class called myValue

Public class myValue
private id as integer
private value as string
end class

how do I make it possible to write something like:

dim val as myValue
if val=myValue.Empty then
...
end if

where the expression evaluated equal to:

if val.id=-1 then
...
end if

TIA

Søren

Nov 21 '05 #2

P: n/a

"Søren M. Olesen" <sm******@hotmail.com> wrote

Can someone tell me how to implement something similar to String.Empty ??

Given a class called myValue

Public class myValue
private id as integer
private value as string
end class

how do I make it possible to write something like:

dim val as myValue
if val=myValue.Empty then
...
end if

where the expression evaluated equal to:

if val.id=-1 then
...
end if

You would have to overload the = for your object, something
not possible in VS2003. What you can do is to provide the
Empty function:

Public Class myValue
Private id As Integer
Private value As String

Public Sub New()
id = -1
End Sub

Shared Function Empty(ByRef Test As myValue) As Boolean
Return (Test Is Nothing) OrElse (Test.id = -1)
End Function

End Class
Then to use it:

Dim var as MyValue
If MyValue.Empty(var) Then ....

HTH
LFS

Nov 21 '05 #3

P: n/a
Søren,
As Larry suggested, you would need to override the = operator, which is not
available till VS.NET 2005.

I normally implement "Empty" as a shared readonly field or property.

If my class needs to know if its "Empty" I will add an IsEmpty property, in
addition to the above Empty value.
Public class myValue
private id as integer
private value as string
Public Shared Readonly Empty As New myValue

Public ReadOnly Property IsEmpty() As Boolean
Get
Return id = 0 AndAlso value = String.Empty
End Get
End Property

end class
The Empty property is most useful when you have an immutable class or
structure. One where all the instance fields are readonly. Such as
EventArgs.Empty. You can then use the class where you need an object, but
that object has no meaningful value, for example when you raise a lot of
events, its better to pass EventArgs.Empty rather then creating a new
"empty" EventArgs object... (a lot of empty objects can hurt performance).

Hope this helps
Jay
"Søren M. Olesen" <sm******@hotmail.com> wrote in message
news:O6**************@tk2msftngp13.phx.gbl... Hi

Can someone tell me how to implement something similar to String.Empty ??

Given a class called myValue

Public class myValue
private id as integer
private value as string
end class

how do I make it possible to write something like:

dim val as myValue
if val=myValue.Empty then
...
end if

where the expression evaluated equal to:

if val.id=-1 then
...
end if

TIA

Søren

Nov 21 '05 #4

P: n/a
"Søren

Reading your message again I get the idea that you just are asking this.

\\\
Public Class MyTest
Public Shared Sub Main()
Dim myvalue As MyvalueClass
If myvalue.IsEmpty Then
MessageBox.Show("Hello I am not even filled")
End If
End Sub
End Class
Friend Class MyvalueClass
Private mystring As String
Public Function IsEmpty() As Boolean
If mystring.Length = 0 Then
Return True
Else
Return False
End If
End Function
End Class
///

This is the most simple way of course.

Cor
Nov 21 '05 #5

P: n/a

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote

I normally implement "Empty" as a shared readonly field or property.

Public Shared Readonly Empty As New myValue

The Empty property is most useful when you have an immutable class or
structure. One where all the instance fields are readonly. Such as
EventArgs.Empty. You can then use the class where you need an object, but
that object has no meaningful value, for example when you raise a lot of
events, its better to pass EventArgs.Empty rather then creating a new
"empty" EventArgs object... (a lot of empty objects can hurt performance).

How about just passing Nothing as either argument (Sender and/or EventArgs)

???
LFS
Nov 21 '05 #6

P: n/a
Larry,
The sender is the object raising the event, if the handler of the event ever
checked who the sender was, sending Nothing would cause an exception or
"worse" require the handler to check for nothing then check who the sender
was. Remember you have no "real" control over who may handle your event.

EventArgs.Empty "Represents an event with no event data".

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

I am suggesting using EventArgs.Empty instead of New EventArgs when using
RaiseEvent on an event you have defined, not calling a specific handler
directly in your code.

I don't see it explicitly stated in the Guidelines, however I would not send
nothing for EventArgs either, as I am not in control of the code that
handles that event, it may attempt to access the EventArgs, and should not
be required to check for Null. EventArgs.Empty is effectively an
implementation of a Null Object or Special Case Pattern.

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

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

http://www.martinfowler.com/eaaCatalog/specialCase.html

In other words if you always send valid values for both sender & eventargs,
then the event handlers can simply use the values if they want, they don't
need to be littered with code to check to see if either is Nothing.

Hope this helps
Jay

"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:eQ**************@TK2MSFTNGP09.phx.gbl...

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote

I normally implement "Empty" as a shared readonly field or property.

Public Shared Readonly Empty As New myValue

The Empty property is most useful when you have an immutable class or
structure. One where all the instance fields are readonly. Such as
EventArgs.Empty. You can then use the class where you need an object, but
that object has no meaningful value, for example when you raise a lot of
events, its better to pass EventArgs.Empty rather then creating a new
"empty" EventArgs object... (a lot of empty objects can hurt
performance).

How about just passing Nothing as either argument (Sender and/or
EventArgs)

???
LFS

Nov 21 '05 #7

P: n/a
"Søren M. Olesen" <sm******@hotmail.com> schrieb:
Can someone tell me how to implement something similar to
String.Empty ??


If you are interested in the implementation of 'String.Empty', take a look
here:

<URL:http://sharedsourcecli.sscli.net/source/browse/sharedsourcecli/clr/src/bcl/system/string.cs>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #8

P: n/a

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote

I am suggesting using EventArgs.Empty instead of New EventArgs when using
RaiseEvent on an event you have defined, not calling a specific handler
directly in your code.
OK....

I don't see it explicitly stated in the Guidelines, however I would not send
nothing for EventArgs either, as I am not in control of the code that
handles that event, it may attempt to access the EventArgs, and should not
be required to check for Null. EventArgs.Empty is effectively an
implementation of a Null Object or Special Case Pattern.
And I was thinking not being null would be an indicator that the data is
valid, but I have to admit, I was thinking in terms of a more limited scope,
where you are addressing the vast (general) Public....

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

That's an interesting example:

' Call event handler.
OnClick()
....

Protected Overridable Sub OnClick(e As ClickEvent)
If Not (onClickHandler Is Nothing) Then
onClickHandler(Me, e)
End If
End Sub
I have to wonder how they got it to compile!

<g>
LFS

Nov 21 '05 #9

P: n/a
Larry,
That's an interesting example:
' Call event handler.
OnClick()
....
Protected Overridable Sub OnClick(e As ClickEvent) Never noticed that in the sample.

I'll try and remember to report it later.

Jay

"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:ex*************@tk2msftngp13.phx.gbl...
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote

I am suggesting using EventArgs.Empty instead of New EventArgs when using
RaiseEvent on an event you have defined, not calling a specific handler
directly in your code.


OK....

I don't see it explicitly stated in the Guidelines, however I would not
send
nothing for EventArgs either, as I am not in control of the code that
handles that event, it may attempt to access the EventArgs, and should
not
be required to check for Null. EventArgs.Empty is effectively an
implementation of a Null Object or Special Case Pattern.


And I was thinking not being null would be an indicator that the data is
valid, but I have to admit, I was thinking in terms of a more limited
scope,
where you are addressing the vast (general) Public....

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

That's an interesting example:

' Call event handler.
OnClick()
....

Protected Overridable Sub OnClick(e As ClickEvent)
If Not (onClickHandler Is Nothing) Then
onClickHandler(Me, e)
End If
End Sub
I have to wonder how they got it to compile!

<g>
LFS

Nov 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.