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

How is String.Empty implemented?

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
9 2351
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

"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
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
"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

"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
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
"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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Derek | last post by:
I'm curious about the performance of string::c_str, so I'm wondering how it's commonly implemented. Do most std::string implementations just keep an extra char allocated for the NULL termination...
25
by: Matthias | last post by:
Hi, I am just reading that book by Scott Meyers. In Item 4 Meyers suggests to always use empty() instead of size() when probing for emptyness of STL containers. His reasoning is that size()...
5
by: SpotNet | last post by:
Hello NewsGroup, I have a custom class and a collection for that custom class that inherits CollectionBase. As such; public class MyClass { private string datamember1 = string.Empty,...
20
by: Guadala Harry | last post by:
In an ASCX, I have a Literal control into which I inject a at runtime. litInjectedContent.Text = dataClass.GetHTMLSnippetFromDB(someID); This works great as long as the contains just...
23
by: Rogers | last post by:
I want to compare strings of numbers that have a circular boundary condition. This means that the string is arranged in a loop without an end-of-string. The comparaison of two strings now...
6
by: Charlie | last post by:
Hi: Is there any difference between string.Empty and String.Empty? And what is the benefit of using it over "". Thanks, Charlie
26
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I...
26
by: anonieko | last post by:
In the past I always used "" everywhere for empty string in my code without a problem. Now, do you think I should use String.Empty instead of "" (at all times) ? Let me know your thoughts.
22
by: j1mb0jay | last post by:
I have had to create a simple string encryption program for coursework, I have completed the task and now have to do a write up on how it could be improved at a later date. If you could look...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.