-1

At the time of developing the code in c# winforms, i have a problem..

In composite pattern, code is like

//Interface

interface Usage
    {  
        public abstract void getinfo();
        public abstract void add(Usage u);
    }

//Leaf

class status : Usage
    {
        public string strng;
        public status()
        {
            this.strng = "Hello";
        }
        public override void getinfo()
        {

        }
        public override void add(Usage u)
        {
        }
    }

//Composite class

class composite : Usage
    {
        string strng;
        ArrayList node=new ArrayList();
        public override void add(Usage u)
        {
            node.Add(u);
        }
        public override void getinfo()
        {
            foreach (Usage u in this.node)
            {
                u.getinfo();
            }

        }   
    }

But i was unable to capture the string strng which is Leaf (status)class? return type of getinfo() method is VOID.But because of interface method implementation i cannot make it STRING return type. anyone please Resolve my problem. Thanks in advance.

kik
  • 247
  • 1
  • 5
  • 15

2 Answers2

3

You'll need to change the interface. It makes no sense to have a 'get' method be void

Robert Levy
  • 28,747
  • 6
  • 62
  • 94
  • ya thank you but what to write in getinfo to capture the string? actually whenever it calls the getinfo it needs to return local string(strng).and that string to be stored in string variable. – kik Jun 14 '11 at 03:48
2

As Robert suggests why not just change it to:

interface Usage
{  
    string getinfo();
    void add(Usage u);
}

and:

class status : Usage
{
    public string strng;
    public status()
    {
        this.strng = "Hello";
    }
    public override string getinfo()
    {
        return strng;
    }
    public override void add(Usage u)
    {
    }
}
Orace
  • 7,822
  • 30
  • 45
b3n
  • 3,805
  • 5
  • 31
  • 46