Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting up dynamic class to change properties

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
I have my own data types for use with a database that have various properties to manipulate values such as:

First: original value
IsNull: is the current state null (no value in Data)
IsFirstNull: was the initial state of the object null
Changed: has the value changed since the initial value was set.
SetNull(): set the object to null
SetFirstNull: set the initial value to null
Reset: set values all to original settings.

Each one of the objects has these. There is an object for each type of standard variables, such as:

int - IntType
string - StringType
bool - BoolType

I set up these types to handle the different states of each variable I read from a table as well as handle nulls. It was always hard to tell whether a value was null or not or whether it was null when the value was originally read so I would know how to write it back or whether the value was changed. Sometimes you need to know what the original value was when you read the data from the table.

So I only use these for data I have read from my tables, this is so I can keep track of values, including nulls, that I got originally and track changes as well as reset to original values.

I want to be able to access the data via properties, so I have it set up like:
Code:
public class LoginDTO
{
    public IntType loginID = new IntType();
    public StringType userName = new StringType();

    #region Properties

    public object LoginID
    {
        get
        {
            object value = null;
            return NullHandler.SetDbObjectValueFromValue(loginID, ref value);
        }
        set
        {
            NullHandler.GetValueFromDbObject(value, ref loginID);
        }
    }

    public object UserName
    {
        get
        {
            object value = null;
            return NullHandler.SetDbObjectValueFromValue(userName, ref value);
        }
        set
        {
            NullHandler.GetValueFromDbObject(value, ref userName);
        }
   }

To access the data, I could do something like

loginDto.UserName


But to access (set or get) the IsNull property, I can't really use a property for each variable.

I could set up a method:

SetNull()

but not sure how I would access the variable to change the IsNull property of each variable.

How would I pass a literal ( i.e. SetNull("loginID") or SetNull("userName")) such that the method would know which variable to set?

Or is there a better way to do this?

Thanks,

Tom
 
Not sure I fully understand but couldnt you just do a GetType on the variable passed in to the property then have a separate function within for each type - this is vb code but you get the gist
eg:
Select Case VariableName.GetType
Case GetType(Integer)
SetIntegerValue(VariableName)
Case GetType(String)
SetStringValue(VariableName)
Case GetType(Boolean)
SetBooleanValue(VariableName)
etc...
End Select
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top