Re: Abstract class variables question
- From: "tshad" <tfs@xxxxxxxxxxxxxx>
- Date: Wed, 14 Nov 2007 22:16:31 -0800
"Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote in message
news:2007111400001816807-NpOeStPeAdM@xxxxxxxxxxxxxxxxxx
On 2007-11-13 22:42:29 -0800, "tshad" <tfs@xxxxxxxxxxxxxx> said:
Also, you'll need "override" with the declarations of that property in the
concrete types.
That fixed it.
I made some changes and agree that I don't need SetNull methods or the
setters for some of the Properties.
I did need to add a method Reset() to reset the _objInitial variable to be
whatever the _objCurrent is at the time as well as change the _fChange to
false.
This is to handle the situations, such as Database updates or inserts, where
we still want to use the same screen but have already updated the data. At
that point, I don't care what the original data was, since the record(s)
have been updated and I now want the current data to match the original data
so I can tell if the data changed so that I can update the records again if
necessary.
I did find that the methods:
// 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; }
}
doesn't seem to work.
Actually, it compiles fine. But the problem is that the compiler won't let
me do the following in my code that calls these objects:
bool test;
BoolType booltemp = new BoolType(false);
booltemp.Data = true;
test = booltemp.Data;
test = booltemp.First;
I get an error on the last 2 lines.
Cannot implicitly convert type 'object' to 'bool'
I assume the method is supposed to handle that - but I don't think the
compiler sees the method.
Am I missing something?
Here is the code I am using at the moment:
**************************************
C:\VSProjects\DBTypesCSharp2\Form1.cs(93): 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 != 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
abstract protected 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 (!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)
{
_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(bool initial)
{
_objInitial= initial;
_objCurrent = initial;
}
protected override Type _TypeRequired
{
get { return typeof(string); }
}
public string TypedData
{
get { return (string)Data; }
set { Data = value; }
}
}
}
**************************************
Thanks,
Tom
.
- Follow-Ups:
- Re: Abstract class variables question
- From: Peter Duniho
- Re: Abstract class variables question
- References:
- Abstract class variables question
- From: tshad
- Re: Abstract class variables question
- From: Peter Duniho
- Re: Abstract class variables question
- From: tshad
- Re: Abstract class variables question
- From: Peter Duniho
- Abstract class variables question
- Prev by Date: Re: generic interface conversion question
- Next by Date: how to open one form from the other form and close the first form
- Previous by thread: Re: Abstract class variables question
- Next by thread: Re: Abstract class variables question
- Index(es):
Relevant Pages
|