Re: Override the property
- From: "Family Tree Mike" <FTM@xxxxxxxxxxxxxxxx>
- Date: Sun, 14 Dec 2008 18:26:23 -0500
"tshad" <tfs@xxxxxxxxxxxxxx> wrote in message news:erbqCMhXJHA.4596@xxxxxxxxxxxxxxxxxxxxxxx
I have a class that I have that tracks when an object has changed or is set to null.
I use it for data I get from my database to tell me if I need to update or reset the data to nulls. I am making changes to it and want to be able to do the same thing that you can do with nullable types.
If you have int? and you want to change the value you can do:
int? something;
something.value = x;
or
something = x;
You don't have to specify the property.
In my code I do
something.Data = x.
Where the property is set as:
public object Data
{
get { return _objCurrent; }
set
{
if (value == DBNull.Value) value = null;
if (value != null)
{
_ValidateType(value);
}
_objCurrent = value;
_fChanged = true;
}
}
This handles the data or nulls.
How can I change this to allow me to call set the variable by either calling the property or not calling it?
The code itself (with some removed as it is pretty large) is:
**************************************************
using System;
using System.IO;
namespace FtsData
{
abstract class DataType
{
protected object _objInitial;
protected object _objCurrent;
private bool _fChanged;
public bool IsNull
{
get { return _objCurrent == null; }
}
public bool IsFirstNull
{
get { return _objInitial == null; }
}
// Reset _objInitial to _objCurrent and changed flag to false to track
// when this variable changes again. This would be necessary if were to
// write out data to a database record and need to track when it changes again
public void Reset()
{
_objInitial = _objCurrent;
_fChanged = false;
}
public object First
{
get { return _objInitial; }
}
public object Data
{
get { return _objCurrent; }
set
{
if (value == DBNull.Value) value = null;
if (value != null)
{
_ValidateType(value);
}
_objCurrent = value;
_fChanged = true;
}
}
// Likewise, I don't think Changed should include a setter
public bool Changed
{
get { return _fChanged; }
}
// This is what deriving classes will define so the type can be checked
protected abstract Type _TypeRequired { get; }
private void _ValidateType(object obj)
{
Type typeRequired = _TypeRequired;
// Depending on how you're using this class, you may instead prefer
// to check for exact type equality. The below simply requires that
// the passed-in object has the required type in its inheritance chain.
if(obj == DBNull.Value)
{
_fChanged = _fChanged;
}
if (!typeRequired.IsInstanceOfType(obj))
{
throw new ArgumentException("assigned value type of " +
obj.GetType().Name + " is incompatible with required type of " +
typeRequired.Name);
}
}
}
class BoolType : DataType
{
public BoolType()
{
}
public BoolType(bool initial)
{
Type type = Type.GetType("initial");
Console.WriteLine("Type = {0}",type);
_objInitial= initial;
_objCurrent = initial;
}
// Each class defines this so that the base type can validate the data's type
protected override Type _TypeRequired
{
get { return typeof(bool); }
}
// A convenience method so that no casting is needed when you already have
// a fully typed object
public bool TypedData
{
get { return (bool)Data; }
set { Data = value; }
}
}
class StringType : DataType
{
public StringType()
{
}
public StringType(string initial)
{
Type type = initial.GetType();
Console.WriteLine("Type = {0}",type);
_objInitial= initial;
_objCurrent = initial;
}
protected override Type _TypeRequired
{
get { return typeof(string); }
}
public string TypedData
{
get { return (string)Data; }
set { Data = value; }
}
}
}
**************************************************
Thanks,
Tom
It sounds like you want to indicate that "Data" is the default property for your class. This is not available to the best of my knowledge in C# (nor VB either).
--
Mike
.
- Follow-Ups:
- Re: Override the property
- From: tshad
- Re: Override the property
- References:
- Override the property
- From: tshad
- Override the property
- Prev by Date: Re: Partial file hashing
- Next by Date: Re: Base Class constructor problem
- Previous by thread: Re: Override the property
- Next by thread: Re: Override the property
- Index(es):
Relevant Pages
|