Solved

Retrieving a Constant from a polymorphic object

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How would you add MULTITHREADING to the attached C# code? 4 62
Error in script 11 56
Diagnostics with Net and Net.Sockets 2 28
2 questions 10 25
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!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

772 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