Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# need help parsing method in generic class

Posted on 2009-03-29
7
Medium Priority
?
390 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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 2000 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

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

718 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