[2.0] Easy way to identify a class with versions?
So I have classes that are for different versions and have like like.
csharp Code:
interface TheInterface
{
void AFunc();
}
//Version 1 uses this
class ClassA : TheInterface
{
void AFunc()
{
//stuff here
}
}
//Version 2 uses this
class ClassB : TheInterface
{
void AFunc()
{
//stuff here
}
}
//etc
I want to easily use the different classes based on a string.
Currently I am doing
csharp Code:
string ver = "1.0";
TheInterface inter;
if (ver == "1.0")
inter = (TheInterface)ClassA;
else if (ver == "2.0")
inter = (TheInterface)ClassB;
else
//unsupported
Is there a neater way of doing that?
Re: [2.0] Easy way to identify a class with versions?
Different versions of what? If we don't know what it's supposed to represent we can't really say whether there's a better way to represent it.
Re: [2.0] Easy way to identify a class with versions?
Well the class processes a file which is processed differently for each version.
I was thinking of something like.
csharp Code:
class ClassVersion
{
TheInterface TheClass;
string version;
public version(string ver,TheInterface theclass)
{
TheClass = theclass;
version = ver;
}
}
ClassVersion[] Versions = {
new ClassVersion("1.0",ClassA),
new ClassVersion("2.0",ClassB),
};
void Process(string ver)
{
for(int i = 0;i < Versions.Length; i++)
{
if (Versions[i].Version = ver)
{
Versions[i].TheClass.AFunc();
break;
}
}
}
Re: [2.0] Easy way to identify a class with versions?
I don't think there really is an elegant way to do this. Your original idea looks as good as anything to me. That second code snippet looks a bit convoluted and it also would require you to create processing class instances that may not get used.