Solved

struct as an interface and use in an abstract class

Posted on 2010-08-30
3
492 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
ID: 33560675
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
ID: 33560707
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
ID: 33560871
Great answer. There is so much info on this topic but far few good examples.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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