Solved

C# need help parsing method in generic class

Posted on 2009-03-29
7
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
API v SOA 8 64
Timeouts during development 3 29
Whice version VisualStudio for new Dev Laptop? 10 43
Returning multiple values with a stored procedure 9 31
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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

751 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