是否可以使用“返回”?和“出”;同时?
我有一个方法 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您已经有了两个变量,尽管您不必要地初始化其中之一。只是使用:
就我个人而言,我不太喜欢使用 out 参数,并且如果可能的话会寻找替代设计,但这是一个单独的问题。 (我会将查找所有服务与选择其中一些服务分开。)
You've already got two variables, although you're initializing one of them unnecessarily. Just use:
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.)
只需使用两个变量,如下所示:
“return”对象现在由
lstSelected
引用,而out
对象由lst
引用。Just use two variables, like this:
The 'return'ed object is now referenced by
lstSelected
, while theout
ed object is referenced bylst
.您实现
return
和out
参数的方式似乎没问题。但调用是错误的。 @Ken 的答案指出了正确的方向。然而,
GetSelectedServices
方法中的逻辑很奇怪。 “选定”服务和“常规”服务之间的唯一区别是“常规”服务为NULL
。这导致allServices
列表是 NULL 加上所选服务的集合。在我看来这毫无意义。The way you implemented the
return
and theout
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 isNULL
. Which leads to the result that theallServices
list is a collection of NULLs plus the selected services. Which makes no sense in my opinion.