Wrox Programmer Forums
|
C# 4.0 aka C# 2010 General Discussion Discussions about the C# 4.0, C# 4, Visual C# 2010 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 4.0 aka C# 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 1st, 2012, 02:38 AM
Authorized User
 
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
Default Linked List pg 112 program difficulty

Lets see the actual program in the book

Code:
// this program is the program on page 112 C#4 Wrox  27/11/2012
// it deals with the creation of a linkedlist------ we may remember this program.
//a n error came up as 
//Error	1	The non-generic type 'System.Collections.IEnumerable' cannot be used with type arguments	
// for the following code:  "public class LinkedList<int> : IEnumerable<int>"
// so we included Systems.Collections.Generic;
using System;	
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace scratch_pad_2
{
    class Program
    {

        static void Main(string[] args)
        {
            var list2 = new LinkedList<int>();
            list2.AddLast(1);
            list2.AddLast(3);
            list2.AddLast(5);
            foreach (int i in list2)
            {
                Console.WriteLine(i);
                Console.Read();
            }
        }
    }

    public class LinkedListNode<int>
    {
        public LinkedListNode(int value)
        {
            this.Value = value;
        
        }

        public int Value { get; private set; }
        public LinkedListNode<int> Next { get; private set; }
        public LinkedListNode<int> Prev { get; private set; }

    }

    public class LinkedList<int>:IEnumerable<int>

    {
        public LinkedListNode<int> First { get; private set; }
        public LinkedListNode<int> Last { get; private set; }

        public LinkedListNode<int> AddLast(int node) 
        {
            var newNode = new LinkedListNode<int>(node);
            if (First == null)
            {
                First = newNode;
                Last = First;
            }
            else 
            {
                Last.Next = newNode;
                Last = newNode;
            }
            return newNode;
        }

        public IEnumerator<int> GetEnumerator() 
        {
            LinkedListNode<int> curren = First;
            while (curren != null) 
            {
                yield return curren.Value;
                curren = curren.Next;
            }
        }
        IEnumerator IEnumerable.GetEnumerator() 
        {
            return GetEnumerator();
        }
    }

}


Error	1	Type parameter declaration must be an identifier not a type	C:\Users\lenovo\documents\visual studio 2010\Projects\scratch pad 2\Program.cs	33	33	scratch pad 2

Error	2	Type parameter declaration must be an identifier not a type	C:\Users\lenovo\documents\visual studio 2010\Projects\scratch pad 2\Program.cs	47	29	scratch pad 2

Error 1 Type parameter declaration must be an identifier not a type C:\Users\lenovo\documents\visual studio 2010\Projects\scratch pad 2\Program.cs 33 33 scratch pad 2

Error 2 Type parameter declaration must be an identifier not a type C:\Users\lenovo\documents\visual studio 2010\Projects\scratch pad 2\Program.cs 47 29 scratch pad 2

Q1 ) why these 2 errors come up? And what do they mean?
Q2 ) Can we use the following 2 directives simultaneously:-
using System.Collections.Generic;
using System.Collections;

Q3) why did we use
IEnumerator IEnumerable.GetEnumerator()
Instead of
Public IEnumerator GetEnumerator() { ...etc}, also because IEnumerable<T> inherits from IEnumerable
Q4) Why a data type of var has been selected in the following statement as the datatype of newNode:
var list2 = new LinkedList<int>();
 
Old May 1st, 2014, 09:55 AM
Friend of Wrox
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

When implementing generics (thats what the "<" ">" signify) you can not specify a type within the "<" ">" brackets. That is where you declare the generic type, it is sort of a place holder for the real type that is to be assigned at runtime. To fix do this.

Code:
class Program
    {

        static void Main(string[] args)
        {
            var list2 = new LinkedList<int>();
            list2.AddLast(1);
            list2.AddLast(3);
            list2.AddLast(5);
            foreach (int i in list2)
            {
                Console.WriteLine(i);
                Console.Read();
            }
        }
    }

    public class LinkedListNode<T>
    {
        public LinkedListNode(T value)
        {
            this.Value = value;

        }

        public T Value { get; private set; }
        // we must declare the setters internal so the LinkedList has access
        public LinkedListNode<T> Next { get; internal set; }  
        public LinkedListNode<T> Prev { get; internal set; }

    }

    public class LinkedList<T> : IEnumerable<T>
    {
        // only need public getters, we want setting node to be done
        // via the addlast method
        public LinkedListNode<T> First { get; private set; }
        public LinkedListNode<T> Last { get; private set; }

        public LinkedListNode<T> AddLast(T node)
        {
            var newNode = new LinkedListNode<T>(node);
            if (First == null)
            {
                First = newNode;
                Last = First;
            }
            else
            {
                Last.Next = newNode;
                Last = newNode;
            }
            return newNode;
        }

        // implement interface IEnumberable<T>
        public IEnumerator<T> GetEnumerator()
        {
            LinkedListNode<T> curren = First;
            while (curren != null)
            {
                yield return curren.Value;
                curren = curren.Next;
            }
        }

        // need to implement this to fulfill interface requirement for IEnumberable which IEnumerable<T> inhertits
        // we also implement it explicitly because we implement IEnumerable<T> implicity
        // this one return a type specific enumerator that will down casted to just an IEnumerator on retrun.
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

Last edited by mmorgan30; May 1st, 2014 at 01:59 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked List program on page 112 C#4 Wrox amigo1 C# 4.0 aka C# 2010 General Discussion 0 November 30th, 2012 02:35 AM
about linked list in c++ indumit18 C++ Programming 0 October 5th, 2011 11:26 AM
Linked List? pmcizhere Java Basics 2 March 6th, 2008 09:02 PM
Linked List....!!! amahja56 C++ Programming 4 April 6th, 2004 01:05 PM
Linked Table working very slow with client program Nerijus Access 0 July 7th, 2003 08:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.