Click to See Complete Forum and Search --> : Help with Dynamic Type’s
DrPepper808
April 30th, 2008, 01:51 PM
Hello All
This might be a dumb question but I am having problems defining a data member
Using .net 3.5
Here is my example, any help would be wonderful.
Class1:
class Class1
{
private static bool Class1Active = false;
}
Class2:
class Class2
{
private static bool Class2Active = false;
}
My Program:
internal static class MyProg
{
// This is where I need to replace XXX with something Gerneric
internal static XXX MyClass;
internal static void Initialize()
{
If (True)
{
MyClass = new Class1;
}
Else
{
MyClass = new Class2;
}
}
}
Arjay
April 30th, 2008, 03:02 PM
Where is 'XXX' defined?
DrPepper808
April 30th, 2008, 03:36 PM
thats the problem.
if I define it as Class1 then I can't do "MyClass = new Class2;" later in the code and if I do it as Class2 I can't do "MyClass = new Class1;"
JonnyPoet
April 30th, 2008, 04:58 PM
Hi !
At first welcome to Codeguru.
Here is a general ruel: For any code use code Tags otherwise it losts format as you see and is difficult to read.
Additional : Lots of helpers ignore such posts, because we have to read a lot of posts and are interested to do it quick.Without troubles of bad format.
How to do you can see in the end of my pos. Or simple press the 'code' button in the advanced editor and then add your code just between the tags.
To your question.
This is one of the advantage of c# compared to very old other languages where you could change one object to another without getting any error.
So what you want to do is impossible in C# if there is no inheritance betwenn Class1 and Cass2
You can do it if for example you have
public class Class2 {
// whatever
}
public class Class1 : Class2 {
private int a ;
public int A {
get { return a; }
set { a = value; }
}
// whatever
}
In this case Cass1 is derived from Class2 so you are able to do
class Program {
static void Main(string[] args) {
Class1 myClass = new Class1();
myClass.A = 5;
Class2 testClass = (Class2)myClass;
Class1 resultClass = (Class1)testClass;
Console.WriteLine("a {0}", resultClass.A);
}
}Here you are able to use explicit casting to convert between cllasses when they are related to each other.
But in the given case -casting to baseclass and back to original class for example when you need to store both classes in one array, I would use an Interface instead of derived classes.
So its always necessary to know what you need to achive to find the best solution for your problem.
Hope this helps
DrPepper808
May 1st, 2008, 09:03 AM
Sorry about the bad paste, I will use code Tags from now on.
I have been playing with this for a few days now, and I understand what you are saying but was unable to use your advice with my problem.
This statement worries me
"So what you want to do is impossible in C# if there is no inheritance between Class1 and Class2"
It's might make it Ez'er to think of class 1 and 2 as to different sorting algorithms that come from two DLL. And I want to decide on the fly the one I want to load and initialize.
The Your may also want to Change from one to the other.
I have been playing with this:
http://www.dofactory.com/Patterns/PatternStrategy.aspx
The examples are close to my problem, but it seems that their solution seems very convoluted. Needing both a Strategy and Context Class.
JonnyPoet
May 1st, 2008, 06:13 PM
I have been playing with this:
http://www.dofactory.com/Patterns/PatternStrategy.aspx
The examples are close to my problem, but it seems that their solution seems very convoluted. Needing both a Strategy and Context Class.[/QUOTE]This is funny as I answered this question a few days ago also here in the forum. I'll have a look for which thread it was and giving you he link. The problem cannot be done as it is done for example in C++ But you can use interfaces for this reason. Interfaces are the way we can have different classes in one and the same list. They only need to have all the same interface implemented. But as told before I'll find the link for you.
Here is the link
http://www.codeguru.com/forum/showthread.php?t=450705
And there you will have to look for 'state pattern' ( There is a link to wikipedia )
And here is another thread using generic delegates.
http://www.codeguru.com/forum/showthread.php?t=449944
DrPepper808
May 2nd, 2008, 09:22 AM
Thanks everyone. With your help I think I got this one beat.
An another nice trick for my bag
JonnyPoet
May 2nd, 2008, 10:42 AM
...An another nice trick for my bag Hehe, thts nice. You said you are not english as mothertongue. Whats your mothertongue. ( Mine is Austrian which is a german slang ):D
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.