Alright I'm having a heck of a time figuring out what's wrong with this code. It throws an error of

Error 1 'System.Collections.Generic.Dictionary<Assignment7.Station,int>' does not contain a definition for 'ElementAt' and the best extension method overload 'System.Linq.Enumerable.ElementAt<TSource>(System.Collections.Generic.IEnumerable<TSource>, int)' has some invalid arguments7

However, when I was writing the code I followed the definition exactly... if someone could enlighten me I'd be grateful.
Code follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment7
{
    class SubwaySystem
    {
        //attributes
        Dictionary<Station, int> stationDictionary = new Dictionary<Station,int>();

        //methods
        /**
         * Inserts a new station ahead of one with the given ID
         **/
        void InsertAhead(int id, Station insert)
        {
            if (stationDictionary.ElementAt<Station>(id) == null)
                Console.WriteLine("There is no element at the given ID.");
            else
                if (stationDictionary.ElementAt<Station>(insert.StID) != null)
                    Console.WriteLine("The ID of the station being added already exists.");
                else
                {
                    if (stationDictionary.Count == 0)
                    {
                        stationDictionary.Add(insert, insert.StID);
                        insert.Next = insert;
                        insert.Previous = insert;
                    }
                    else
                    {
                        stationDictionary.Add(insert, id);
                        insert.Next = stationDictionary.ElementAt<Station>(id).Next;
                        insert.Previous = stationDictionary.ElementAt<Station>(id);
                        insert.Next.Previous = insert;
                        insert.Previous.Next = insert;
                    }
                }
        }//End InsertAhead
        /**
         * Inserts a new station before one with the given ID
         **/
        void InsertBehind(int id, Station insert)
        {
            if (stationDictionary.ElementAt<Station>(id) == null)
                Console.WriteLine("There is no element at the given ID.");
            else
                if (stationDictionary.ElementAt<Station>(insert.StID) != null)
                    Console.WriteLine("The ID of the station being added already exists.");
                else
                {
                    if (stationDictionary.Count == 0)
                    {
                        stationDictionary.Add(insert, insert.StID);
                        insert.Next = insert;
                        insert.Previous = insert;
                    }
                    else
                    {
                        insert.Next = stationDictionary.ElementAt<Station>(id);
                        insert.Previous = stationDictionary.ElementAt<Station>(id).Previous;
                        insert.Next.Previous = insert;
                        insert.Previous.Next = insert;
                    }
                }
        }
        /**
         * Removes station with specified ID
         **/
        void RemoveStation(int id)
        {

        }
        /**
         * Starts at the station with the specified id, following the references forward around the subway line,
         * writing the station name and id until returning to the starting station.
         **/
        void TraverseSubwayLine(int startId)
        {

        }

        static void Main(string[] args)
        {
        }
    }
}

Recommended Answers

All 4 Replies

I would like to help, but I need to have the class definition for Assignment7.Station. Thanks!

I would like to help, but I need to have the class definition for Assignment7.Station. Thanks!

Ah, sure thing. Here it is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment7
{
    class Station
    {
        //attributes
        private string stName;
        private int stID;
        private Station next;
        private Station previous;

        //properties
        public string StName { get { return this.stName; } }
        public int StID { get { return this.stID; } }
        public Station Next { get; set; }
        public Station Previous { get; set; }

        //constructor
        public Station(string name, int ID)
        {
            this.stName = name;
            this.stID = ID;
        }
    }
}

Look at error description:

a definition for 'ElementAt' and the best extension method overload 'System.Linq.Enumerable.ElementAt<TSource>(System.Collections.Generic.IEnumerable<TSource>, int)' has some invalid arguments7

You need to set KeyValuePair as a TypeSource,

if (stationDictionary.ElementAt<KeyValuePair<Station, int>>(id).Key ==null)
                Console.WriteLine("There is no element at the given ID.");
            else
                if (stationDictionary.ElementAt<KeyValuePair<Station, int>>(insert.StID).Key != null)
                    Console.WriteLine("The ID of the station being added already exists.");
......

Look at error description:


You need to set KeyValuePair as a TypeSource,

if (stationDictionary.ElementAt<KeyValuePair<Station, int>>(id).Key ==null)
                Console.WriteLine("There is no element at the given ID.");
            else
                if (stationDictionary.ElementAt<KeyValuePair<Station, int>>(insert.StID).Key != null)
                    Console.WriteLine("The ID of the station being added already exists.");
......

Okay, that works for that part, thanks. However, when I try to replace what I have with that for the pointer assignments, it doesn't recognize that the dictionary entry has a next and previous value...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.