Click here to Skip to main content
15,921,295 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I asked a question a little while ago, so I apologies for asking too many questions too quick. However I am refactoring an application of mine and have been stuck for a while with this.

I have a method that retrives several plants names from a source and I want to return a value of the PlantCount of this list without calling the method to count the list because this would cause it to double.

C#
int PlantCount;

private readonly List<string> _ListOfPlantNames = new List<string>();
public List<string> ListOfPlantNames() 
{
            foreach (HtmlNode insidenode in htmlDocument.DocumentNode.SelectNodes("//h2"))
            {
                if (insidenode.InnerText.Contains("("))
                {
                    int index = insidenode.InnerText.IndexOf("(", System.StringComparison.Ordinal);

                    _ListOfPlantNames.Add(insidenode.InnerText.Substring(0, index));
                }
                else
                {
                    _ListOfPlantNames.Add(insidenode.InnerText);
                }

return _ListOfPlantNames;

} 
}


Code format went wrong, sorry. The only way I can do this is by using a method without returning a property but I would prefer to do it this way if possible.

PlantCount = _ListOfPlantsNames.Count;


I appreciate any help and your time given as always!

Thank you.
Posted
Updated 12-Aug-13 2:14am
v2

You've got a few issues in that code.

1) You are doing a return inside your foreach loop

C#
foreach (HtmlNode insidenode in htmlDocument.DocumentNode.SelectNodes("//h2"))
            {
                if (insidenode.InnerText.Contains("("))
                {
                    int index = insidenode.InnerText.IndexOf("(", System.StringComparison.Ordinal);

                    _ListOfPlantNames.Add(insidenode.InnerText.Substring(0, index));
                }
                else
                {
                    _ListOfPlantNames.Add(insidenode.InnerText);
                }

return _ListOfPlantNames;
}


This would explain you getting a value of 0. Move that outside your foreach loop and...at least looking at the code you should be good to go.


However if that is not the case, then i would imagine this could be an alternative

C#
public class Plants
{
    public int PlantCount {get;set;}
    
    public Plants()
    {
        ListOfPlantNames();
    }

    public void ListOfPlantNames()
    {
        List<string> _ListOfPlantNames = new List<string>();
        foreach (HtmlNode insidenode in htmlDocument.DocumentNode.SelectNodes("//h2"))
        {
            if (insidenode.InnerText.Contains("("))
            {
                int index = insidenode.InnerText.IndexOf("(", System.StringComparison.Ordinal);

                _ListOfPlantNames.Add(insidenode.InnerText.Substring(0, index));
            }
            else
            {
                _ListOfPlantNames.Add(insidenode.InnerText);
            }
        }

        PlantCount = _ListOfPlantNames.Count;
    }
}



Usage

C#
Plants plant = new Plants();
plant.ListOfPlantNames();
Console.WriteLine(plant.PlantCount);

[Edit]Moved method call into constructor[/Edit]

If this still does not work then i would start to think that the issue lies somewhere in your foreach loop/conditions of the loop/conditions within the loop. But you should be able to just do list.Count either ways...just my code above provides you an alternative (a good one? who knows...but it should work). Also, if this doesn't work, could you post some of your HTML for me to look at/run my code against. That would help so i could see what is going on as i don't have access to your PC.
 
Share this answer
 
v3
Comments
SteveBaldwin21 12-Aug-13 8:37am    
I see on my question I had wrote the return inside the loop, my apologies as this was not in my code. This happened when I was trying to adjust the formatting in the "<pre>" section. I will try the code below and thank you!.
David_Wimbley 12-Aug-13 8:41am    
Ah ok, well then the other thing to look at would be to set a break point on your foreach loop and see if you are going into that loop at all or if it is skipping over the loop in general.

Since you are getting 0 for .Count i would say that you are skipping the foreach loop all together since based on your foreach loop no matter what your list should have contents of some sort.

So that's reasoning why posting some of your HTML would be helpful since i think the issue lies somewhat with the conditions of the loop.
SteveBaldwin21 12-Aug-13 8:46am    
This answer worked. Thank you very much David.
BillWoodruff 12-Aug-13 11:07am    
David, I don't intend to nit-pick here, but I can't help thinking that for this to be accurate either: 1. the source list (data in the HTML) never changes; or, 2. if the source data changes: a new instance of class 'Plants must be created every time.

As it is now, if the source data changes, and only one instance of 'Plants is created: the list will be built only once: thus, accessing 'PlantCount a second time, when the data changed, would result in a possibly inaccurate value.

If the source data changes, you could simply make the method ListOfPlantNames()into a function that returns an int, and call that function in the 'get accessor of the property 'PlantCount. It also seems to me this class could be made 'static.
David_Wimbley 12-Aug-13 11:37am    
Bill, I agree. I also included that it was a solution...not necessarily a good one...but it should work...and apparently it did. I would imagine/hope that if further tweaking was necessary by OP that he'd be able to handle such but for his needs it appears to have worked.
C#
PlantCount = _ListOfPlantsNames.Count;


try this
 
Share this answer
 
Comments
SteveBaldwin21 12-Aug-13 8:25am    
I did and it passed a value of 0.
Maarten Kools 12-Aug-13 8:29am    
Did you call your method, ListOfPlantNames()? Is the xpath selector correct?
SteveBaldwin21 12-Aug-13 8:31am    
Yep, its called when the list's are assigned to one. Yes the xpath is correct as the values are all correct. It's just I need the count to iterate how many plants to add to the ListView without causing duplication.
Maarten Kools 12-Aug-13 8:34am    
Then _ListOfPlantsNames.Count can never be 0.
SteveBaldwin21 12-Aug-13 8:36am    
sorry - I mean null rather than 0. No items appear in the listbox meaning that the loop is not iterating because the count is null.

Sorry again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900