Re: Abstract class variables question
- From: "Chris Mullins [MVP - C#]" <cmullins@xxxxxxxxx>
- Date: Tue, 13 Nov 2007 17:02:59 -0800
The obvious answer is to switch over to Visual Studio 2005, and .Net 2.0.
The newer version of .Net has deep support of Nullable types, for exactly
the reason you laid out.
http://blogs.msdn.com/ericgu/archive/2004/05/27/143221.aspx
There is qutie a bit of material on this available on the web.
If you can't use .Net 2.0, write back, and hopefully we can figure something
out...
--
Chris Mullins
"tshad" <tfs@xxxxxxxxxxxxxx> wrote in message
news:OgZpAflJIHA.4592@xxxxxxxxxxxxxxxxxxxxxxx
Using VS 2003, I am trying to take a class that I created to create new
variable types to handle nulls and track changes to standard variable
types. This is for use with database variables. This tells me if a
variable has changed, give me the original and current value, and whether
the current value and original value is/was null or not.
This one works fine but is recreating the same methods over and over for
each variable type.
What I wanted to do was create an Abstract class that did most of the work
and create a class for each variable type that only does what is needed.
I can't seem to get it to work correctly.
Part of it has to do with where to define my actual data variables. I
tried defining them only as objects in the abstract class but that won't
work because the class doesn't know what type of object it is.
The original file (with most variable types cut out is):
******************************************
using System;
using System.IO;
namespace FtsData
{
[Serializable]
public class StringType
{
private string first = ""; //original data
private string data = ""; //current data
private bool nullFirst = false; //original Data null
private bool nullData = false; //current data null
private bool changed = false;
public StringType()
{
}
public StringType(string initial)
{
first = initial;
data = initial;
}
public bool IsNull()
{
return nullData;
}
public bool IsFirstNull()
{
return nullFirst;
}
public void SetNull()
{
nullData = true;
data = "";
}
public void SetFirstNull()
{
nullFirst = true;
first = "";
}
// Properties
public string First
{
get { return first; }
set { first = value; }
}
public string Data
{
get { return data; }
set { data = value; nullData = false; changed = true;}
}
public bool Changed
{
get {return changed;}
set {changed = value;}
}
} // end Class
[Serializable]
public class BoolType
{
private bool first = false; //original data
private bool data = false; //current data
private bool nullFirst = false; //original Data null
private bool nullData = false; //current data null
private bool changed = false;
public BoolType()
{
}
public BoolType(bool initial)
{
first = initial;
data = initial;
changed = false;
}
public bool IsNull()
{
return nullData;
}
public bool IsFirstNull()
{
return nullFirst;
}
public void SetNull()
{
nullData = true;
data = false;
}
public void SetFirstNull()
{
nullFirst = true;
data = false;
}
// Properties
public bool First
{
get { return first; }
set { first = value; }
}
public bool Data
{
get { return data; }
set { data = value; nullData = false; changed = true;}
}
public bool Changed
{
get {return changed;}
set {changed = value;}
}
} // end Class
} // end Namespace
*******************************************
As you can see much if it is identical and can just be done as an object -
but some can't.
I tried this but did get some errors:
*******************************************
using System;
using System.IO;
namespace FtsData
{
public abstract class DataType
{
protected object _first;
protected object _data;
private bool nullFirst = false; //original Data null
private bool nullData = false; //current data null
private bool changed = false;
public bool IsNull()
{
return nullData;
}
public bool IsFirstNull()
{
return nullFirst;
}
public void SetNull()
{
nullData = true;
_data = "";
}
public void SetFirstNull()
{
nullFirst = true;
_first = "";
}
// Properties
public string First
{
get { return _first; }
set { _first = value; }
}
public string Data
{
get { return _data; }
set { _data = value; nullData = false; changed = true;}
}
public bool Changed
{
get {return changed;}
set {changed = value;}
}
}
[Serializable]
public class StringType : DataType
{
private string _first = ""; //original data
private string _data = ""; //current data
public StringType()
{
}
public StringType(string initial)
{
_first = initial;
_data = initial;
changed = false;
}
} // end Class
[Serializable]
public class BoolType : DataType
{
private bool _first = false; //original data
private bool _data = false; //current data
public BoolType()
{
}
public BoolType(bool initial)
{
_first = initial;
_data = initial;
changed = false;
}
} // end Class
} // end namespace
********************************************
I assume I need to create the variables in each class (StringType,
BoolType, IntegerType, DecimalType etc).
I also assume that anything that actually needs to explicitly change the
variables to some value, such as strings to "" or bool to false.
I assume I can't access these variables from my Abstract Class
definitions. This is why I was trying to create them as objects, but then
they are separate variables and I would like to do this only one time.
But I can't seem to figure out how to make that work. So the following
may also have to go each class.
public void SetNull()
{
nullData = true;
_data = "";
}
It's possible I may only be able to handle these from the abstract
classes:
private bool nullFirst = false; //original Data null
private bool nullData = false; //current data null
private bool changed = false;
But I am just trying to find out if I am overlooking something. I am
trying to get this to simplest set of class that I can.
Thanks,
Tom
.
- Follow-Ups:
- Re: Abstract class variables question
- From: tshad
- Re: Abstract class variables question
- References:
- Abstract class variables question
- From: tshad
- Abstract class variables question
- Prev by Date: Abstract class variables question
- Next by Date: Re: Abstract class variables question
- Previous by thread: Abstract class variables question
- Next by thread: Re: Abstract class variables question
- Index(es):
Relevant Pages
|