Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Retrieving a Constant from a polymorphic object

Posted on 2006-07-15
2
Medium Priority
?
211 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
[X]
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
2 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 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