C#对象引用是必需的问题

编程入门 行业动态 更新时间:2024-10-28 09:25:44
本文介绍了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#对象引用是必需的问题

本文发布于:2023-07-30 16:16:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250972.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!