TwitchLib in C# crashes after return values

48 views Asked by At

I wrote my program with many global variables and only with public void methods. So I want to change my methods to private static methods with return values. That's what I've done.

Now my program will only pass once through "globalChatMessageReceived" of TwitchLib and then it does nothing at all. No problems or errors will display and the program is running correctly and saves data but it will only doing this once.

I am using

FindPokemonName(MessageSplitForPokemonName[1]);

without return values and

(string[] PokedexName, Boolean ChosenPokemonIsAvailable, Boolean ChosenPokemonIsNoStarter, string NameOfChosenPokemon) = FindPokemonName(MessageSplitForPokemonName[1]);

with return values.

Here is the code with global variables and no return values:

        public void FindTrainerID(string TrainerID, string ChatDisplayName)
        {
            TrainerVorhanden = false;
            if (File.Exists(@"C:\txt\trainer\" + TrainerID + ".txt"))
            {
                string FoundTrainer = File.ReadAllText(@"C:\txt\trainer\" + TrainerID + ".txt");
                Trainer = FoundTrainer.Split('\\');
                TrainerVorhanden = true;
            }

        }

And that's what I've done now with return values:

        private static (string[] Trainer, bool TrainerAvailable) FindTrainerID(string TrainerID)
        {
            string[] Trainer = new string[5];
            Boolean TrainerAvailable = false;
            if (File.Exists(@"C:\txt\trainer\" + TrainerID + ".txt"))
            {
                string FoundTrainer = File.ReadAllText(@"C:\txt\trainer\" + TrainerID + ".txt");
                Trainer = FoundTrainer.Split('\\');
                TrainerAvailable = true;
            }
            return (Trainer, TrainerAvailable);
        }

I tried the program without using the methods with a return value. The program is running constantly with the TwitchLib method "globalChatMessageReceived".

If I'm using return values it does nothing after that.

1

There are 1 answers

5
Mike Bruno On BEST ANSWER

You might find it easier to work with a class rather than returning a tuple like this.

For instance, declare this data class:

class TrainerInfo {
    public string[] Trainer { get; set;}
    public bool TrainerAvailable { get; set;}
}

And then give this a try:

static TrainerInfo FindTrainerID(string TrainerID) {
    var result = new TrainerInfo();

    string trainerPath = $@"D:\junk\trainer\{TrainerID}.txt";
    if (File.Exists(trainerPath)) {
        string FoundTrainer = File.ReadAllText(trainerPath);
        result.Trainer = FoundTrainer.Split('\\').ToArray();
        result.TrainerAvailable = true;
    }
    return result;
}

Also food for thought: Does Trainer absolutely have to be an array, or could you use a more dynamic collection type (such as List<string>)?