Accessing variable from one class from another

86 views Asked by At

I want to use a variable out of file "a" in file "b". I searched online a bit but it still doesn't work.

So I got the file bot.cs and the file profanityFilter.cs.

In the profanityFilter.cs I want to use the incomingMessage from bot.cs.

bot.cs:

class Bot
{
        public static void Client_OnMessageReceived(object sender, OnMessageReceivedArgs e)
        {
            var incomingMessage = e.ChatMessage.Message;

            //Profanity filter testing received message (need to get incoming message over to profanityFilter.cs)

            profanityFilter.Filter();

            Console.WriteLine($"[{TwitchInformation.BotName}]: [{e.ChatMessage.DisplayName}]: 
            {e.ChatMessage.Message}");
        }
} 

profanityFilter.cs:

class profanityFilter
{
        public static async void Filter()
        {
            var client = new HttpClient();

            Bot message = new Bot();
            var incomingMessagesFromUser = message.Client_OnMessageReceived.IncomingMessages;

            var request = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = new Uri("https://neutrinoapi-bad-word-filter.p.rapidapi.com/bad-word-filter"),
                Headers =
                {
                    { "x-rapidapi-key", "xxx" },
                    { "x-rapidapi-host", "neutrinoapi-bad-word-filter.p.rapidapi.com" },
                },
                Content = new FormUrlEncodedContent(new Dictionary<string, string>
                {
                    { "censor-character", "*" },
                    { "content", incomingMessagesFromUser },
                }),
            };

            using (var response = await client.SendAsync(request))
            {
                response.EnsureSuccessStatusCode();
                var body = await response.Content.ReadAsStringAsync();
                Console.WriteLine(body);
            }
        }
}

But I get an error shown in this screenshot:

enter image description here

1

There are 1 answers

5
David Kroukamp On BEST ANSWER

Simply have profanityFilter.Filter accept an argument, the message from the bot i.e.

public static async void Filter(string message)

    public static async void Filter(string message)
    {
        var client = new HttpClient();

        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Post,
            RequestUri = new Uri("https://neutrinoapi-bad-word-filter.p.rapidapi.com/bad-word-filter"),
            Headers =
            {
                { "x-rapidapi-key", "xxx" },
                { "x-rapidapi-host", "neutrinoapi-bad-word-filter.p.rapidapi.com" },
            },
            Content = new FormUrlEncodedContent(new Dictionary<string, string>
            {[enter image description here][1]
                { "censor-character", "*" },
                { "content", message },
            }),
        };
        using (var response = await client.SendAsync(request))
        {
            response.EnsureSuccessStatusCode();
            var body = await response.Content.ReadAsStringAsync();
            Console.WriteLine(body);
        }
    }

PS: Usually in C# class names are Pascal Cased