Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to calculate the sum of ListNodes, but it says that "'s' Specified cast is not valid."
C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        
        static void Main(string[] args)
        {
            List listOfNodes = new List();
           
            Random r = new Random();
            int sum = 0;
            for (int i = 0; i < 10; i++)
            {
                listOfNodes.addObjects(r.Next(1, 100));
               
            }
            listOfNodes.DisplayList();
            foreach (object s in listOfNodes) 
               sum += (int)s;
            Console.WriteLine(sum);

            Console.ReadLine();
            
        }

        class ListNode
        {
            public object inData { get; private set; }
            public ListNode Next { get; set; }

            public ListNode(object dataValues)
                : this(dataValues, null) { }

            public ListNode(object dataValues, 
                ListNode nextNode)
            {
                inData = dataValues; Next = nextNode;
            }
        } // class ListNode-s dasasruli

        public class List : IEnumerable
        {
            private ListNode firstNode, lastNode;
            private string name;

            public List(string nameOfList)
            {
                name = nameOfList;
                firstNode = lastNode = null;
            }

            public List()
                : this("listOfNodes") { }
           
           
            public void addObjects(object inItem)
            {
                if (isEmpty())
                { firstNode = lastNode = new ListNode(inItem); }
                else { firstNode = new ListNode(inItem, firstNode); }
            }
            
            private bool isEmpty()
            {
                return firstNode == null;
            }

            public void DisplayList()
            {
                if (isEmpty())
                { Console.Write("Empty " + name); }
                else
                { 
                    Console.Write("The " + name + " is:\n");

                    ListNode current = firstNode;

                    while (current != null)
                    {
                        Console.Write(current.inData + " ");
                        current = current.Next;
                    }
                    Console.WriteLine("\n");
                }
            }

            public IEnumerator GetEnumerator()
            {
                ListNode node = firstNode;
                while (node != null)
                {
                    yield return node;
                    node = node.Next;
                }
            }
        }//end of class List
    }
}
Posted
Comments
♥…ЯҠ…♥ 27-Nov-13 9:07am    
In which line?

You can't cast a ListNode to an integer - that's just silly!
C#
foreach (object s in listOfNodes)
    sum += (int)s;
s is a ListNode - because that is what your GetEnumerator method returns:
C#
public IEnumerator GetEnumerator()
{
    ListNode node = firstNode;
    while (node != null)
    {
        yield return node;
        node = node.Next;
    }
}

Probably, what you actually want to do is change the GetEnumerator to return the value instead:
C#
public IEnumerator GetEnumerator()
    {
    ListNode node = firstNode;
    while (node != null)
        {
        yield return node.inData;
        node = node.Next;
        }
    }
 
Share this answer
 
Comments
CPallini 27-Nov-13 9:25am    
5.
OriginalGriff suggested changing what the enumerator returned.
I'd argue against that because it is not the expected semantics of GetEnumerator().
I'd use object less and correct types more:
C#
public class List : IEnumerable<ListNode> ...

and
C#
public IEnumerator<ListNode> GetEnumerator() ...

then the summation can be:
C#
foreach (ListNode s in listOfNodes)
  sum += (int)s.inData;

or, using Linq:
C#
sum = listOfNodes.Sum(s => (int)s.inData);

And, of course, if ListNode.inData will always be int,
then declare it as such and the cast to int is unnecessary.
 
Share this answer
 

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