Re: [Mono-list] Parametized generics puzzler

2009-02-23 Thread Chris Howie
On Wed, Jan 28, 2009 at 7:40 AM, ebichete  wrote:
> Does anyone know why the following code doesn't "do the expected thing" ? I
> think I've properly coded my intent in C# but I could be wrong.
>
> I'm trying to get the base class static methods to act differently based on
> static data in the derived classes.
>
> [snip]

The problem here is that you are declaring new members with the same
name on the derived type Truck and are not actually overriding them.
This distinction is important.  Further, static members cannot be
overridden -- period.  What you want is something like:

public class Vehicle {
public virtual string REG_TYPE { get { return ""; } }
public virtual string TAX_CAT { get { return ""; } }
}

public class Truck : Vehicle {
public override string REG_TYPE { get { return "G"; } }
public override string TAX_CAT { get { return "J4"; } }
}

No generics or obnoxious getter methods needed.

P.S. Names like REG_TYPE are in bad taste.  Try something like RegistrationType.

-- 
Chris Howie
http://www.chrishowie.com
http://en.wikipedia.org/wiki/User:Crazycomputers
___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list


[Mono-list] Parametized generics puzzler

2009-02-22 Thread ebichete

Does anyone know why the following code doesn't "do the expected thing" ? I
think I've properly coded my intent in C# but I could be wrong.


I'm trying to get the base class static methods to act differently based on
static data in the derived classes.


- Edward -



using System.Collections.Generic;

public class Vehicle {
protected const string REG_TYPE = "";
protected static string TAX_CAT = "";

public static string getRegType() where T : Vehicle
{
return T.REG_TYPE;
}

public static string getTaxCategory() where T : Vehicle
{
return T.TAX_CAT;
}
}

public class Truck : Vehicle {
protected new const string REG_TYPE = "G";
protected new static string TAX_CAT = "J4";
}

public class GenericsBug {
public static void Main(string[] args)
{
System.Console.WriteLine("Registration Class: " + 
Truck.getRegType());
System.Console.WriteLine("Tax Category: " + 
Truck.getTaxCategory());
}
}


-- 
View this message in context: 
http://www.nabble.com/Parametized-generics-puzzler-tp21704985p21704985.html
Sent from the Mono - General mailing list archive at Nabble.com.
___
Mono-list maillist  -  Mono-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list