Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c类中使用形式为的函数#_C#_Function_Class_Label - Fatal编程技术网

C# 如何在c类中使用形式为的函数#

C# 如何在c类中使用形式为的函数#,c#,function,class,label,C#,Function,Class,Label,我试图得到一个在类中工作的函数 public void Say(string Text) { lalSay.Text = Text; } 这是表单中的代码 Say("Darn i dont have" + ItemCost + "Bits."); //^this is a string in my program 这是在课堂上 Say(Darn i dont have" + ItemCost + "Bits.

我试图得到一个在类中工作的函数

public void Say(string Text)
    {
        lalSay.Text = Text;
    }
这是表单中的代码

Say("Darn i dont have" + ItemCost + "Bits.");
                          //^this is a string in my program
这是在课堂上

Say(Darn i dont have" + ItemCost + "Bits.);
我就是找不到有什么不对

它不允许我在课堂上使用这个函数

Say(Darn i dont have" + ItemCost + "Bits.);
您的参数不是字符串。像这样使用它

Say("Darn i dont have" + ItemCost + "Bits.");
//   ^ String one         ^ variable  ^ string two

像这样的代码“一些文本”被视为字符串。

您的问题非常缺乏细节,但也许这是一个开始

如果你不能“在类中使用函数”,你是这样使用它的吗

public class Form1:Form{
    public void Say(string Text)
    {
        lalSay.Text = Text;
    }
}

public class AnotherClass{
    public void ConsumingMethod()
    {
        var say = new Form1();
        say.Say("Darn i dont have" + ItemCost + "Bits.");
    }   
}
编辑:
在另一个类中使用表单类中的方法是错误的代码。您应该在自己的类中拥有业务逻辑,并让GUI层使用它。将您的
Say
方法移动到支持类是一个更好的主意,从表单中从另一个类调用它。

如果表单中有Say(),另一个类将不知道Say()是什么。要在类中使用Say(),需要将对表单的引用传递到类中,以便可以针对该表单实例调用Say()。另一种方法是让类引发窗体侦听的自定义事件。然后,当收到该事件时,表单可以使用请求的字符串直接调用Say()。您收到了什么错误?@MahchinLizard:那么您应该指定问题所在。@MahchinLizard可能是因为您没有将
ToString()
放在
ItemCost
上?什么类型是
ItemCost
?它是程序/游戏中的一个字符串。我试图获取一个在类中工作的函数。@MahchinLizard请告诉我这些编辑是否有意义。