473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Different datatypes, same accessor

AAJ
Hi all

I would like to have a class that can set/return values of different
datatype via a single accessor, i.e. overload the accessor

i.e. something like

DateTime m_DateValue;
string m_StringValue;

public string Value
{
get { return m_StringValue; }
set { m_StringValue = value; }
}
public DateTime Value
{
get { return m_DateValue; }
set { m_DateValue = value; }
}

so I can use something like

ValueHandler testDate = new ValueHandler();
testDate.Value = "1/1/2006";
DateTime returndate = testDate.Value;

As well as

ValueHandler testString = new ValueHandler();
testString.Value = "My test String";
string returnString = testString.Value;

i.e. depending on the datatype, I can use the same accessor to store in
either a DateTime field or a string field

is there anyone who knows how to do this

thanks

Andy
Aug 31 '06 #1
8 1761
I don't think you can do this, not least because it would result in a getter
that varies only by return type; while the CLR supports return-type
overloading, C# does not, or it wouldn't know what to do here:

object val = testDate.Value; // what type to use?

Similarly, you can't (IIRC) use generics in properties.

So either you need different names, or you could use (for string) the far
more common .Parse(string) and .ToString() pair.

Marc

Aug 31 '06 #2
The problem you are seeing is that the properties are being expanded by the
compiler to:

string GetValue();
SetValue(string);

DateTime GetValue();
SetValue(DateTime);

The Set methods are not too much of a problem as C# follows C++ overloading
rules and allows for methods to have the same name but different types of
parameters.

The Get methods however only differ by return value and therefore are seen
to be the same method. Incidently the CLR does support methods having the
same name but a different return value it's just that the language doesn't
for a variety of reasons.

Based on the code below if this class is to be used then having two fields
holding the same value is probably asking for trouble (normalisation of data
etc.).

If the class is wrapping a DateTime then I'd suggest:

1. Only contain a DateTime field; lose the string.
2. Use the Value accessor to get/set the DateTime value (or possible use
conversion operators see below).
3. Use ToString to convert the DateTime to a string.
4. Either add a SetValue(string) and parse the string into the DateTime
field or look into using conversion operators (see
http://windowssdk.msdn.microsoft.com...85w54y0a.aspx).

HTH

- Andy

"AAJ" <a.a.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Hi all

I would like to have a class that can set/return values of different
datatype via a single accessor, i.e. overload the accessor

i.e. something like

DateTime m_DateValue;
string m_StringValue;

public string Value
{
get { return m_StringValue; }
set { m_StringValue = value; }
}
public DateTime Value
{
get { return m_DateValue; }
set { m_DateValue = value; }
}

so I can use something like

ValueHandler testDate = new ValueHandler();
testDate.Value = "1/1/2006";
DateTime returndate = testDate.Value;

As well as

ValueHandler testString = new ValueHandler();
testString.Value = "My test String";
string returnString = testString.Value;

i.e. depending on the datatype, I can use the same accessor to store in
either a DateTime field or a string field

is there anyone who knows how to do this

thanks

Andy

Aug 31 '06 #3
"AAJ" <a.a.coma écrit dans le message de news:
%2****************@TK2MSFTNGP04.phx.gbl...

| I would like to have a class that can set/return values of different
| datatype via a single accessor, i.e. overload the accessor

This is not a good idea as it can cause confusion, unless you expressly name
the proeprties differently or perhaps, if you are using VS2005, use
generics.

public class ValueHandler<typeT>
{
private typeT value;

public typeT Value
{
get { return value; }
set { this.value = value; }
}
}

{
ValueHandler<stringtestString = new ValueHandler<string>();
testString = "My test String";
string returnString = testString.Value;
}

....or...

{
ValueHandler<DateTimetestDate = new ValueHandler<DateTime>();
testDate = DateTime.Parse("1/1/2006");
DateTime returnDate = testDate.Value;
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Aug 31 '06 #4
AAJ
Thanks all for the rapid answers!!!

the genreal consensus is its not the way to go...

I just started typing a long winded reply with full details of my problem,
but as so often happens when you stop and think, an alternative solution
popped into my head.

It turns out I can use an inherited Abstract base class to form a structure,
and overide the bits I need for managing the type conversions

thanks again for all the replies

Andy

"AAJ" <a.a.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Hi all

I would like to have a class that can set/return values of different
datatype via a single accessor, i.e. overload the accessor

i.e. something like

DateTime m_DateValue;
string m_StringValue;

public string Value
{
get { return m_StringValue; }
set { m_StringValue = value; }
}
public DateTime Value
{
get { return m_DateValue; }
set { m_DateValue = value; }
}

so I can use something like

ValueHandler testDate = new ValueHandler();
testDate.Value = "1/1/2006";
DateTime returndate = testDate.Value;

As well as

ValueHandler testString = new ValueHandler();
testString.Value = "My test String";
string returnString = testString.Value;

i.e. depending on the datatype, I can use the same accessor to store in
either a DateTime field or a string field

is there anyone who knows how to do this

thanks

Andy

Aug 31 '06 #5
AAJ,

Nice you found it, but I am curious about the sence, can you explain that to
us?

Cor

"AAJ" <a.a.comschreef in bericht
news:OA**************@TK2MSFTNGP04.phx.gbl...
Thanks all for the rapid answers!!!

the genreal consensus is its not the way to go...

I just started typing a long winded reply with full details of my problem,
but as so often happens when you stop and think, an alternative solution
popped into my head.

It turns out I can use an inherited Abstract base class to form a
structure, and overide the bits I need for managing the type conversions

thanks again for all the replies

Andy

"AAJ" <a.a.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>Hi all

I would like to have a class that can set/return values of different
datatype via a single accessor, i.e. overload the accessor

i.e. something like

DateTime m_DateValue;
string m_StringValue;

public string Value
{
get { return m_StringValue; }
set { m_StringValue = value; }
}
public DateTime Value
{
get { return m_DateValue; }
set { m_DateValue = value; }
}

so I can use something like

ValueHandler testDate = new ValueHandler();
testDate.Value = "1/1/2006";
DateTime returndate = testDate.Value;

As well as

ValueHandler testString = new ValueHandler();
testString.Value = "My test String";
string returnString = testString.Value;

i.e. depending on the datatype, I can use the same accessor to store in
either a DateTime field or a string field

is there anyone who knows how to do this

thanks

Andy


Aug 31 '06 #6
AAJ
Hi Cor

The class was needed to provide an easy to use standard, for seperating the
GUI and the Database, so the GUI layer was totally removed from the business
layer and the database layer.

Its a bit more involved but in essence, the class needed to hold a value, a
fieldname, a flag to say an update was required and a method to prepare
itsself in a common way for one of a standardised stored procedure. One
stored procedure handles DateTime updates, another VARCHAR updates, another
Decimal (for currency) etc...

The objects created needed to be of a common base, so I could easily add to
an array list, meaning I could use foreach.. if myclass.changereq = true
then ...UpdateDatabase()

My initial thought (and post) was to overload the accessors, so If the
stored value was a DateTime, UpdateDataBase() would return Value and get its
base class variable and the over loading would know to return a DATETIME
value, Varchar with a string etc...

After reading the replies, it seemd it couldn't be done other than by
converts and object classes.

However, It turned out to be easy when I thought about it ( I should have
been more carefull at the start!), all I needed was a class that I could
inherit from my base class that had UpdateDataBase(), change pending etc
defined. The inherited classes overrode UpdateDataBase() method, and used
their own variable types.

so I have base class --UpdateDataBase()

then I inherit from it for say a

DateManager class, that has its own Dataype Value of DateTime plus an
overridden UpdateDataBase()
a StringManager class, that has its own DataypeValue of String plus an
overridden UpdateDataBase()

it was easy to add each object to an ArrayList and when I need to update I
can use foreach, check the Update required, flag, and then call the
UpdateDatabase flag

worked a treat

I'm new to C# and OOPS so things that used to come easily, I now have to
think about much more, so thanks again everyone who responded

Andy

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:Og**************@TK2MSFTNGP02.phx.gbl...
AAJ,

Nice you found it, but I am curious about the sence, can you explain that
to us?

Cor

"AAJ" <a.a.comschreef in bericht
news:OA**************@TK2MSFTNGP04.phx.gbl...
>Thanks all for the rapid answers!!!

the genreal consensus is its not the way to go...

I just started typing a long winded reply with full details of my
problem, but as so often happens when you stop and think, an alternative
solution popped into my head.

It turns out I can use an inherited Abstract base class to form a
structure, and overide the bits I need for managing the type conversions

thanks again for all the replies

Andy

"AAJ" <a.a.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>>Hi all

I would like to have a class that can set/return values of different
datatype via a single accessor, i.e. overload the accessor

i.e. something like

DateTime m_DateValue;
string m_StringValue;

public string Value
{
get { return m_StringValue; }
set { m_StringValue = value; }
}
public DateTime Value
{
get { return m_DateValue; }
set { m_DateValue = value; }
}

so I can use something like

ValueHandler testDate = new ValueHandler();
testDate.Value = "1/1/2006";
DateTime returndate = testDate.Value;

As well as

ValueHandler testString = new ValueHandler();
testString.Value = "My test String";
string returnString = testString.Value;

i.e. depending on the datatype, I can use the same accessor to store in
either a DateTime field or a string field

is there anyone who knows how to do this

thanks

Andy



Sep 1 '06 #7
You mention ArrayList, so I guess you're using 1.1 - but note that this
would be an ideal candidate for generics in 2.0 - so your DateManager
and StringManager would become simply Manager<DateTimeand
Manager<string>, and you only need to write the code once. The generic
class can still talk happily to the non-generic database code, but it
means your items are a: type-safe and b: only boxed at the final step,
and not before.

The only thing you might need to do would be a simple parameter-type
converter (for the DbType)...

Marc

Sep 1 '06 #8
AAJ,

Thanks for your explanation,

Cor

"AAJ" <a.a.comschreef in bericht
news:%2****************@TK2MSFTNGP04.phx.gbl...
Hi Cor

The class was needed to provide an easy to use standard, for seperating
the GUI and the Database, so the GUI layer was totally removed from the
business layer and the database layer.

Its a bit more involved but in essence, the class needed to hold a value,
a fieldname, a flag to say an update was required and a method to prepare
itsself in a common way for one of a standardised stored procedure. One
stored procedure handles DateTime updates, another VARCHAR updates,
another Decimal (for currency) etc...

The objects created needed to be of a common base, so I could easily add
to an array list, meaning I could use foreach.. if myclass.changereq =
true then ...UpdateDatabase()

My initial thought (and post) was to overload the accessors, so If the
stored value was a DateTime, UpdateDataBase() would return Value and get
its base class variable and the over loading would know to return a
DATETIME value, Varchar with a string etc...

After reading the replies, it seemd it couldn't be done other than by
converts and object classes.

However, It turned out to be easy when I thought about it ( I should have
been more carefull at the start!), all I needed was a class that I could
inherit from my base class that had UpdateDataBase(), change pending etc
defined. The inherited classes overrode UpdateDataBase() method, and used
their own variable types.

so I have base class --UpdateDataBase()

then I inherit from it for say a

DateManager class, that has its own Dataype Value of DateTime plus an
overridden UpdateDataBase()
a StringManager class, that has its own DataypeValue of String plus an
overridden UpdateDataBase()

it was easy to add each object to an ArrayList and when I need to update I
can use foreach, check the Update required, flag, and then call the
UpdateDatabase flag

worked a treat

I'm new to C# and OOPS so things that used to come easily, I now have to
think about much more, so thanks again everyone who responded

Andy

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:Og**************@TK2MSFTNGP02.phx.gbl...
>AAJ,

Nice you found it, but I am curious about the sence, can you explain that
to us?

Cor

"AAJ" <a.a.comschreef in bericht
news:OA**************@TK2MSFTNGP04.phx.gbl...
>>Thanks all for the rapid answers!!!

the genreal consensus is its not the way to go...

I just started typing a long winded reply with full details of my
problem, but as so often happens when you stop and think, an alternative
solution popped into my head.

It turns out I can use an inherited Abstract base class to form a
structure, and overide the bits I need for managing the type conversions

thanks again for all the replies

Andy

"AAJ" <a.a.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl.. .
Hi all

I would like to have a class that can set/return values of different
datatype via a single accessor, i.e. overload the accessor

i.e. something like

DateTime m_DateValue;
string m_StringValue;

public string Value
{
get { return m_StringValue; }
set { m_StringValue = value; }
}
public DateTime Value
{
get { return m_DateValue; }
set { m_DateValue = value; }
}

so I can use something like

ValueHandler testDate = new ValueHandler();
testDate.Value = "1/1/2006";
DateTime returndate = testDate.Value;

As well as

ValueHandler testString = new ValueHandler();
testString.Value = "My test String";
string returnString = testString.Value;

i.e. depending on the datatype, I can use the same accessor to store in
either a DateTime field or a string field

is there anyone who knows how to do this

thanks

Andy



Sep 2 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: mirandacascade | last post by:
When I look at how classes are set up in other languages (e.g. C++), I often observe the following patterns: 1) for each data member, the class will have an accessor member function (a...
3
by: LuCk | last post by:
Can someone explain what these really are for example: ---------------------------------------------------------- void SetFrameRate(int iFrameRate) { m_iFrameDelay = 1000 / iFrameRate; }; ...
3
by: madsen | last post by:
Hi, I am a developper who needs to return a collection of different datatypes. Have considered using an array containing arraylists. Is it posible? or is there a better way? Would like to use some...
1
by: Pol Bawin | last post by:
Hi, I have a objectA with a property that return an object B (accessor GET only) I have defined an ExpandableObjectConverter on the type B and overrided CanConvertTo, CanConvertFrom, ... The...
6
by: Jason Shohet | last post by:
I have a class with protected variables and some accessor methods, , get, set ... Maybe I have a brain blockage today but I'm thinking, why not just make those variables public. After all,...
11
by: Kavvy | last post by:
How come a lot of the examples I have seen on the net define accessor pairs within a class, and then only ever use the accessor outside the class? When used within the class the variable is...
7
by: Javaman59 | last post by:
This is probably common knowledge to .Net gurus, but please bear with me while I share a discovery with the group... I needed to create a public lock on a class, as follows... class Locked {...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
8
by: Tim Sprout | last post by:
Why is it considerd best practice to use Properties rather than Get and Set accessor methods? -Tim Sprout
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.