locked
Create and Initialize Generic Stack RRS feed

  • Question

  • I'm trying to get around the error that my stack is null as I cannot use it until I initialize it it seems.

            private Stack<int> currentStep = new Stack<int>();
            currentStep.Push(1);

    I get an error: Invalid Token

    I can't get this to work because I have a method that is trying to increment the stack and it keeps telling me it's null:

            public static void IncrementStack(Stack<int> s, int numberToIncrementBy)
            {
                    // gets the current value (peek)
                    // Add the new number to the stack (push)
                    s.Push(s.Peek() + numberToIncrementBy);  <--- I get a null exception here because the stack is null
            }

    C# Web Developer
    Wednesday, August 27, 2008 4:10 AM

Answers