CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2008
    Posts
    5

    Help with Dynamic Type’s

    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;

    }
    }
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Help with Dynamic Type’s

    Where is 'XXX' defined?

  3. #3
    Join Date
    Apr 2008
    Posts
    5

    Re: Help with Dynamic Type’s

    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;"

  4. #4
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Help with Dynamic Type’s

    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
    Code:
    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
    Code:
    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
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  5. #5
    Join Date
    Apr 2008
    Posts
    5

    Re: Help with Dynamic Type’s

    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.

  6. #6
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Help with Dynamic Type’s

    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
    Last edited by JonnyPoet; May 1st, 2008 at 06:33 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  7. #7
    Join Date
    Apr 2008
    Posts
    5

    Re: Help with Dynamic Type’s

    Thanks everyone. With your help I think I got this one beat.
    An another nice trick for my bag 

  8. #8
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Help with Dynamic Type’s

    Quote Originally Posted by DrPepper808
    ...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 )
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured