UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Default Property To "" Instead Of Null    
 
   
jleach
post May 30 2012, 08:56 PM
Post#1


UtterAccess Editor
Posts: 10,225
Joined: 7-December 09
From: St Augustine, FL


Hi,
I'm working in C# and wondering if there's some trick to default a public property using the automatic property generation to be a ZLS rather than Null?
Example:
CODE
public string MyProp { get; private set; }

This is the extent of what I actually require, no designated field needed on my behalf. I understand I can use the constructor to init this, more or less just curious if there's any trick for a lazy person such as myself...
Thanks!
Go to the top of the page
 
datAdrenaline
post May 30 2012, 11:45 PM
Post#2


UtterAccess Editor
Posts: 18,006
Joined: 4-December 03
From: Northern Virginia, USA


Well ... I don't set that in the protype, but I do coerce with the ?? operator in use ...
!--c1-->
CODE
If (someClass.MyStringProp ?? "").Length == 0
Go to the top of the page
 
jleach
post May 31 2012, 04:32 AM
Post#3


UtterAccess Editor
Posts: 10,225
Joined: 7-December 09
From: St Augustine, FL


Handy, I didn't know about that. In this case I'd opt for using the constructor to default the values though.
Thanks Brent
Go to the top of the page
 
datAdrenaline
post May 31 2012, 09:37 AM
Post#4


UtterAccess Editor
Posts: 18,006
Joined: 4-December 03
From: Northern Virginia, USA


>> I'd opt for using the constructor to default the values though. <<
One thing that is important to know, which you probably already do know, is that string typed variables initialize to null, so changing (or rather masking) that behavior does require intervention (ie: through the constructor or an expanded property with matching field, etc.).
It would be nice if C# allowed you to declare an non-nullable string, sorta like a nullable int ...
Nullable int ..
int? x
a fictitious non-nullable string ..
?string str
...
Osuppose folks more skilled than I with overloading could do such a thing, but that is beyond me at this point. <
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    20th November 2019 - 04:49 AM