Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Retrieving a Constant from a polymorphic object

Posted on 2006-07-15
2
207 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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