Set strings in criteria class to 'String.Empty'?

Set strings in criteria class to 'String.Empty'?

Old forum URL: forums.lhotka.net/forums/t/4259.aspx


reagan123 posted on Wednesday, January 30, 2008

Hello again,
I've seen that you should initialize all of your strings in your business objects like the following

dim _myString as String = String.Empty

Do I need to do the same thing to strings that exist in my criteria class as well?

Thanks!

JohnB replied on Wednesday, January 30, 2008

Regan,

In your example, _myString would be "Nothing" without String.Empty. Strings act like reference types. So yes you want to initialize all strings with String.Empty where the object is marked as serializable.

Hope this help.

John

ajj3085 replied on Wednesday, January 30, 2008

Serialization doesn't require you to have non-null (Nothing) references before serializing.

I would suggest that it depends on who is consuming the criteria classes.  They're usually private to the class in which they are used, so as long as the code getting values out of the criteria class can handle Nothing, there's no need to initialize.   If the code would be simply by not ever having Nothings, then code it that way.  I think its more of a personal preference myself.

For publicly visible types which I will be using in databinding, I like to set strings to Empty.

JohnB replied on Wednesday, January 30, 2008

ajj3085:
Serialization doesn't require you to have non-null (Nothing) references before serializing.


I agree with your statement as well but I thought I remembered reading somewhere in the Csla book that suggested initializing strings to String.Empty?

ajj3085 replied on Wednesday, January 30, 2008

It might, do you remember where you saw it?  For criteria though, which are usually private classes, it would likely not matter as long as your code handles that fine.  For BOs though, I personally think its a good idea for public string properties to be set to Empty, especially if you're databinding, but I don't recall one way or the other what the book said, if anything.

JohnB replied on Wednesday, January 30, 2008

I just did a quick search through the books and I did not see anything just yet. I'll continue to look as I am curious if it at all matters. But back to your point, I always initialize to String.Empty as a standard.

simon_may replied on Wednesday, January 30, 2008

I believe off the top of my head, that it is a requirement fot successful databinding, which does not like binding to nulls.

reagan123 replied on Wednesday, January 30, 2008

Thanks for the reply guys...

I did find and example of this in the criteria class in the book on pg. 78 or pg. 249 and Rocky does not initialize the string in the Criteria class.  I'm assuming this is the okay way to do it.

Thanks so much for the feedback.

RockfordLhotka replied on Friday, February 01, 2008

The reason for initializing a string to string.Empty or "" is for data binding. Data binding doesn't like null values, and the default value for a string field is null/Nothing.

Most criteria objects are not data bound, so it doesn't matter if you initialize string fields.

That said, sometimes people do bind their criteria objects (to allow the user to specify options), in which case the fields would need to be initialized.

Copyright (c) Marimer LLC