dealing with nulls

Posts   
 
    
Asimov
User
Posts: 113
Joined: 05-Dec-2003
# Posted on: 09-Sep-2004 20:31:29   

Hi!

I was wondering if any of you has had a brilliant idea to deal with null values on fields of numeric type (int, bigint, float, etc on sql server). Here's my situation:

when I want to set the Text property of a textbox with a value from a numerical database field, I first check if the field was originally null (TestOriginalFieldValueForNull) If it's null then I put an empty string in the field (otherwise a "0" appears when I convert to llbl property (which is an int, double ,etc) to a string. If it's not null, I simply convert the property value to a string and put it in the textbox.

when I read the value back in the entity, I must check if the input value is an empty string (for instance if the user cleared the field). In this case, I must set the isNull property of the field to true, otherwise I set it to false (because if I keep the entity in memory and try to TestOriginalFieldValueForNull again then it must return false..) and assign the textbox value to the llbl property.

It makes a lot of "dirty" and useless code in my app. So I thought about encapsulating all this code directly into the entity classes by redefining each property that links to a numerical database field that can be read from a textbox in my application. So If I have a property called Salary (that is a double) in entity Employee, I redefine in EmployeeEntity:


public new string Salary
        {
            get {if(base.TestOriginalFieldValueForNull(EmployeeFieldIndex.Salary)) return ""; else return base.Salary.ToString();}
            set {if(value != "") base.Salary = Convert.ToDouble(value); this.Fields["Salary"].IsNull = false; else this.Fields["Salary"].IsNull = true;}
        }

So assuming a valid double is typed in a textbox, I could use this thing by simply doing:


employee.Salary = txtSalary.Text;

I wanted to know if someone developped a better/simpler way to do it or if I missed something that already does that in llbl.

Thank you all for taking the time to read this simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 09-Sep-2004 21:02:04   

How about this:


public static string ValueTypeToString(IEntityField field)
{
    string toReturn = string.Empty;
    
    if(!field.IsNull)
    {
        toReturn = field.CurrentValue.ToString();
    }
    
    return toReturn;
}

store this in a generalutils class:

use it like:


public new string Salary
{
    get 
    {
        return GeneralUtils.ValueTypeToString(Fields[(int)EmployeeFieldIndex.Salary]);
    }
    set {if(value != "") base.Salary = Convert.ToDouble(value); this.Fields["Salary"].IsNull = false; else this.Fields["Salary"].IsNull = true;}
}

For the setter you can write a similar utility function. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Asimov
User
Posts: 113
Joined: 05-Dec-2003
# Posted on: 09-Sep-2004 21:16:30   

good idea simple_smile but I still need to create a new property for each property that has a numerical value that is read from a textbox. Is there something I can do in the designer to somehow auto generate this? (purely a guess simple_smile )

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 09-Sep-2004 21:52:01   

Asimov wrote:

good idea simple_smile but I still need to create a new property for each property that has a numerical value that is read from a textbox. Is there something I can do in the designer to somehow auto generate this? (purely a guess simple_smile )

No, the designer can't help you with this at this point...

You can make life easier by pushing each property through a function if you want to show the value in the gui. This way, you don't need to override each property, and you still get the behavior you want. After all, the empty strings is pure GUI related material.

So something like: myTextBox.Text = CreateTextBoxString(myEntity.Fields[(int)EntityFieldIndex.Foo]);

and in CreateTextBoxString you check if the type of the IEntityField is numeric, (grab the code for that from the TypeDefaultValue class stuck_out_tongue_winking_eye ) and if so, check for null and return the value accordingly.

Frans Bouma | Lead developer LLBLGen Pro
Asimov
User
Posts: 113
Joined: 05-Dec-2003
# Posted on: 09-Sep-2004 22:08:46   

Hmm this probably is the best option. And better yet, I can use the fonction only when I am affecting a field that needs special processing. Thanks for discussing those ideas with me simple_smile

MrWalle
User
Posts: 1
Joined: 16-Jul-2005
# Posted on: 16-Jul-2005 18:50:26   

Otis wrote:

How about this:


public static string ValueTypeToString(IEntityField field)
{
    string toReturn = string.Empty;
    
    if(!field.IsNull)
    {
        toReturn = field.CurrentValue.ToString();
    }
    
    return toReturn;
}

store this in a generalutils class:

use it like:


public new string Salary
{
    get 
    {
        return GeneralUtils.ValueTypeToString(Fields[(int)EmployeeFieldIndex.Salary]);
    }
    set {if(value != "") base.Salary = Convert.ToDouble(value); this.Fields["Salary"].IsNull = false; else this.Fields["Salary"].IsNull = true;}
}

For the setter you can write a similar utility function. simple_smile

Just wondering how a similar utility function for the setter might look like. My code looks something like this:


if myTextBox.text <> "" then
    myEntityObject.someColumn = CType(myTextBox.text, Double)
else
    myEntityObject.SetNewFieldValue(myEntityFieldIndex.someColumn, Nothing)
end if

If I had a utility function, I wouldn't have to write all this for every property on the entity object. simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 18-Jul-2005 11:12:12   

Well, calling SetNewFieldValue can be made generic, as it accepts an int and a value, you just have to make a couple of routines which know of which type the text in the textbox is, then call the generic SetNewFieldValue routine.

Frans Bouma | Lead developer LLBLGen Pro