Jump to content
  • Advertisement
Sign in to follow this  
mounty0

Basic C# query

This topic is 4066 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Recently I've been learning C#, but I'm stuck trying to figure out how to manipulate an individual object on a stack, from the code below:
public class MyObj 
{
	private int val;	

	public MyObj(int v) 
        {
		Val = v;
	}	
	
	public int Val 
        {
		get { return val;  }
		set { val = value; }
	}
}

class MainApp
{	
	public static void Main()
	{
		testStack();		
	}	

	public static void testStack()
	{
		System.Collections.Stack stack = new System.Collections.Stack();

		stack.Push(new MyObj(1));
		stack.Push(new MyObj(10));	
	
		//stack[0].Val = 2;	//--> ?
	}
}


How can I go about accessing and changing the Val variable in stack[0]? as the line doesn't compile at all. I've been searching all over but the examples I found used arrays which do allow subscripts such as this, but not stacks? thanks! mounty

Share this post


Link to post
Share on other sites
Advertisement
You don't. A stack is not meant for use in that way. You only want to perform operations on the top of the stack if you want to use a stack. Otherwise, use some other container (a List<T> -- which, incidentally, isn't a linked list -- is a pretty decent one to use as a de facto container).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!