-5

I making a hashtable(not really important I think), I wanna print out all my elements from my variabel linked list, the insertionOrder(it holds strings, but doesn't matter), I have to do like this, in my method i want to return insertionOrder, but not sure how to do that so the method get all the elements, and in my class Program I wanna print out the insertionOrder, I can't find a solution, thanks! Why I have to do like this? my teacher want it like this. I just wonder if you know a solution. Thanks!

class Hashtable
{
    private LinkedList<object> insertionOrder = new LinkedList<object>();
    .
    .
    public LinkedList<object> GetInsertionOrder()
    {
        return insertionOrder;
    }
}


class Program
{
    static void Main(string[] args)
    {
        Hashtable hT= new Hashtable(8);

        Console.Writeline("The elements: " + hT.insertionOrder());         
    }
}
  • Possible duplicate [http://stackoverflow.com/questions/759133/...](http://stackoverflow.com/questions/759133/how-to-display-list-items-on-console-window-in-c-sharp). – John Odom Oct 27 '15 at 21:49
  • Why are you using a LinkedList? What are you trying to accomplish?? – Jonathan Carroll Oct 27 '15 at 21:51
  • My teacher want it like this. I just wonder if you know a solution. I have much more code, but I don't think it's relevant right now. But it should be like this, you can modify it if you want. – Kenneth Ström Oct 28 '15 at 11:03

1 Answers1

0

A few things;

insertionOrder is declared outside the main class, as private, so you're not going to be able to get to it from Main().

insertionOrder has absolutely nothing to do with the Hashtable. I'm surprised your IDE isn't throwing up an error at the hT.insertionOrder() call, since it's not valid.

Try something like this.

class Program
{
    private LinkedList<object> insertionOrder = new LinkedList<object>();

    static void Main(string[] args)
    {
        int count = 1;
        foreach (object o in insertionOrder)
        {
            Console.Writeline("The Element(" + count + "): " + o.ToString());
            count++;
        }
    }
}
Trent
  • 1,595
  • 15
  • 37
  • I didn't add all my code, I have added some code now – Kenneth Ström Oct 28 '15 at 11:07
  • OK, have you tried compiling what you've given here? `insertionOrder()` is not a method available to the Hashtable class, it's a field, which would mean you access it like so: `hT.insertionOrder`. That's just going to print out the object definition though. Please add in the Hashtable constructor and perhaps what your expected output is. – Trent Oct 29 '15 at 00:26