C# text控件的text属性是某个函数的返回值

来源:百度知道 编辑:UC知道 时间:2024/06/15 01:40:05
如题 我自己写了一个类 静态的 类的代码如下
using System.Text;

namespace Mystucknew
{
class mystucknew
{
private String [] m=new String [10];
private int top = 0;
public int gettop() { return top; }
public String returntop() { return m[top ] ;}
public String pop() { top--; return m[top+1]; ; }
public void push(String x) {top++;x =m[top]; }
public bool IsEmpty() { if (top > 0) return false; else return true; }
public bool IsFull() { if (top == 9) return true; else return false; }
public void clear() { top = -1; }
public static mystucknew Stuck1=new mystucknew() ; }
}
现在我在窗体设计中 加入了1个按钮 希望是点击这个按钮之后 tex1.text变成text1.Text = mystucknew.Stuck1.returntop()
具体来说 就是某个值是某个对象函数的返回值 可是我这样写text1.Text = mystucknew.Stuck1.returntop() 点击按钮之后text1没有任何反应。。。求助!

原因很简单。。恩。。真的很简单。

public void push(String x) {top++;x =m[top]; }
后面你的赋值方向错了。。很明显应该是 m[top]=x;

其它的。在C#里面写属性不需要像C语言和java里面这样使用 gettop(),而应该直接定义为Property:
public property string Top{get{ return m[top]; }}

我帮你把代码给美化+完善+修正一下:

class mystucknew
{
string[] m = new string[10];

int top = -1;

public int Top { get { return top; } } //原来的gettop,改写为属性

public string ReturnTop { get { if (top < 0) { throw new InvalidOperationException("数组中没有内容"); } else { return m[top]; } } } //改写为属性

public string Pop() { return m[top--]; } //合并

public void Push(string x) { if (top >= m.Length - 1) { throw new InvalidOperationException("数组元素溢出"); } else { m[top++] = x; } } //合并

public bool IsEmpty() { return top != -1; }

public bool IsFull() { return top >= m.Length; }

public void Clear() { top = -1; }