Iñaki Arango Iñaki Arango - 1 month ago 15
C# Question

How to declare a var inside a class inside in c#?

I want to declare the following things:

var simlBot = new SimlBot();

var botUser = simlBot.CreateUser();

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


If I declare it directly in the class I get: "The contextual keyword 'var' may only appear within a local variable declaration or in script code"

I can't declare it inside an if statement because I can't access it from outside.

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

var botUser = simlBot.CreateUser();

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


I need to use the following form inside a function:

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


What can I do? The problem is that the last snippet of code can't access botUser and simlBot. The way I have it working now is recreating the object every time the function is called, but that takes a lot of time.

My complete code is

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


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

Answer

Add the members:

 SimlBot simlBot;
 WhatEverTypeOfCreateUserIs botUser;

Then initialize them:

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

        botUser = simlBot.CreateUser();

       ....
    }

---------- Full Code ----------

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