Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > C# 4.0 aka C# 2010 General Discussion
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 1st, 2012, 01:38 AM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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>();
  #2 (permalink)  
Old May 1st, 2014, 09:55 AM
Friend of Wrox
Points: 528, Level: 8
Points: 528, Level: 8 Points: 528, Level: 8 Points: 528, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 01: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 08: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



All times are GMT -4. The time now is 08:24 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.