本文介绍了C#对象引用是必需的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的主程序中我试图使用
in my main program I am trying to use
stack.createStack(40);堆栈类中的我有这个功能
in stack class I have this function
public void createStack(object value) { stack S = new stack(); head = new Node(); head.Cont = value; size++; }我只是想做一个构造函数将会创建一个新堆栈,堆栈定义如下:
I am just trying to make a constructor function which will creare a new stack, stack defined like this
<pre>public List<int> stacklist; public class Node { public object Cont; public Node Next; } public Node head; public stack() { head = null; } public stack(object value) { head = new Node(); head.Cont = value; }我收到错误:
I get the error:
Error CS0120 An object reference is required for the non-static field, method, or property 'stack.createStack(object)' Lists我试过的: 我不知道下一步该做什么或如何避免这个
What I have tried: I have no idea what to do next or how to avoid this
推荐答案尝试添加 static ,如下所示: Try adding static like this : public static void createStack(object value) { stack S = new stack(); head = new Node(); head.Cont = value; size++; }
或如果 static 不是您需要的,请按以下方式使用您的课程你的班级有州:
or using your class as follows if static is not what you need and your class has state :
var s = new stack(); s.createStack(40);
更多推荐
C#对象引用是必需的问题
发布评论