Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working on WPF project to display a list of path in a treeview. I have propertyPaths (Ex: NetworkControl.AlternateIndexText.Value") paths have ids.

Constructors:

public MessageElement()
{
    Children = new List<MessageElement>();
    messageElements = new List<MessageElement>();
}

public MessageElement(int id, string name,   List<MessageElement> children)
{
    this.ID = id;
    this.Name = name;
    this.Children = children;
}

public MessageElement(int id, string path)
{
    this.ID = id;
    this.path = path;
}

Here I am showing how I am doing the parsing.

I am building the child from the IEnumerable propertyPath I pass in to the BuildTree method.

public List<MessageElement> BuildTree(IEnumerable<string> propertyPath)
{
    return (
            from path in propertyPath     // Ex: NetworkControl.AlternateIndexText.Value"
            let splitPath = path.Split('.')
            group path by path.Split('.')[0] into pathElementGroup

            select new MessageElement(ID, path)
            {
              // ID = Id,
              Name = pathElementGroup.Key,             //name of each tree node
              Children = BuildTree(                  //create child from the propertyPath
                from propertyPathElement in pathElementGroup
                where propertyPathElement.Length > pathElementGroup.Key.Length + 1
                select propertyPathElement.Substring(pathElementGroup.Key.Length + 1))
            }).ToList();

    }               

Populate data:

public List<MessageElement> GetRequestTreeNodes()
{
    messageElements.Add(new MessageElement(1, "NetworkControl.AlternateIndexText.Value"));
    messageElements.Add(new MessageElement(2, "NetworkControl.AddressData.DestinationID"));
    messageElements.Add(new MessageElement(2, "NetworkControl.AddressData.MessageOriginatorID.Value"));
}

here I am preparing a list that I will pass to the BuildTree method from the messageElements list.

String[]  propertyPath = messageElements.Select(I => Convert.ToString(I.path)).ToArray();            
// int[] idList = messageElements.Select(I => (I.id)).ToArray();
List<MessageElement> nodeList = BuildTree(propertyPath);
return nodeList;

How can I create an association between the propertyPath list that I am using to build tree node and Id. For example MessageElement(1, "NetworkControl.AlternateIndexText.Value"));

share|improve this question

1 Answer

The answer to your question 'Why is ID still 0?' is that you are never setting ID anywhere. How would it be any other value?

The best opportunity for setting the ID is probably where you create the MessageElement object. You are doing that in your select statement in the BuildTree method.

select new MessageElement
                  { ... }

You are selecting a new MessageElement, but not using the constructor to pass in an ID, nor setting the ID in the initializer block. You'd need to construct your MessageElement and use the constructor that takes id and path as parameters.

select new MessageElement(theId, thePath)
                  { ... }

As to where you get the value of ID, I'm not sure. You're making me work way to hard to understand your code. The code would be a lot easier to understand if you used some meaningful variable names. What is the meaning of 's' and 'g' for example? Some might argue that the meaning is obvious, but it doesn't cost you anything to use meaningful names. Also, your entire BuildTree method is a single return statement, yet it contains two separate LINQ comprehension queries spread over 16 lines. I suspect if you put some effort into rewriting this code to take smaller logical steps, the problems might become more apparent, let alone making the debugging a bit easier. C# is a very expressive language, but it is better to use that expressiveness to enhance clarity, rather than achieving the least possible number of statements.

share|improve this answer
I have a constructor that takes (int id, string name, List<MessageElement> children) but i couldn't find easy way to pass the ID in the BuildTree method. – Ayda Sayed 14 hours ago
The problem is simply that you are never setting the value of ID anywhere. – Tim Long 14 hours ago
thank you for your insightful feed back I am new to c#. The meaning of S is a variable name for string path I am passing in and the g is for the group of strings at position 0. Yes, here my problem is on how I obtain the ID. – Ayda Sayed 14 hours ago
I made changes per your suggestion and made my code more understandable – Ayda Sayed 12 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.