C
C#2y ago
SaltTonic

❔ 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
Bin
Bin2y ago
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
SaltTonic
SaltTonicOP2y ago
so when im making a method i have to type static after the public?
Bin
Bin2y ago
first of all, what do you want to do? and what are you expecting? i do not understand completely of your intention
SaltTonic
SaltTonicOP2y ago
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.
Bin
Bin2y ago
is this a winform app?
SaltTonic
SaltTonicOP2y ago
yeah
Bin
Bin2y ago
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)
SaltTonic
SaltTonicOP2y ago
so should i just remove the static?
Bin
Bin2y ago
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
SaltTonic
SaltTonicOP2y ago
alright imma try to study more when
Buddy
Buddy2y ago
$static
MODiX
MODiX2y ago
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/static
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