Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.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#_Icsharpcode - Fatal编程技术网

C# 如何从另一个方法调用一个方法?

C# 如何从另一个方法调用一个方法?,c#,icsharpcode,C#,Icsharpcode,我正在处理一个代码编辑器,我想将字符串行调用到另一个void返回方法中的keyargs事件中 当我键入enter键时,输出应该发生,然后ComboBox中的选定列表应该附加到RichTextBox中保存的文本中 现在,为了实现这一点,我想问您,如何调用此方法: void Parse() { String inputLanguage = "using System;\n" + "\n" + "public class Stuff :

我正在处理一个代码编辑器,我想将字符串行调用到另一个void返回方法中的keyargs事件中

当我键入enter键时,输出应该发生,然后ComboBox中的选定列表应该附加到RichTextBox中保存的文本中

现在,为了实现这一点,我想问您,如何调用此方法:

void Parse()
    {
        String inputLanguage =

          "using System;\n" + "\n" +
          "public class Stuff : Form { \n" +
          "  public static void Main(String args) {\n" +
          "\n" + "\n" +
        "  }\n" +
        "}\n";

        // Foreach line in input,
        // identify key words and format them when adding to the rich text box.
        Regex r = new Regex("\\n");
        String[] lines = r.Split(inputLanguage);
        foreach (string l in lines)
        {
            ParseLine(l);
        }
    }
void ParseLine(string line)
{
    Regex r = new Regex("([ \\t{}();])");
    String[] tokens = r.Split(line);

    foreach (string token in tokens)
    {

        // Set the token's default color and font.
        rtb.SelectionColor = Color.Black;
        rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Regular);

        // Check for a comment.
        if (token == "//" || token.StartsWith("//"))
        {
            // Find the start of the comment and then extract the whole comment.
            int index = line.IndexOf("//");

            rtb.SelectedText = comment;
            break;
        }

        // Check whether the token is a keyword. 
        var keywordsDef = new KeyWord();
        String[] keywords = keywordsDef.keywords;

        for (int i = 0; i < keywords.Length; i++)
        {
            if (keywords[i] == token)
            {
                // Apply alternative color and font to highlight keyword.
                HighlighType.keywordsType(rtb);
                break;
            }
        }
        rtb.SelectedText = token;
    }
    rtb.SelectedText = "\n";
}

我真的需要帮助。非常感谢

您传递的参数错误。调用方法时不能传递类型。注释行应为

ParseLine(line);
变量
line
必须在
ParseLine
上方的某个位置声明。它包含的内容取决于您,但您可能希望设置

string line = lb.Text;
因此,您的代码可以如下所示:

void lb_KeyDown(object sender, KeyEventArgs e)
{

    if (e.KeyCode == Keys.Escape)
    {
        lb.Visible = false;
        lb.Items.Clear();
    }

    if (e.KeyCode == Keys.Enter)
    {
        string line = lb.Text;
        ParseLine(line);
        //Parse();

        string comment = line.Substring(index, line.Length - index);
        rtb.SelectionColor = Color.Green;
        rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Italic);
        rtb.SelectedText = comment + " " + lb.SelectedIndex.ToString();
    }
}

调用函数不是问题,但是您需要某种方法来检索正在使用的编辑器中的当前行。一旦您检索到它,您就可以对它调用
ParseLine
,但在找到它之前,您没有任何工作要做。

注释行有什么问题?我的意思是
//ParseLine(字符串行)3出现错误、无效的表达式术语“字符串”、无效的表达式术语“)”和;我第一次没注意到。应该是:
ParseLine(line)您不需要在方法调用中指定类型。是的,我已经这样做了,5个错误显示名称“行”在当前上下文(x3)中不存在,名称“索引”在当前上下文(x2)中不存在。请使用正确的术语。你没有“某物在虚空中”,你也没有“称虚空为虚空”。你所说的这些东西叫做方法或函数
void
是“未返回数据”的占位符。如果一个方法返回一个
int
你没有说“我在int中有这个”,是吗?正如我在上面的评论中提到的,在我声明出现了5个错误之后。名称“line”在当前上下文(x3)中不存在,名称“index”在当前上下文(x2)中不存在。这行中有一个问题>>>字符串注释=line.Substring(index,line.Length-index);是的,那么
索引在哪里声明?我看不见它,编译器也看不见。另外,如果您在我的代码中正确地声明了它,则不会出现第一个错误-您在我打赌的错误位置声明了
line
。使用当前代码更新您的问题。如果(标记=“/”/“| |标记.StartsWith(“/”){//找到注释的开头,然后提取整个注释。int index=line.IndexOf(“/”);string comment=line.Substring(index,line.Length-index);rtb.SelectedText=comment;break;}您似乎对“变量范围”缺乏基本了解。变量仅在声明它的块和该块下的任何块中有效。仅仅因为你在某处声明了
索引
,并不意味着你可以在任何地方使用它。
void lb_KeyDown(object sender, KeyEventArgs e)
{

    if (e.KeyCode == Keys.Escape)
    {
        lb.Visible = false;
        lb.Items.Clear();
    }

    if (e.KeyCode == Keys.Enter)
    {
        string line = lb.Text;
        ParseLine(line);
        //Parse();

        string comment = line.Substring(index, line.Length - index);
        rtb.SelectionColor = Color.Green;
        rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Italic);
        rtb.SelectedText = comment + " " + lb.SelectedIndex.ToString();
    }
}