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

set a nullable property in object

I have a business object (class) with some nullable properties declared like
this:

Private _MyProp as Nullable(of Int16)
public Property MyProp as Nullable(Of Int16)
get
Return _MyProp
end get
set(ByVal Value as Nullable(Of Int16))
If not _MyProp.Equals(value) then
_MyProp = Value
NotifyPropertyChanged("MyProp") ' marks the property changed
end if
end set

I can read from my database with no problem for both MyProp's with or
without a value. When MyProp = 1 and I try to set it to Null in a Windows
TextBox, the form will not even let me move to another control. If I change
rows in a datagridview, the value is set back to 0.

So, I added an OnValidate event like this:

if String.IsNullOrEmpty(myControl.Text) then _
myBusinessObject.MyProperty = Nothing

Now I can set the nullable property to "Nothing" and it works as expected.
My question is - Is this the normal procedure to set a nullable value or am
I missing some shortcut where I don't have to add the OnValidate event?

Rick
May 18 '07 #1
2 3792
Rick,

I would imagine that the code is attempting to set the Nullable value to ""
when there is nothing in the textbox, which is not null. I don't see any
harm in overriding the OnValidate event with the code you have, to perform
the translation from Empty to Null.

Hope this helps.

Steve

"Rick" <Ri************@LakeValleySeed.comwrote in message
news:Oz**************@TK2MSFTNGP05.phx.gbl...
>I have a business object (class) with some nullable properties declared
like this:

Private _MyProp as Nullable(of Int16)
public Property MyProp as Nullable(Of Int16)
get
Return _MyProp
end get
set(ByVal Value as Nullable(Of Int16))
If not _MyProp.Equals(value) then
_MyProp = Value
NotifyPropertyChanged("MyProp") ' marks the property changed
end if
end set

I can read from my database with no problem for both MyProp's with or
without a value. When MyProp = 1 and I try to set it to Null in a Windows
TextBox, the form will not even let me move to another control. If I
change rows in a datagridview, the value is set back to 0.

So, I added an OnValidate event like this:

if String.IsNullOrEmpty(myControl.Text) then _
myBusinessObject.MyProperty = Nothing

Now I can set the nullable property to "Nothing" and it works as expected.
My question is - Is this the normal procedure to set a nullable value or
am I missing some shortcut where I don't have to add the OnValidate event?

Rick

Jun 4 '07 #2
Rick schreef:
Now I can set the nullable property to "Nothing" and it works as expected.
My question is - Is this the normal procedure to set a nullable value or am
I missing some shortcut where I don't have to add the OnValidate event?
I prefer to do it at Parsing time...

this.myDataSource = new MyDataSource();
this.textBox1.DataBindings.Add("Text", this.myDataSource, "Double",
true);
this.textBox1.DataBindings["Text"].Parse += this.Text_Parse;

void Text_Parse( object sender, ConvertEventArgs e )
{
if( e.Value == null || e.Value.ToString().Length == 0 )
{
e.Value = null;
}
}
--
Tim Van Wassenhove - Read my mind <url:http://www.timvw.be/>
Jun 4 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Mark Rae | last post by:
Hi, I posted a couple of days ago about the possibility of "simulating" in ..NET1.1 the nullable datatypes available in .NET2.0 - I'm nearly there, but require a bit more guidance. Basically,...
30
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
8
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked...
3
by: Nemisis | last post by:
Hi everyone, i have been stressing over this for the past hour or so, and just cannot figure it out, sorry if i am being stupid, but it is a friday! lol I have the following Private _ID as...
3
by: Goofy | last post by:
The 2.0 Framework has a new type called System.Nullable. This allows something to Not exist, basically you can pass 'Nothing' to a method whose parameters as System.Nullable. Im looking at table...
15
by: scparker | last post by:
I have yet to find a satisfactory solution to this problem. It involves VB.NET 2.0 and datetime issues. I have a form that asks for a Date to be submitted in dd/mm/yyyy format. When this is...
5
by: =?Utf-8?B?emlubw==?= | last post by:
in .net 2.0, the class (myClass) contains a property defined as: private _creationDate as As Nullable(Of DateTime) Public Property CreationDate() As Nullable(Of DateTime) Get If...
2
by: Benton | last post by:
Hi there, I'm creating a custom server control, inheriting from TextBox. It has this AsDateTime property that returns the textbox contents converted to the nullable DateTime data type, as...
4
by: Aamir Mahmood | last post by:
Hi I am unable to access the HasValue and Value properties in an Nullable object. int? i = 9; object value = i; if ((value as Nullable).HasValue) { ... }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.