Results 1 to 4 of 4

Thread: How to perform property conversions?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    How to perform property conversions?

    Ok, in my business tier I have a class called Car, it has a
    property of type double

    Now this is a potential null field in the database

    in my car class I have a method called load which calls the dataaccess tier and returns a datareader of the carinfo...

    SOmehow a string must be passed to the presentation layer in the form of a double.
    Because when the user access the carpage (with car id)

    I do like this

    dim mycar as new car(id)

    txtname.text = mycar.name
    txtprice.text = mycar.price
    .
    .
    .

    if I encounter a dbnull here it is very bad. But where should I define the logic that says

    "If the price property of the car object is dbnull, then it should say "not available" in the presentation layer"?

    And it feels bad to define the price property as string, although it would solve my problems...

    kind regards
    Henrik

  2. #2
    Addicted Member
    Join Date
    Dec 2002
    Posts
    175
    Can you not allow NULL in the first place?
    Otherwise set the string in the presentation layer.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Well, what do you mean? I can see two problems with your suggestion:

    1)In the requirement spec our customer has clearly specified the presentation layer and what fields she want and what it should say if there is a value (and if there isn't a value) In this case the customer want a N/A string in the textbox.


    2)How should I handle the property which has type double if I encounter a dbnull?? I can't set it to 0.00 because that is incorrect and defy the business rules. And I can't do like this


    my method in the car class :

    VB Code:
    1. 'Called by the car constructor, hence it's private
    2.  
    3. private sub Load(byval carid as int)
    4.  
    5. dim dr as new sqldatareader
    6.  
    7. dr = datatier.getcarbyid(carid)
    8.  
    9. while not dr.read
    10. m_carid = carid
    11. m_carname = dr.items("name").ToString
    12. m_dblPrice = cdbl(dr.items("price"))
    13.  
    14. loop
    15.  
    16.  
    17. 'etc etc etc

    As you can see in the code, I will get an exception if I try to

    m_dblPrice = cdbl(dr.items("price"))

    set the property price to a dbnull value (from the items collection in the dr)


    How can I solve this problem using proper 3 tier architecture? Is there a pattern for this?

    kind regards
    Henrik

    ps Apologize if my explanation of the problem was vague at first... ds

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Well, I have had the same problem myself.

    What I did, was look at what the strongly typed dataset code was doing to solve this problem. It really is unimaginative. This is based off a three column table. ChildID can't be null as it is the primary key, but the other two can be null. Basically they add in two methods that can be used to set null, and check for null.

    (C# code, but you can get the point)
    Code:
                public int ChildID {
                    get {
                        return ((int)(this[this.tableChild.ChildIDColumn]));
                    }
                    set {
                        this[this.tableChild.ChildIDColumn] = value;
                    }
                }
                
                public string ChildName {
                    get {
                        try {
                            return
    ((string)(this[this.tableChild.ChildNameColumn]));
                        }
                        catch (System.InvalidCastException e) {
                            throw new
    System.Data.StrongTypingException("StrongTyping_CannotAccessDBNull", e);
                        }
                    }
                    set {
                        this[this.tableChild.ChildNameColumn] = value;
                    }
                }
                
                public int ParentID {
                    get {
                        try {
                            return
    ((int)(this[this.tableChild.ParentIDColumn]));
                        }
                        catch (System.InvalidCastException e) {
                            throw new
    System.Data.StrongTypingException("StrongTyping_CannotAccessDBNull", e);
                        }
                    }
                    set {
                        this[this.tableChild.ParentIDColumn] = value;
                    }
                }
                
                public bool IsChildNameNull() {
                    return this.IsNull(this.tableChild.ChildNameColumn);
                }
                
                public void SetChildNameNull() {
                    this[this.tableChild.ChildNameColumn] = System.Convert.DBNull;
                }
                
                public bool IsParentIDNull() {
                    return this.IsNull(this.tableChild.ParentIDColumn);
                }
                
                public void SetParentIDNull() {
                    this[this.tableChild.ParentIDColumn] = System.Convert.DBNull;
                }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width