Results 1 to 4 of 4

Thread: [2.0] Easy way to identify a class with versions?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    [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:
    1. interface TheInterface
    2. {
    3.     void AFunc();
    4. }
    5.  
    6. //Version 1 uses this
    7. class ClassA : TheInterface
    8. {
    9.     void AFunc()
    10.     {
    11.         //stuff here
    12.     }
    13. }
    14. //Version 2 uses this
    15. class ClassB : TheInterface
    16. {
    17.     void AFunc()
    18.     {
    19.         //stuff here
    20.     }
    21. }
    22. //etc

    I want to easily use the different classes based on a string.

    Currently I am doing

    csharp Code:
    1. string ver = "1.0";
    2. TheInterface inter;
    3. if (ver == "1.0")
    4. inter = (TheInterface)ClassA;
    5. else if (ver == "2.0")
    6. inter = (TheInterface)ClassB;
    7. else
    8. //unsupported

    Is there a neater way of doing that?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    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:
    1. class ClassVersion
    2. {
    3.     TheInterface TheClass;
    4.     string version;
    5.     public version(string ver,TheInterface theclass)
    6.     {
    7.         TheClass = theclass;
    8.         version = ver;
    9.     }
    10. }
    11.  
    12. ClassVersion[] Versions = {
    13. new ClassVersion("1.0",ClassA),
    14. new ClassVersion("2.0",ClassB),
    15. };
    16.  
    17. void Process(string ver)
    18. {
    19.     for(int i = 0;i < Versions.Length; i++)
    20.     {
    21.         if (Versions[i].Version = ver)
    22.         {
    23.             Versions[i].TheClass.AFunc();
    24.             break;
    25.         }
    26.     }
    27. }

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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