Solved

C# need help parsing method in generic class

Posted on 2009-03-29
7
384 Views
Last Modified: 2013-12-17
I am studying C# 3.0 syntax using O'Reilly's Pocket Reference on my own.  In the attached code, I am trying to call the "indexer" method as in:

public T this[int index] { get { return data[index]; } }

I am unsure how to parse this line.  I also need to know how to call it from Main.  The class code is attached.  Can anyone explain the line simply and let me know how to call it?

The error I am getting is: Using the generic type 'Generics.Stack<T>' requires '1' type arguments
//1.21.1. Generic Types: Here is a generic type Stack<T>, designed to stack instances of type T. 

            //Stack<T> declares a single generic parameter T:
 

            public class Stack<T>

            {

              //In our generic stack example, for instance, we could write an indexer that returns a generic item:
 

              public T this[int index] { get { return data[index]; } }
 

              int position;

              T[] data = new T[100];

              public void Push (T  obj) { data[position++] = obj;  }

              public T Pop()            { return data[--position]; }

            }

Open in new window

0
Comment
Question by:RGRodgers
  • 4
  • 2
7 Comments
 
LVL 23

Expert Comment

by:apresto
Comment Utility
What line in your code gives you this error when you build?
0
 
LVL 8

Author Comment

by:RGRodgers
Comment Utility
The line throwing the error is currently:
Console.WriteLine("Indexer = " + Stack.T());
I say currently because I have tried enough permutations to know that I don't know.
Thanks...Rick  
0
 
LVL 23

Expert Comment

by:apresto
Comment Utility
Is this the example the book gave you? It looks as though you are missing the an index. Could you post your code for the declaration of your Stack instance, You are calling Stack as if it is declared as a static instance, but it is not
A useful article:
http://msdn.microsoft.com/en-us/library/3278tedw.aspx
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Author Comment

by:RGRodgers
Comment Utility
The book only gave me the class definition for this particular method.  I am trying to format the call myself.  So, if I am missing an index, that doesn't surprise me.  I'll attach the whole program.  I was trying to simplify it by not doing so but obviously complicated it instead.  Thanks for all your help...Rick
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;
 

namespace Generics

{
 

            //1.21.3. Generic Methods

            //A generic method declares generic parameters within the signature of a method.
 

            public class Swapper

            {

                public static void Swap<T>(ref T a, ref T b)

                {

                    T temp = a; a = b; b = temp;
 

                }

            }
 

            //1.21.1. Generic Types: Here is a generic type Stack<T>, designed to stack instances of type T. 

            //Stack<T> declares a single generic parameter T:
 

            public class Stack<T>

            {

              //In our generic stack example, for instance, we could write an indexer that returns a generic item:
 

              public T this[int index] { get { return data[index]; } }
 

              int position;

              T[] data = new T[100];

              public void Push (T  obj) { data[position++] = obj;  }

              public T Pop()            { return data[--position]; }

            }
 

    class Program

    {

        static void Main(string[] args)

        {
 

            //We can use Stack<T> as follows:
 

            Stack<int> stack = new Stack<int>();

            stack.Push(5);

            stack.Push(10);

            int x = stack.Pop();       // x is 10

            int y = stack.Pop();       // y is 5

            Console.WriteLine("x = " + x + ";y = " + y + ";");
 
 

            //Swap<T> can be used as follows:
 

            int a = 5, b = 10;

            Console.WriteLine("a = " + a + ";b = " + b + ";");

            Swapper.Swap (ref a, ref b);

            Console.WriteLine("a = " + a + ";b = " + b + ";");
 

            //Access generic indexer

            Console.WriteLine("Indexer = " + Stack.T());

        }

    }

}

Open in new window

0
 
LVL 18

Accepted Solution

by:
philipjonathan earned 500 total points
Comment Utility
Indexer means that you can access the internal data of an object as if that object is an array. Normally the said object itself contains an array of some sort, like in your example:

public T this[int index] { get { return data[index]; } }

This means that "index" is the position in the array that you want to access.

To use:
Console.WriteLine("Indexer = " + stack[0]);
// This will return you the first element in the stack (C# is 0-based)
0
 
LVL 8

Author Comment

by:RGRodgers
Comment Utility
That makes just too much sense.  I think I got so far into trying to figure it out that I missed it.  Thanks for you help.  
0
 
LVL 8

Author Closing Comment

by:RGRodgers
Comment Utility
Thanks for the rapid and excellent responses.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now