Solved

Retrieving a Constant from a polymorphic object

Posted on 2006-07-15
2
204 Views
Last Modified: 2010-04-16
Hi all,

I have a number of classes, each inheriting from an interface, and each with a matching const (i.e. the name of the const is the same, the value is different).

If I'm viewing an instance of one of these classes polymorphically via the interface, is there an easy way to access it's constant value? I could obviously add methods to each base class to return the const, but I wondered if there was any other, perhaps better, way of doing it?

Thanks
Gerry
0
Comment
Question by:gamesmeister
2 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 17113978
Looking at clsss instance via interface, you don't see this constant. I think the best way is to add property to the interface and implement it in every class:

    interface ISample
    {
        void Foo();

        int Constant
        {
            get;
        }
    }

    class Class1 : ISample
    {
        void ISample.Foo()
        {
        }

        int ISample.Constant
        {
            get
            {
                return myConstant;
            }
        }

        const int myConstant = 1;
    };

    class Class2 : ISample
    {
        void ISample.Foo()
        {
        }


        int ISample.Constant
        {
            get
            {
                return myConstant;
            }
        }

        const int myConstant = 2;
    };

Client code:

            ISample s1 = new Class1();
            Console.WriteLine(s1.Constant.ToString());

            ISample s2 = new Class2();
            Console.WriteLine(s2.Constant.ToString());
0
 
LVL 4

Author Comment

by:gamesmeister
ID: 17114558
Yeah, I figured that would be the best way, just wondered if I'd missed something.

Off to update 30+ subclasses...

Thanks Alex
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now