1

I've been working on this stringcodes in program:

string[] keywords = { "abstract", "as", "etc" };

and its working the time i use it after this code (in mainform.cs):

for (int i = 0; i < keywords.Length; i++)
{
    if (keywords[i] == token)
    {
        // Apply alternative color and font to highlight keyword.
        rtb.SelectionColor = Color.Blue;
        rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Bold);
        break;
    }
}

but thing is i want to make separate class (KeyWord.cs) for keywords and declare it in mainform but this code not working:

KeyWord.cs:

namespace editor
{
    class KeyWord
    {
        string[] keywords = { "abstract", "as", "etc" };
    }
}

Mainform.cs:

string[] keywords;
for (int i = 0; i < keywords.Length; i++)
{
    if (keywords[i] == token)
    {
        // Apply alternative color and font to highlight keyword.
        rtb.SelectionColor = Color.Blue;
        rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Bold);
        break;
    }
}

Error says:

Use of unassigned local variable 'keywords':

note that this code was under of a void condition in mainform:

private void TextChangedEvent(object sender, EventArgs e)
{
}

What should i do?

3
  • check stackoverflow.com/questions/4815311/… Commented Feb 28, 2013 at 5:01
  • Initialize variable as compiler said: string[] keywords = null; Commented Feb 28, 2013 at 5:02
  • @ spajce sir its connection strings to db @ abatishchev sir still theres an error pointing to if (keywords[i] == token) says Operator = cannot be applied to operands of type 'char' and 'string' and Use of unassisned localvariable 'keywords' Commented Feb 28, 2013 at 5:09

2 Answers 2

1

Welcome to Stackoverflow, you need to get an instance of the KeyWord class and then assign its Keywords string array to your locally declared String[] keywords in Mainform.cs, eg:

     var keyboardCls = new editor.KeyWord();
     String[] keywords = keyboardCls.keywords;

        for (int i = 0; i < keywords.Length; i++)
        {
            if (keywords[i] == token)
            {
                // Apply alternative color and font to highlight keyword.
                rtb.SelectionColor = Color.Blue;
                rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Bold);
                break;
            }
        }

Edit:

The Type or namespace name 'KeyWords' could not be found.

namespace editor //<- remove the namespace or make it the same as frmMain.cs's namespace or fully qualify the namespace when instantiating new editor.KeyWord();

I've edited my code to show the last option. Also if the KeyWord.cs is in a different project to the MainForm.cs then you will need to add a Reference.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks sir but still theres an error: 1. Use of unassigned local variable 'keywords' 2. The Type or namespace name 'KeyWords' ould not be found
your welcome but it isn't solved by the sound of things, whats the error message?
i deleted the namespace and change it to namespace frmMain (also delete it permanently the 2nd time) like what youve said sir and try the code: var keyboardCls = new frmMain.KeyWord(); String[] keywords = keyboardCls.keywords; but it says>>> frmMain.KeyWord.keywords is inaccessible due to its protection level .
i even try to change the class to namespace KeyWord but same thing happens: KeyWord.KeyWord.keywords is inaccessible due to its protection level . although goodthing was keywords string was connected in the class now ... but still have an error :(
0

If you want to just call the object and only have one instance of it, use the static keyword. Either way, you're going to have to assign it a value before you use it.

string[] keywords = new string[3];

3 being the predetermined length of the array. If you need a variable length, use List<T>. This is only going to help you for now, though. The best thing for you to do is start reading some books or tutorials.

3 Comments

"but thing is i want to make separate class (KeyWord.cs) for keywords and declare it in mainform"
Like I said, "If you want to just call the object and only have one instance of it, use the static keyword." That goes for putting it another class as well. You either need to make it public static, or create a new instance of your object.
can you teach me more about that sir? or code sample ... on what should i do .really sorry sir newbie here ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.