❔ 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
You want to make it a class field? Move it to the class body, like just before the method definition
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.
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.
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.
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
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 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.
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?
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.
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.
Is that clear to understand?
Sure, can you hover over the Result so I can see the error?
like this?
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.
put your public result here
above the method, under the class.
you can type
public string Result { get; set; }
Sorry. I got the same error as how I wrote before Public string Result;
What is the error in the AzureAPI?
I reckon it will be related to the method being static.
Remove the ' static' from your FromMic method.
No errors after removing static
It's difficult to explain to you what static does
Now is it accessible?
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.
Thank you so much*1000000
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?
speechConfig is not set.
You need a SpeechConfig object to pass along
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.