Reputation: 2824
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.
Upvotes: 0
Views: 291
Reputation: 14880
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.
Upvotes: 3
Reputation: 19347
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 out
ed object is referenced by lst
.
Upvotes: 3
Reputation: 1504182
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.)
Upvotes: 4