Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

struct as an interface and use in an abstract class

Posted on 2010-08-30
3
Medium Priority
?
527 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 500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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 Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month20 days, 17 hours left to enroll

810 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