簡體   English   中英

C#從另一個類調用函數

[英]C# Call a function from another class

我有 2 個類,一個包含函數,另一個包含命令,我對此很陌生,無法弄清楚如何調用該函數。

這是我試圖調用的函數

private async Task<long> GetMemberId(string members)
{
   long memberID = 0;

   HttpResponseMessage response = await client.GetAsync(StaticObjects.bungieBasePath 
                                  + $@"/GroupV2/Name/{memberID}/1/");
   if (response.IsSuccessStatusCode)
   {
      try
      {
         dynamic content = response.Content.ReadAsAsync<ExpandoObject>().Result;
         Debug.WriteLine(await response.Content.ReadAsStringAsync());
      }
      catch
      {
         throw new ArgumentException("The member could not be found.");
      }
   }
   else
   {
      throw new ArgumentException("An error occurred retrieving the members information.");
   }

   return memberID;
}

然后這是命令

[Command("invite")]
[RequireContext(ContextType.Guild, ErrorMessage = "This command is specific to a particular server so you must send it from a channel within that server")]
public async Task SendInviteAsync()
{
   await Context.Channel.TriggerTypingAsync(new RequestOptions() { Timeout = 30 });

   if (!Context.IsPrivate) await Context.Message.DeleteAsync();

   if (StaticObjects.CheckUserIsAdmin(Context))
   {
      //command to call the memberid function
   }
}

GetMemberId是私有函數,只能在其類中使用。 您需要將其設為公開,然后從其他類調用它,如下所示:

if (StaticObjects.CheckUserIsAdmin(Context))
{
    var class = new firstClass();
    var memberId = class.GetMemberId(members);
}

暫無
暫無

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

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