❔ How to make a method becomes available for all form?
sorry I'm new at using method. I don't really understand about public method.
13 Replies
the public modifiers just makes your member accessible through anywhere, but to obtain it, you need a reference to that member, e.g passing it as argument of a method or make it static to make the reference available anywhere or store it in a field to make the reference available inside that instance.
Also you mix things up by mentioning "method" and "list" so the question is vague
so when im making a method i have to type static after the public?
first of all, what do you want to do? and what are you expecting? i do not understand completely of your intention
im trying to make highscore list for a game that i made. So when i lost, it will show you the highscore in the next form. My friend told me that i should just make a public method and call it in another form. Yet i dont know how to do it.
is this a winform app?
yeah
i see. honestly i am not an expert in a GUI application development but yeah if you want to call a method that can be accessed from anywhere and anytime, you can make the method a static method, but like i said, this may not be the best solution as i am not an experienced person in desktop development
however, marking a method as static will make it unable to access instance members (all members, fields or methods that are not marked with static within the class)
so should i just remove the static?
it depends. it seems you are a bit lost. I suggest for you to study more C# fundamentals such as what is the static keyword, how it works etc
alright imma try to study more
$static
In C#, static allows you to have members (classes, methods, etc) that are not tied to any particular instance and are therefore always, globally, accessible. When applying
static
members, take the following considerations:
• If there are to be multiple instances of a class, do not use static
• If you need to track state, do not use static
• If you are going to have multi threaded workflows, do not use static unless you're aware of the caveats
static
is best used for stateless methods, extension methods/classes and in areas where you understand the pros/cons. Read more here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/staticWas 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.