C
C#2y ago
itgel9462

❔ Calling async method

Hi guys. I tried to call this AzureAPI class in main program. But I got a error. I want to call this string Result in main program and display it. How can I call this class and get a result in a place where I called.
21 Replies
Angius
Angius2y ago
It's not a static method So it needs an instance to be called on
Jorge Morales
Jorge Morales2y ago
You need to create an object of AzureAPI so you can call the method or convert the method to be static
itgel9462
itgel94622y ago
I changed the method to static.
itgel9462
itgel94622y ago
What does this mean?
Jorge Morales
Jorge Morales2y ago
it means the access level is restrictive, did you change the method to be private? because your first screenshot showed it was public
itgel9462
itgel94622y ago
Currently like this. But after I changed to static, string Result got the error.
Angius
Angius2y ago
public static async Task You removed the public for some reason So it defaulted to private
Jorge Morales
Jorge Morales2y ago
apart from that, the Result property has to be static as well
Angius
Angius2y ago
Or... don't make it static
Jorge Morales
Jorge Morales2y ago
because from a static method you can only access another static members in the class
Angius
Angius2y ago
And instantiate the object
Jorge Morales
Jorge Morales2y ago
that's an easier approach I think
Angius
Angius2y ago
That way Result won't have to be static And we'll have proper OOP Instead of funky pseudo-global variables Or, even better, just return the string from this method Instead of using a property
itgel9462
itgel94622y ago
After changed Public static string Result, I got this error. I want to display this string in textbox. I want to do it like this.
Angius
Angius2y ago
I recommend brushing up on the basics What's static, what's an instance, etc
Jorge Morales
Jorge Morales2y ago
I agree, I think you have to review the basics of C#, so you can make the proper changes for your code if you have git enabled, you can undo your changes and apply this, to create an instance of the class
Angius
Angius2y ago
Two ways I can see it working
class Foo
{
public static Task<string> Unga(Bunga boo)
{
return $"Stuff and things and {boo}";
}
}
class Foo
{
public static Task<string> Unga(Bunga boo)
{
return $"Stuff and things and {boo}";
}
}
var stuff = await Foo.Unga(sdfsdf);
var stuff = await Foo.Unga(sdfsdf);
or
class Foo
{
public string Result { get; set; }

public Task Unga(Bunga boo)
{
Result = $"Stuff and things and {boo}";
}
}
class Foo
{
public string Result { get; set; }

public Task Unga(Bunga boo)
{
Result = $"Stuff and things and {boo}";
}
}
var f = new Foo();
await f.Unga();
var stuff = f.Result;
var f = new Foo();
await f.Unga();
var stuff = f.Result;
IMHO the first one is better
itgel9462
itgel94622y ago
Ok guys. Thank you so much. I will try this. Thank you
itgel9462
itgel94622y ago
I called the method in main program. And got this. Do you guys know what that is and what I should do
Jorge Morales
Jorge Morales2y ago
not really, looks like the SpeechRecognizer class comes from a library and it is expecting to find something and fails? I'm just guessing at this time, sorry
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts