是否可以使用“返回”?和“出”;同时?

发布于 2024-12-09 04:41:19 字数 1809 浏览 1 评论 0原文

我有一个方法 GetSelectedServices() 它从树列表中返回选定的节点, 期望以相同的方法返回选定的节点和我尝试添加 out 参数的所有节点。

但是当我调用这个方法时,我应该给出 out 参数,因此带有 selectedNodes 的返回列表被屏蔽,我无法拥有它。

我的方法

internal List<__ServiceInfo> GetSelectedServices(out List<__ServiceInfo> lstAll)
{
    List<__ServiceInfo> lstSelected = new List<__ServiceInfo>();
    List<__ServiceInfo> lstA = new List<__ServiceInfo>();

    foreach (TreeListNode node in this.tlServices.Nodes)
    {
        if (node.Checked)
        {
            var service = this.tlServices.GetDataRecordByNode(node) as __ServiceInfo;
            lstA.Add(service);

            if (service != null)
            {
                lstSelected.Add(service);
            }

            if (node.Nodes.Count > 0)
            {
                foreach (TreeListNode subNode in node.Nodes)
                {
                    if (subNode.Checked)
                    {
                        service = this.tlServices.GetDataRecordByNode(subNode) as __ServiceInfo;
                        lstA.Add(service);

                        if (service != null)
                        {
                            lstSelected.Add(service);
                        }
                    }
                }
            }
        }
    }
    lstAll = lstA;

    return lstSelected;
}

我调用方法的方式

public bool HasValidModel()
{
    List<__ServiceInfo> lstAll = new List<__ServiceInfo>();
    //here I get all nodes
    var allServices = this.GetAllServices(out lstAll);

    List<__ServiceInfo> lstSelected = new List<__ServiceInfo>();
    //but how to get the list from  ""return lstSelected"";
}

感谢您的任何建议。

I have a method GetSelectedServices() which returns Selected Nodes from a Tree List,
Expecting to return with the same method selected nodes and all Nodes i tried to add an out parameter.

But when I call this method i'm supposed to give the out parameter and so the returned list with selectedNodes is masked, and i cannot have it.

My Method

internal List<__ServiceInfo> GetSelectedServices(out List<__ServiceInfo> lstAll)
{
    List<__ServiceInfo> lstSelected = new List<__ServiceInfo>();
    List<__ServiceInfo> lstA = new List<__ServiceInfo>();

    foreach (TreeListNode node in this.tlServices.Nodes)
    {
        if (node.Checked)
        {
            var service = this.tlServices.GetDataRecordByNode(node) as __ServiceInfo;
            lstA.Add(service);

            if (service != null)
            {
                lstSelected.Add(service);
            }

            if (node.Nodes.Count > 0)
            {
                foreach (TreeListNode subNode in node.Nodes)
                {
                    if (subNode.Checked)
                    {
                        service = this.tlServices.GetDataRecordByNode(subNode) as __ServiceInfo;
                        lstA.Add(service);

                        if (service != null)
                        {
                            lstSelected.Add(service);
                        }
                    }
                }
            }
        }
    }
    lstAll = lstA;

    return lstSelected;
}

The way I call the method

public bool HasValidModel()
{
    List<__ServiceInfo> lstAll = new List<__ServiceInfo>();
    //here I get all nodes
    var allServices = this.GetAllServices(out lstAll);

    List<__ServiceInfo> lstSelected = new List<__ServiceInfo>();
    //but how to get the list from  ""return lstSelected"";
}

thank you for any suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

临走之时 2024-12-16 04:41:19

您已经有了两个变量,尽管您不必要地初始化其中之一。只是使用:

public bool HasValidModel()
{
    List<__ServiceInfo> lstAll;
    var selectedServices = this.GetAllServices(out lstAll);

    // Now use lstAll and selectedServices
}

就我个人而言,我不太喜欢使用 out 参数,并且如果可能的话会寻找替代设计,但这是一个单独的问题。 (我会将查找所有服务与选择其中一些服务分开。)

You've already got two variables, although you're initializing one of them unnecessarily. Just use:

public bool HasValidModel()
{
    List<__ServiceInfo> lstAll;
    var selectedServices = this.GetAllServices(out lstAll);

    // Now use lstAll and selectedServices
}

Personally I don't really like using out parameters much, and would look for an alternative design if possible, but that's a separate matter. (I'd separate out finding all services from selecting some of them.)

玉环 2024-12-16 04:41:19

只需使用两个变量,如下所示:

List<__ServiceInfo> lst;
List<__ServiceInfo> lstSelected = GetSelectedServices(out lst);

“return”对象现在由 lstSelected 引用,而 out 对象由 lst 引用。

Just use two variables, like this:

List<__ServiceInfo> lst;
List<__ServiceInfo> lstSelected = GetSelectedServices(out lst);

The 'return'ed object is now referenced by lstSelected, while the outed object is referenced by lst.

网白 2024-12-16 04:41:19

您实现 returnout 参数的方式似乎没问题。但调用是错误的。 @Ken 的答案指出了正确的方向。

然而,GetSelectedServices 方法中的逻辑很奇怪。 “选定”服务和“常规”服务之间的唯一区别是“常规”服务为NULL。这导致 allServices 列表是 NULL 加上所选服务的集合。在我看来这毫无意义。

The way you implemented the return and the out parameter seems to be fine. But the call is wrong. @Ken's answer points in the right direction.

However the logic in the GetSelectedServices method is odd. The only difference between a "selected" service and a "regular" service is that a "regular" service is NULL. Which leads to the result that the allServices list is a collection of NULLs plus the selected services. Which makes no sense in my opinion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文