-1

So I'm trying to access a task form program.cs in form 1.cs but I cant find a way that works anywhere. Here is an example of what I want: program.cs

private async void ConsoleSendMessage()
{
    Console.WriteLine("Select the guild:");
    var guild = GetSelectedGuild(_client.Guilds);
    var textChannel = GetSelectedTextChannel(guild.TextChannels);
    var msg = string.Empty;
    while (msg.Trim() == string.Empty)
    {
        Console.WriteLine("Your message:");
        msg = Console.ReadLine();
    }

    await textChannel.SendMessageAsync(msg);
}

And then I want to be able to run that task in form1.cs

mason
  • 28,517
  • 9
  • 66
  • 106
Tristan Read
  • 61
  • 10
  • This method is `async void`, which is a very bad idea. That means you won't wait for the code to complete, and any errors raised by this code won't be surfaced back to the calling code, meaning you won't have any idea that it didn't perform properly. Unless you're in a few situations (such as a framework event handler), do not use `async void`. Use `async Task`. – mason Oct 15 '18 at 17:09
  • It has to be like that for the discord.net thing to work – Tristan Read Oct 16 '18 at 11:57
  • No, no it doesn't. Why do you think that? – mason Oct 16 '18 at 12:29
  • ok well now it is `private void ConsoleInput()`, but i still dont know how to access all of the tasks/functions from `program.cs` – Tristan Read Oct 16 '18 at 14:54
  • You can't, if the method is private. Private means "only this class can see it". you should learn about [access modifiers](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/access-modifiers). – mason Oct 16 '18 at 14:55
  • Ok so i had a little look at that and i understand them now, ive made them public, so how could i access this in another class then? – Tristan Read Oct 16 '18 at 15:03
  • Since it's not static, you need an instance of the class, then you invoke the method on the instance of the class. – mason Oct 16 '18 at 15:19
  • Could you please give an example for my case, i just searched it up but im not too sure about what it is, im quite new to this stuff – Tristan Read Oct 16 '18 at 15:24
  • You know how to create an instance of a class? `MyClass myInstance = new MyClass();` Then you can call methods on the instance of the class `myInstance.MyMethod();` – mason Oct 16 '18 at 15:32
  • OHHH I thnik i got it working :) – Tristan Read Oct 16 '18 at 15:37
  • Ok so it knows what the tasks are now but when i press the button to run it it gives me this error `Object reference not set to an instance of an object` – Tristan Read Oct 16 '18 at 15:50
  • Sounds like you need to read [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142) – mason Oct 16 '18 at 15:57
  • Yeah im lost now – Tristan Read Oct 16 '18 at 16:30

1 Answers1

-1

It usually is not a good idea to add methods to the Program class. This class is just supposed to start the program.

You should store your method in another class. But if you really want to keep in the Program.cs file, make it public and static. Replace :

private async void ConsoleSendMessage()

By

public static async void ConsoleSendMessage()

And use it like this :

Program.ConsoleSendMessage();

in your form1.cs file.

Léo D.
  • 175
  • 1
  • 10
  • You completely ignored the fact that this is an `async void` method. – mason Oct 15 '18 at 17:07
  • His question was about accessibility of his method, not about it being correct. – Léo D. Oct 15 '18 at 17:13
  • No, the question isn't about accessibility. It's about making the code work *properly*. And `async void` in this case is not proper, and your answer should include addressing such an issue rather than completely avoiding it. – mason Oct 15 '18 at 17:58