Solved

struct as an interface and use in an abstract class

Posted on 2010-08-30
3
509 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

707 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