如何在c#里面的类中声明一个var?

时间:2016-10-23 03:11:57

标签: c#

我想声明以下内容:

            var simlBot = new SimlBot();

            var botUser = simlBot.CreateUser();

            var packageString = File.ReadAllText("SIMLPackage.simlpk");
            simlBot.PackageManager.LoadFromString(packageString);

如果我直接在类中声明它:“上下文关键字'var'可能只出现在局部变量声明或脚本代码中”

我无法在if语句中声明它,因为我无法从外部访问它。

        if (hasBotBeenCreated == false)
        {
            var simlBot = new SimlBot();

            var botUser = simlBot.CreateUser();

            var packageString = File.ReadAllText("SIMLPackage.simlpk");
            simlBot.PackageManager.LoadFromString(packageString);
        }

我需要在函数中使用以下形式:

        var chatRequest = new ChatRequest(textReceived, botUser);
        var chatResult = simlBot.Chat(chatRequest);

我该怎么办?问题是最后一段代码无法访问botUser和simlBot。我现在使用它的方法是每次调用函数时重新创建对象,但这需要花费很多时间。

我的完整代码是

public partial class Form1 : Form
{
    string textEntered = "";

    string response = "";

    SpeechSynthesizer synth = new SpeechSynthesizer();

    bool hasBotBeenCreated = false;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        textEntered = textBox1.Text;

        textBox1.Text = "";

        if (textEntered != "")
        {
            sendToAI(textEntered);
        }
    }

    void sendToAI(string textReceived)
    {
        listBox1.Items.Add(textEntered);

        response = getResponse(textEntered);

        listBox1.Items.Add(response);
        synth.Speak(response);
    }

    string getResponse(string textReceived)
    {
        if (hasBotBeenCreated == false)
        {
            var simlBot = new SimlBot();

            var botUser = simlBot.CreateUser();

            var packageString = File.ReadAllText("SIMLPackage.simlpk");
            simlBot.PackageManager.LoadFromString(packageString);
        }

        var chatRequest = new ChatRequest(textReceived, botUser);//These two can't access the objects created above
        var chatResult = simlBot.Chat(chatRequest);

        if (chatResult.Success)
        {
            var botMessage = chatResult.BotMessage;
            return botMessage;
        }
        else
        {
            return "I don't have a response for that";
        }
    }
}

修订版2:

    string getResponse(string textReceived)
    {
        SimlBot simlBot;

        BotUser botUser;

        if (hasBotBeenCreated == false)
        {
            simlBot = new SimlBot();

            botUser = simlBot.CreateUser();

            var packageString = File.ReadAllText("SIMLPackage.simlpk");
            simlBot.PackageManager.LoadFromString(packageString);
        }

        var chatRequest = new ChatRequest(textReceived, botUser); //These get the error
        var chatResult = simlBot.Chat(chatRequest);
        if (chatResult.Success)
        {
            var botMessage = chatResult.BotMessage;
            return botMessage;
        }
        else
        {
            return "I don't have a response for that";
        }
    }

1 个答案:

答案 0 :(得分:0)

添加成员:

 SimlBot simlBot;
 WhatEverTypeOfCreateUserIs botUser;

然后初始化它们:

    if (hasBotBeenCreated == false)
    {
        simlBot = new SimlBot();

        botUser = simlBot.CreateUser();

       ....
    }

----------完整代码----------

public partial class Form1 : Form
{
    string textEntered = "";

    string response = "";

    SpeechSynthesizer synth = new SpeechSynthesizer();

    bool hasBotBeenCreated = false;

    SimlBot simlBot;

    BotUser botUser;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        textEntered = textBox1.Text;

        textBox1.Text = "";

        if (textEntered != "")
        {
            sendToAI(textEntered);
        }
    }

    void sendToAI(string textReceived)
    {
        listBox1.Items.Add(textEntered);

        response = getResponse(textEntered);

        listBox1.Items.Add(response);
        synth.Speak(response);
    }

    string getResponse(string textReceived)
    {
        if (hasBotBeenCreated == false)
        {
            simlBot = new SimlBot();

            botUser = simlBot.CreateUser();

            var packageString = File.ReadAllText("SIMLPackage.simlpk");
            simlBot.PackageManager.LoadFromString(packageString);
        }

        var chatRequest = new ChatRequest(textReceived, botUser);//These two can't access the objects created above
        var chatResult = simlBot.Chat(chatRequest);

        if (chatResult.Success)
        {
            var botMessage = chatResult.BotMessage;
            return botMessage;
        }
        else
        {
            return "I don't have a response for that";
        }
    }
}