C
C#

help

❔ Calling async method

Iitgel94622/4/2023
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.
AAngius2/4/2023
It's not a static method So it needs an instance to be called on
JMJorge Morales2/4/2023
You need to create an object of AzureAPI so you can call the method or convert the method to be static
Iitgel94622/4/2023
I changed the method to static.
Iitgel94622/4/2023
What does this mean?
JMJorge Morales2/4/2023
it means the access level is restrictive, did you change the method to be private? because your first screenshot showed it was public
Iitgel94622/4/2023
Currently like this. But after I changed to static, string Result got the error.
AAngius2/4/2023
public static async Task You removed the public for some reason So it defaulted to private
JMJorge Morales2/4/2023
apart from that, the Result property has to be static as well
AAngius2/4/2023
Or... don't make it static
JMJorge Morales2/4/2023
because from a static method you can only access another static members in the class
AAngius2/4/2023
And instantiate the object
JMJorge Morales2/4/2023
that's an easier approach I think
AAngius2/4/2023
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
Iitgel94622/4/2023
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.
AAngius2/4/2023
I recommend brushing up on the basics What's static, what's an instance, etc
JMJorge Morales2/4/2023
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
AAngius2/4/2023
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
Iitgel94622/4/2023
Ok guys. Thank you so much. I will try this. Thank you
Iitgel94622/4/2023
I called the method in main program. And got this. Do you guys know what that is and what I should do
JMJorge Morales2/4/2023
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
AAccord2/5/2023
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.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
build errors in my functioni want to call the function until the loop ends but its giving me build errors that I'm not 100% sur❔ View cannot be found.LoginModel.cs Pages/Account/Login.cshtml Pages/Account/Register.cshtml AccountController.cs attache❔ Unsupported Media Type - Razor page```{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","st✅ JsonConvert debugging helpHello! I'm having a hard time figuring out how to solve an issue where JsonConvert.DeserializeObject❔ Make a sprite follow mouse while pivoting around character in UnityHi, I its hard to describe my issue by describing it since im not sure exactly what I'm looking for ❔ is Generic Repository a bad implementation of Repository Pattern?Surfing the internet I have found several articles that say it is an anti-pattern because it is an a❔ Problems with using Dotnet 6 on Windows to publish to an exe formatDotnet/C# newb here. Previously I've only done a small amount of development using the **.Net Framew❔ Using net7 documentation while targeting netstandard2.0This might be a bit of a dumb question, but is there anyway to make omnisharp/roslyn's tooltips use ❔ WPF app unit testingI am learning how to make unit tests. I built my app on .net core 6.0 and I created class library (o❔ where exactly should i put authorizationso i read a few posts on where to put authorization and most of the people agree on putting it in th❔ C# WPF playing background musicHello, I have WPF app for game launcher/updater. I wanted to add background music, so I added this cC# wpf mvvm Datagrid filter using LINQSo here I have a MVVM form. the Form contains a Datagrid which is connected to the Databank. I also ❔ Is it worth to create lazy dictionary in this case?I have following class: so as you see Add function add to the dictionary class TreeNode, it looks li❔ Going to use a listbox with a switch for something in the morning, need help with format for bothI would like to set two variables with a listbox using a switch and one variable with a second listb✅ What's your view on tool like Sonarqube?Should I learn from the code smells it captures?✅ Autodesk Maya HelpAnyone in here know Autodesk Maya really well? My mesh tool isn't letting my polygon primitives mesh❔ circular primesso this program is meant to find how many circular primes there are below 1 million (should be 55), ❔ WPF Cannot find child when it's thereHi! I am working on my own RoundedCorners attachedproperty however I have stumbled upon a problem Pr❔ HttpClient exiting program during GetStringAsync() and GetAsync()I'm sending a GET to an API, but when debugging, as soon as you step off the last line, it cuts out ✅ Homework Helphttps://i.gyazo.com/f23d758d46581bcfb84f88d4178cd484.png What should I return from the FillArray()