[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

struct as an interface and use in an abstract class

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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

650 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