Solved

struct as an interface and use in an abstract class

Posted on 2010-08-30
3
469 Views
Last Modified: 2012-05-10
I am struggling with how to fefine a struct to be used for data passing into a class.

I would like to create an interface that defines a struct.
Ultimately this struct would be used in a derived class.
I have created an abstract class defined by my interface but I dont knwo how to use the struct.

Is it possible to do what I ask?
I have created a small exmaple of how I see it working.

Am I on the right track here?

Thanks in advance


namespace abc

{

    public interface iParms

    {

        string p1

        {

            get;

            set;

        }

        string p1

        {

            get;

            set;

        }

     }



   public interface iabc

    {



        iParms ParmList

        {

            get;

            set;

        }



	Code method 1,2,3

	.

	.

	.

    }







    public abstract class cabc : iabc

    {

        public struct cParms : iParms;





	Code method 1,2,3

	.

	.

	.

    }

Open in new window

0
Comment
Question by:CSUGDEN
  • 2
3 Comments
 
LVL 4

Expert Comment

by:ricovox
Comment Utility
Hi,

To quickly answer your question: YES to the first part, NO to the second

MS Documentation says:
There is no inheritance for structs as there is for classes. A struct  cannot inherit from another struct or class, and it cannot be the base  of a class. Structs, however, inherit from the base class Object. A struct can implement interfaces, and it does that exactly as classes do.

So you certainly can have an interface as you have shown, and you can use a struct to implement that interface if you want to.
From what I can see, you code would work fine except the last part (in the abstract class)

    public abstract class cabc : iabc
    {
        public struct cParms : iParms; <-- this is NOT valid.


You can do some different things here:
You can simply have a property that you can set to whatever you want (as long as it is an iParms)
Like this:

    public abstract class cabc : iabc
    {
        public iParms ParamList {get;set;}
        //.....

Or maybe you meant that you wanted to define a new struct that is an iParms inside the abstract class. That is possible too:

    public abstract class cabc : iabc
    {
        public iParms ParamList {get;set;}  <--note: you NEED this for it to be an "iabc".
        //.....
        public struct CoolParms : iParm  
        {
                  private string _p1, _p2;
        {
            get {return _p1;}
            set {_p1 = value;}
        }
        string p2
        {
            get {...;}
            set {...;}
        }

       

Does that answer your question, or did I misunderstand?
0
 
LVL 4

Accepted Solution

by:
ricovox earned 125 total points
Comment Utility
Note that in either example I posted above, you can also define other structs elsewhere that also implement iParm, and you can set the "ParmList" property of a non-abstract descendant of cabc to ANY struct OR class that implements iParm. I think that is what you are looking for.
0
 

Author Closing Comment

by:CSUGDEN
Comment Utility
Great answer. There is so much info on this topic but far few good examples.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

12 Experts available now in Live!

Get 1:1 Help Now