?
Solved

What is the best way to Set or Get an empty string?

Posted on 2011-04-25
8
Medium Priority
?
169 Views
Last Modified: 2012-05-11
I have the following (relatively) straightforward property:

   Private Property _firstNm As String
    Public Property FirstNm As String
        Get
            Return _firstNm
        End Get
        Set(ByVal value As String)
            If _firstNm <> value Then
                If ObjectState = AssignedClaims.ObjectState.Unchanged Then
                    ObjectState = AssignedClaims.ObjectState.Changed
                End If
                _firstNm = value
                Notify("FirstNm")
            End If
        End Set
    End Property

This property is causing me problems because if the setter is entered with a value of String.Empty, the test of _firstNm (which is nothing) = value (which is string.empty) returns true and I end up not entering my IF statement and bad things happen.

Is there a standard way of handling this situation? Should I change my setter (to use IsNothing(_firstNm) or change my getter?

0
Comment
Question by:AIBMass
8 Comments
 
LVL 25

Expert Comment

by:Ron Malmstead
ID: 35461786
I would either prevent it from being string.empty, or detect that it is.

 If _firstNm <> value AND Value <> "" then
              If ObjectState = AssignedClaims.ObjectState.Unchanged Then
                    ObjectState = AssignedClaims.ObjectState.Changed
                End If
                _firstNm = value
                Notify("FirstNm")
ELSE
'do something else.
            End If
0
 

Assisted Solution

by:AIBMass
AIBMass earned 0 total points
ID: 35461804
Actually I want string.Empty to be treated as an acceptable value. I want to enter my IF.

I am looking for a better or more standard way other than:

Set
   If IsNothing(_firstNm) or (_firstNm <> value)
       do the usual stuff.

Is this typical coding practice?
0
 
LVL 25

Expert Comment

by:Ron Malmstead
ID: 35461869
Seems you need to test for both conditions..
This is where you would use IF....then use an ELSEIF  and match both possible conditions.

so...

'Value is not string.empty and _firstNm is not equal to value.
If _firstNm <> value AND Value <> "" then

'value is string empty and _firstNm is not equal to value.
ELSEIF  _firstNm <> value AND Value = "" then


end if


Is it typical ??
...depends on who you ask.  lol... It's typical to test for each condition that needs to be handled differently.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 500 total points
ID: 35461878
Hi, and this works for you scenario?

    Private Property _firstNm As String = String.Empty
    Public Property FirstNm As String
        Get
            Return _firstNm
        End Get
        Set(ByVal value As String)
           If value = Nothing Then
                value = String.Empty
            End If


            If _firstNm <> value Then
                If ObjectState = AssignedClaims.ObjectState.Unchanged Then
                    ObjectState = AssignedClaims.ObjectState.Changed
                End If
                _firstNm = value
                Notify("FirstNm")
            End If
        End Set
    End Property
0
 

Author Comment

by:AIBMass
ID: 35461926
Uh oh. We are on the verge of miscommunicating.

Inside my setter, I originally wrote:

If _firstNm <> value
   do stuff

which looks quite sensible and concise. Then I realized that this was not working the way I wanted when value was string.empty.

So I changed my setter to:

If IsNothing(_firstNm) or (_firstNm <> value)
  do stuff

whcih works just fine. But it looks ugly to my eye and before I go and change all the setters in my string properties, I am looking for some experience about whether there is a better coding practice.
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 35462009
First thing I'd do is change the object state setter to happen in that Notify method. That will clean things up a fair bit.

Other than that, there's a slightly better way in newer versions of .NET.

        If String.IsNullOrEmpty(_firstNm) OrElse _firstNm <> value Then
0
 
LVL 25

Assisted Solution

by:Ron Malmstead
Ron Malmstead earned 500 total points
ID: 35462014
"" But it looks ugly ""


.... How so ?

If it accomplishes the logic of the application, then I wouldn't go looking for alternatives just for the sake of doing it a different way.
0
 

Author Closing Comment

by:AIBMass
ID: 35499793
It appears that there is no standard way of handling this. Therefore it's largely a matter of taste and I have awarded points to multiple solutions.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question