set a nullable property in object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rick

    #1

    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
    NotifyPropertyC hanged("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.IsNullOr Empty(myControl .Text) then _
    myBusinessObjec t.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


  • PlatinumBay

    #2
    Re: set a nullable property in object

    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" <RickREMOVETHIS @LakeValleySeed .comwrote in message
    news:OzojkWUmHH A.4592@TK2MSFTN GP05.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
    NotifyPropertyC hanged("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.IsNullOr Empty(myControl .Text) then _
    myBusinessObjec t.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
    >

    Comment

    • Tim Van Wassenhove

      #3
      Re: set a nullable property in object

      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.myDataSour ce = new MyDataSource();
      this.textBox1.D ataBindings.Add ("Text", this.myDataSour ce, "Double",
      true);
      this.textBox1.D ataBindings["Text"].Parse += this.Text_Parse ;

      void Text_Parse( object sender, ConvertEventArg s e )
      {
      if( e.Value == null || e.Value.ToStrin g().Length == 0 )
      {
      e.Value = null;
      }
      }


      --
      Tim Van Wassenhove - Read my mind <url:http://www.timvw.be/>

      Comment

      Working...