簡體   English   中英

從一個類訪問另一個類的變量

[英]Accessing variable from one class from another

我想在文件“b”中使用文件“a”之外的變量。 我在網上搜索了一下,但仍然不起作用。

所以我就文件bot.cs和文件profanityFilter.cs

profanityFilter.cs我想用incomingMessagebot.cs

機器人.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);
            }
        }
}

但我收到此屏幕截圖中顯示的錯誤:

在此處輸入圖片說明

只需讓profanityFilter.Filter接受一個參數,即來自機器人的消息

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:通常在 C# 中的類名是Pascal Cased

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM