Solved

C# need help parsing method in generic class

Posted on 2009-03-29
7
387 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
ID: 24015241
What line in your code gives you this error when you build?
0
 
LVL 8

Author Comment

by:RGRodgers
ID: 24015295
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
ID: 24015333
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 8

Author Comment

by:RGRodgers
ID: 24015350
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
ID: 24015398
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
ID: 24015411
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
ID: 31564136
Thanks for the rapid and excellent responses.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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