0

I'm working on a code-editor and I want to call the string line into a keyargs event which is inside another void-returning method.

Output should occur when I type enter key, and then the selected-list from ComboBox should append to text held in RichTextBox.

Now to fulfill that, I'd like to ask you, how to call this method:

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

from within this one:

void lb_KeyDown(object sender, KeyEventArgs e)
    {

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

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

            string comment = line.Substring(index, line.Length - index);

            rtb.SelectedText = comment + " " + lb.SelectedIndex.ToString();
        }
    }

I really need help. Big thanks in advance!

  • What's the problem with commented line? I mean `//ParseLine(string line);` – default locale Mar 06 '13 at 11:32
  • 3errors appears, invalid expression term 'string', invalid expression term ')' and ; expected –  Mar 06 '13 at 11:34
  • 1
    Haven't noticed first time. It should be: `ParseLine(line);` You don't need to specify types in method call. – default locale Mar 06 '13 at 11:35
  • yeah i already do that, 5errors appears The name 'line' does not exist inthe current context(x3) , The name 'index' does not exist in the current context(x2) . –  Mar 06 '13 at 11:37
  • 1
    Please use correct terms. You don't have "something is within a void", and you don't "call a void". These things you're talking about are called *methods* or *functions*. `void` is a placeholder for "no data returned". If a method returns an `int` you don't say "I have this within the int", do you? – Thorsten Dittmar Mar 06 '13 at 11:37
  • Does it compile with this line commented out (as shown in question body)? – default locale Mar 06 '13 at 11:39
  • but everytime i runs it out it always pointing the word 'line' w/c is in this void ParseLine(string line) .how can i call line without using the void statement? –  Mar 06 '13 at 11:39
  • You're passing the parameter wrong. When calling the method, you can not specify a type. See my answer. Also: where's the `index` variable declared? I can't see it... – Thorsten Dittmar Mar 06 '13 at 11:45
  • There is NO VOID STATEMENT. `void` is a special "kind of data", similar to `int` or `string`. You do not use "void" in function calls. In function CALLs you use: function NAME and ARGUMENTS/PARAMETERS. The function runs and either (a)produces a result and returns it, or (b)returns 'nothing', 'void'. Compare `public string MakeDoubleText(string text) { return text+text; }` versus `public void DoPing(string howLoud) { pinger.Ping(howLoud); }`. The first one produces result, the second - just performs something and does not return any value. Hence, the RESULT of the second is VOID. – quetzalcoatl Mar 06 '13 at 11:50
  • so how can i do that sir? how can i call the string line? –  Mar 06 '13 at 11:52
  • To call those two functions I've included above you use only their names and parameters: `MakeDouble("Mom");` or `DoPing("very");`. Please note that there is not a single difference. This is the method call, it does not use any "void" things here. The difference between value-returning and void-returning function is visible only when you actually **try receiving** the produced result: `string result = MakeDouble("Mom");` or `string result = DoPing("very");` -- the former will compile, the latter **will not compile** and fail with errors - that's because it "returns VOID" not string. – quetzalcoatl Mar 06 '13 at 11:54

2 Answers2

1

You are passing the parameter wrong. You can not pass a type when calling a method. The commented line should read

ParseLine(line);

The variable line must be declared somewhere above ParseLine. What it contains is up to you, but probably you want to set

string line = lb.Text;

So your code could read like this:

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();
    }
}
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • as ive mention in comment above after i declare that 5errors appear .The name 'line' does not exist inthe current context(x3) , The name 'index' does not exist in the current context(x2) . . . just a problemwithin this line >>> string comment = line.Substring(index, line.Length - index); <<< source was always this: void ParseLine(string line), how can i call the string line without using void? –  Mar 06 '13 at 11:46
  • Yes, well where *is* `index` declared? I can't see it and neither can the compiler. Also, if you declare it correctly as in my code, there first error can not occur - you're declaring `line` in the wrong place I bet. Update your question with the current code. – Thorsten Dittmar Mar 06 '13 at 11:48
  • if (token == "//" || token.StartsWith("//")) { // Find the start of the comment and then extract the whole comment. int index = line.IndexOf("//"); string comment = line.Substring(index, line.Length - index); rtb.SelectedText = comment; break; } –  Mar 06 '13 at 11:53
  • It seems you're lacking basic understanding of "variable scopes". A variable is only valid in the block where it is declared and any block below that. Just because you have `index` declared *somewhere* doesn't mean you can use it *anywhere*. – Thorsten Dittmar Mar 06 '13 at 11:57
0

Calling the function is not the problem, but you need some way of retrieving the current line in whichever editor you're using. Once you have retrieved it, you can call ParseLine on it, but until you have it you have nothing to work on.

Evan Knowles
  • 7,426
  • 2
  • 37
  • 71