C
C#2y ago
itgel9462

❔ can't make it Public

Hi guys. There's some type problem. Please help me out. I want to declare that variable named 「string Result」 in other place. To do so I changed its type to public. (made it "public string Result;") Then I got errors. How can I make this variable accessible?
29 Replies
jalepi
jalepi2y ago
You want to make it a class field? Move it to the class body, like just before the method definition
itgel9462
itgel94622y ago
This Class1.cs file represents this code that got error. I want to declare this string type variable in Mainwindow.xaml.cs. I think the type must be public to be accessible, so I tried to make it public up there, and having errors.
itgel9462
itgel94622y ago
Sorry for that, I'm totally new at programming. I can't figure it out how to make it accessible. I have questions. Is class body determined? And this class1.cs is not fitting the class body? that's why i cannot access to this program from out of outside.
jalepi
jalepi2y ago
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/fields At learn.microsoft there are several courses and it's a good starting point for beginners
Fields - C# Programming Guide
A field in C# is a variable of any type that is declared directly in a class or struct. Fields are members of their containing type.
Shinyshark
Shinyshark2y ago
Just to clarify, your issue is regarding scopes. In C# programming (and other languages too, I would assume) you can create resources in scopes. @Itgel
Shinyshark
Shinyshark2y ago
So you have a few scopes in your screenshot. I marked them with arrows for you. Scopes can access scopes inside of them, but not the other way around. If you want to use your result string inside of the entire method, you have to declare it in the method scope. Above the
using (var audioConfig)
using (var audioConfig)
line. The reason you cannot make this string public is because your method scope does not support access modifiers. Access modifiers are part of objects, like your class. Your method cannot contain something public because you cannot reach inside the method to grab it. I hope that makes sense to you. It is hard to explain your issue without going into a little bit of detail.
itgel9462
itgel94622y ago
Thank you man. Thank you so much. I feel like I understand something a bit. That means I cannot make it public by only writing "public string Result" in this class. What I have to do is, change a method scope to make it accessible variable right?
Shinyshark
Shinyshark2y ago
Why do you want the result variable to be public? If you tell me that I can help you understand where you should place it / why.
itgel9462
itgel94622y ago
This string Result is the string that I get it as a result from speech recognition API. I want to create a subtitle box in main program. To do so, I want to grab string Result from this class1.cs and declare in main program to create the subtitle box. Here is a main program. x_coordinate.Text is the variable that represents subtitle string.
itgel9462
itgel94622y ago
Is that clear to understand?
Shinyshark
Shinyshark2y ago
Sure, can you hover over the Result so I can see the error?
itgel9462
itgel94622y ago
like this?
Shinyshark
Shinyshark2y ago
Ah, I see. You have to make a public property called Result in your AzureAPI class And then instead of using a local variable, set your result to that.
Shinyshark
Shinyshark2y ago
put your public result here
Shinyshark
Shinyshark2y ago
above the method, under the class. you can type public string Result { get; set; }
itgel9462
itgel94622y ago
itgel9462
itgel94622y ago
Sorry. I got the same error as how I wrote before Public string Result;
Shinyshark
Shinyshark2y ago
What is the error in the AzureAPI? I reckon it will be related to the method being static.
itgel9462
itgel94622y ago
Shinyshark
Shinyshark2y ago
Remove the ' static' from your FromMic method.
itgel9462
itgel94622y ago
No errors after removing static
Shinyshark
Shinyshark2y ago
It's difficult to explain to you what static does
itgel9462
itgel94622y ago
Now is it accessible?
Shinyshark
Shinyshark2y ago
because it requires a bit of pre-knowledge. It should be yeah. If you call FromMic, it will set your Result. You can then grab your Result. It's hard for me to see what you want to do, but your Result issue is solved. If you want to learn a bit more, read into scopes and static. Static is often used by people as a solution for their issues when they do not understand their issue. In your situation, your editor likely told you to mark your method as static.
itgel9462
itgel94622y ago
Thank you so much*1000000
Shinyshark
Shinyshark2y ago
NatsumiThumbsUp
itgel9462
itgel94622y ago
Sorry to bother you again. If you have some time let me ask you again 😅 . If you have not, Don't force yourself. I tried to call the method From mic in main program. But still got a error. I changed speechConfig to Result and it didn't work. Why?
Shinyshark
Shinyshark2y ago
speechConfig is not set. You need a SpeechConfig object to pass along
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