Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.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# 错误CS0246 ;类型或命名空间名称';将数据写入控制台dr';找不到(是否缺少using指令或程序集引用_C# - Fatal编程技术网

C# 错误CS0246 ;类型或命名空间名称';将数据写入控制台dr';找不到(是否缺少using指令或程序集引用

C# 错误CS0246 ;类型或命名空间名称';将数据写入控制台dr';找不到(是否缺少using指令或程序集引用,c#,C#,我在同一个类中运行下面的,不确定为什么找不到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.Appen

我在同一个类中运行下面的,不确定为什么找不到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();
应该是

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;
}

您正在初始化一个方法,就像它是一个类一样

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\u To_Console\u 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#的教程。祝您好运!

它不是控制台应用程序,而是一个表单应用程序,我添加了一个文本框将其用作控制台。如果我这样做,我会得到一个非静态字段、方法、,或属性我试图使写入控制台成为静态,但很难写入文本框。在textBoxConsole.AppendText(ConsoleCmd+“\n”)中–我需要能够从代码中的多个位置访问Write_to_控制台,所以我想它应该是静态的。静态并不意味着要从多个位置或一个位置调用。它意味着要在程序启动和类实例化之间的某个点调用。我建议阅读一些
免费在线C#基本教程
代码您提供的不会重现错误。请确保发布。如果我这样做,我将获取非静态字段、方法或属性“FormMain.Write_to_Console_dr(字符串)”所需的对象引用'TSDK C:\Users\Administrator\Box Sync\Tena\TSDK\Rev_0\TSDK\TSDK\Form1.cs 204I试图将Write_to_Console_dr设置为静态,但很难写入文本框。在textBoxConsole.AppendText(ConsoleCmd+“\n”)中,我能够按照上面所述使用cls.Write_to_to_Console_dr进行编译,但现在我看不到文本框中的文本。