繁体   English   中英

错误CS0246找不到类型或名称空间名称“ Write_To_Console_dr”(您是否缺少using指令或程序集引用

[英]Error CS0246 The type or namespace name 'Write_To_Console_dr' could not be found (are you missing a using directive or an assembly reference

我在同一类中运行以下命令,不确定为什么找不到Write_To_Console_dr。

public Int16 Write_To_Console_dr(string ConsoleCmd)
{
    this.textBoxConsole.AppendText(ConsoleCmd + "\n" );
    this.textBoxConsole.AppendText( "Tena_Console>");
    using (StreamWriter MySw = File.AppendText(Globals.LogFileName))
    {
        MySw.WriteLine(ConsoleCmd, " \n");
    }
    return 1;
}

public static Int16 Write_To_Console(string ConsoleCmd)
{
    Write_To_Console_dr Winst = new Write_To_Console_dr();
    Winst(ConsoleCmd);
    return 1;
}

您正在将方法初始化为类。

Write_To_Console_dr Winst = new Write_To_Console_dr();

相反,您应该写:

public static Int16 Write_To_Console(string ConsoleCmd)
{
    Write_To_Console_dr(ConsoleCmd);
    return 1;
}

同样,根据编写代码的方式,您的Write_To_Console_dr方法也可能需要为static

public static Int16 Write_To_Console_dr(string ConsoleCmd)

如果您的方法在类中,则为:

public static Int16 Write_To_Console(string ConsoleCmd)
{
    MyClass cls = new MyClass(); // Where this is your class' name.
    cls.Write_To_Console_dr(ConsoleCmd);
    return 1;
}

但是,正如上面的评论中其他人所述,我认为您需要阅读一些有关面向对象设计和有关C#的教程。 祝好运!

您正在使用函数作为类

你应该这样称呼它而不是

Write_To_Console_dr Winst = new Write_To_Console_dr();

它应该是

int retVal = Write_To_Console(ConsoleCmd);

编辑

还有一种更好的方式编写代码

this.textBoxConsole.AppendText(ConsoleCmd + "\n" );
this.textBoxConsole.AppendText( "Tena_Console>");

将会

textBoxConsole.Text =  textBoxConsole.Text + ConsoleCmd + "\n" + "Tena_Console>";

最后你应该有这样的代码

public Int16 Write_To_Console_dr(string ConsoleCmd)
{
    textBoxConsole.Text =  textBoxConsole.Text + ConsoleCmd + "\n" + "Tena_Console>";
    using(StreamWriter MySw = File.AppendText("akjsdh"))
    {
        MySw.WriteLine(ConsoleCmd, " \n");
    }
    return 1;
}

public Int16 Write_To_Console(string ConsoleCmd)
{
    int retVal =  Write_To_Console_dr(ConsoleCmd);
    return 1;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM