繁体   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