如何连接字符串从我的类到形式 [英] How to connect string from my class to form

查看:177
本文介绍了如何连接字符串从我的类到形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编程这个字符串:

I've been working on this stringcodes in program:

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

和它的工作时间我使用它之后的代码(在mainform.cs):

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

但事情是我想制作单独的类.cs),并在mainform中声明,但此代码无效:

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:

KeyWord.cs:

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

Mainform.cs:

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

错误说:


使用未分配的局部变量关键字:

Use of unassigned local variable 'keywords':

请注意,在mainform中处于无效状态:

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

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

我该怎么办?

推荐答案

欢迎来到Stackoverflow,您需要获取KeyWord类的实例,然后将其关键字字符串数组分配给本地声明的 String []关键字在Mainform.cs中,例如:

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

编辑:


找不到类型或名称空间名称KeyWords。

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

code>命名空间编辑器 //< - 删除命名空间或使其与frmMain.cs的命名空间相同或完全限定命名空间实例化 new editor.KeyWord );

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();

我编辑了我的代码以显示最后一个选项。另外如果KeyWord.cs在一个不同的项目到MainForm.cs然后你将需要添加一个参考。

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.

这篇关于如何连接字符串从我的类到形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆