C#对象引用是必需的问题 [英] C# an object reference is required problem

查看:104
本文介绍了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#对象引用是必需的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆