开发者

How to get value from VOID type method?

开发者 https://www.devze.com 2023-03-12 12:01 出处:网络
At the time of developing the code in c# winforms, i have a problem.. In composite pattern, code is like

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 (U开发者_JAVA技巧sage 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.


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


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)
    {
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号