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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • check http://stackoverflow.com/questions/4815311/error-use-of-unassigned-local-variable-for-string-array – spajce Feb 28 '13 at 05:01
  • Initialize variable as compiler said: `string[] keywords = null;` – abatishchev Feb 28 '13 at 05: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' –  Feb 28 '13 at 05:09

2 Answers2

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.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • 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 –  Feb 28 '13 at 05:06
  • your welcome but it isn't solved by the sound of things, whats the error message? – Jeremy Thompson Feb 28 '13 at 05:07
  • 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 . –  Feb 28 '13 at 05:25
  • 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 :( –  Feb 28 '13 at 05:57
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.

Tim
  • 857
  • 6
  • 13
  • "but thing is i want to make separate class (KeyWord.cs) for keywords and declare it in mainform" – Jeremy Thompson Feb 28 '13 at 05:15
  • 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. – Tim Feb 28 '13 at 05:37
  • can you teach me more about that sir? or code sample ... on what should i do .really sorry sir newbie here ... –  Feb 28 '13 at 06:00