Click here to Skip to main content
16,020,701 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to implement a stack using C#,but I am not able to declare the variables, I tried a code but I get an error saying "The name 'top' does not exist in the current context".I opened a public static class and declared all the variables in there,but it doesn't seem to be working,can anyone help me out.

I even tried Data.top...it doesnt work

This is the code that I tried
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _2
{




    class Program
    {
      public static class Data
        {
            public const int MAX = 5;//Defining the stack value

           public static int top = -1;//Stack is empty
           public int[] stack = new int[MAX];//Declare an array called stack
        }

            public static void push()
            {
                
                int element;


                if (top ==MAX - 1)
                {
                    Console.WriteLine("Stack Overflow");
                    return;
                }

                else
                {
                    Console.WriteLine("enter the number to be pushed");
                    element = int.Parse(Console.ReadLine());

                    stack[++top] = element;
                    Console.WriteLine("The number is pushed successfully", element);

                }
                return;
            }

            public static void pop()
            {




                int y;



                if (top == -1)
                {
                    Console.WriteLine("Stack underflow");
                    return;
                }

                else
                {
                    y = stack[top];
                    stack[top--] = 0;
                    Console.WriteLine("The element has been popped", y);
                    return;
                }
            }

            public static void display()
            {



                int i;
                if (top == -1)
                {
                    Console.WriteLine("Stack is empty");

                    return;
                }
                else
                {
                    Console.WriteLine("Elements are :\n");
                    for (i = 0; i <= top; i++)
                    {
                        Console.WriteLine("%d\n", stack[i]);
                    }
                    return;
                }
            }



            static void Main()
            {

                int ch;
                do
                {
                    Console.WriteLine("Enter Your choice \n1.Push\n2.Pop \n3.Display \n4.Exit");
                    ch = int.Parse(Console.ReadLine());



                    switch (ch)
                    {
                        case 1: push();
                            break;

                        case 2: pop();
                            break;

                        case 3: display();
                            break;

                        case 4: break;

                        default: Console.WriteLine("Invalid Selection");
                            break;
                    }
                } while (ch != 4);
            }
        }
    }
Posted
Updated 20-May-13 22:51pm
v2

In order to access top (as well other class Data's variables) from class Program, you have to fully reference it, e.g.

C#
if (Data.top == Data.MAX - 1)

instead of
Quote:
if (top ==MAX - 1)



By the way, since Data.stack is not static, you cannot access it without an instance of the Data class.
 
Share this answer
 
v2
Move the close bracket!
C#
public static class Data
  {
      public const int MAX = 5;//Defining the stack value

     public static int top = -1;//Stack is empty
     public int[] stack = new int[MAX];//Declare an array called stack
  }
This is the totality of your Data class, because the close bracket ends the definition.
You need the push, pop and display methods to be within the Data class, so move the close curly bracket below the Main method.
 
Share this answer
 
Here is the code,display doesn't work otherwise the push and pop work perfectly fine!! Thanx so much for helping me out :)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _2
{




class Program
{
public static class Data
{
public const int MAX = 5;//Defining the stack value

public static int top = -1;//Stack is empty
public static int[] stack = new int[MAX];//Declare an array called stack
}

public static void push()
{





int element;


if (Data.top==Data.MAX-1)
{
Console.WriteLine("Stack Overflow");
return;
}

else
{
Console.WriteLine("enter the number to be pushed");
element = int.Parse(Console.ReadLine());
Data.stack[++Data.top] = element;


Console.WriteLine("The number is pushed successfully", element);

}
return;
}

public static void pop()
{

int y;



if (Data.top == -1)
{
Console.WriteLine("Stack underflow");
return;
}

else
{
y = Data.stack[Data.top];
Data.stack[Data.top--] = 0;
Console.WriteLine("The element has been popped", y);
return;
}
}

public static void display()
{



int i;
if (Data.top == -1)
{
Console.WriteLine("Stack is empty");

return;
}
else
{
Console.WriteLine("Elements are :\n");
for (i = 0; i <= Data.top; i++)
{
Console.WriteLine("\n",Data.stack[i]);
}
return;
}
}



static void Main()
{

int ch;
do
{
Console.WriteLine("Enter Your choice \n1.Push\n2.Pop \n3.Display \n4.Exit");
ch = int.Parse(Console.ReadLine());



switch (ch)
{
case 1: push();
break;

case 2: pop();
break;

case 3: display();
break;

case 4: break;

default: Console.WriteLine("Invalid Selection");
break;
}
} while (ch != 4);
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900