if you don't know the type.. you can either use "object", (which is kinda
crappy)..or
Use a Generic. Here is a sample. (see below).
As far as "compulsary typing"...strong typing makes more maintainable code.
The time spent on code is maintenance, not development.
class MyClass<T>
{
private T _model = default (T);
public MyClass(T attribute)
{
_model = attribute;
}
public T FindIt()
{
return _model;
}
}
class Program
{
static void Main(string[] args)
{
MyClass<intmc1 = new MyClass<int>(123);
int x = mc1.FindIt();
Console.WriteLine(Convert.ToString (x));
MyClass<Exceptionmc2 = new MyClass<Exception>(new Exception("You can use
any object here with Generics!"));
Exception foundEx = mc2.FindIt();
Console.WriteLine( (foundEx.Message ));
MyClass<Guidmc3 = new MyClass<Guid>(Guid.NewGuid());
Guid g = mc3.FindIt();
Console.WriteLine(g.ToString ("N"));
}
}
"Snaggy" <l.******@gmail.comwrote in message
news:69**********************************@c23g2000 hsa.googlegroups.com...
Hi, this is my first day using c# and I must say it's cool but data
types.. I'm not use to them (php...) they're driving me crazy.
I need to declare a class variable, but I don't know it's type since
it's passed to the constructor when the object is created. I'd like
something like this:
class MyClass
{
public MyClass(int attribute)
{
if (attributes == 1)
{
string myDeck = "something";
}
else if (attributes 1)
{
int myDeck = "123";
}
}
public string tellMe() // by the way I'm saying string, but
it might be integer... what the..
{
return myDeck;
}
}
how can I solve this mess? Can anyone reassure me that this compulsory
typing isn't actually just a headache?
bye!
Luca(IT)