Question menu - non static or static method?

WB1975

Well-known member
Joined
Apr 3, 2020
Messages
87
Programming Experience
Beginner
If im going to make a menu for my utility

should i have a non static method to print the menu
and a static method to handle the logic?
or would you just put it all in one method, or does it not really matter?
 
Writing methods that has single purpose is a good approach to object oriented programming. Printing a menu sounds like a good candidate for a single purpose method.

As for static the general idea is that static is something that is common for all instances of a type. Writing code based off the static Main method for example in a Console application forces you to use other static members, unless you wrap the whole thing in a new class and use an instance of that in Main method.
 
" As for static the general idea is that static is something that is common for all instances of a type. Writing code based off the static Main method for example in a Console application forces you to use other static members, unless you wrap the whole thing in a new class and use an instance of that in Main method."

I'm really sorry John, I dont understand this too well...

"As for static the general idea is that static is something that is common for all instances of a type." i thought static was to be able to use something without having to create an instance of it

"Writing code based off the static Main method for example in a Console application forces you to use other static members, unless you wrap the whole thing in a new class and use an instance of that in Main method." this i understand a bit better, yes you cant just use a method in the same class from main, you need to instantiate an object then call its methods from that, seen it before.

Im just not getting an answer to my question from this, sorry again John.
 
thought static was to be able to use something without having to create an instance of it
Yes, that is correct, that also means that something that is static is shared for all instances. In VB the keyword Shared is used instead of static actually.
 
Yes, that is correct, that also means that something that is static is shared for all instances. In VB the keyword Shared is used instead of static actually.

right but this is discouraged as much as possible? Static should only be used in very specific cases?
 
That is true, but in console application that naturally runs directly off Main you have no choice but to use other static members - or work against a class instance.
 
With regards to declaring members static, I think that there are two ways to approach it:

1. Make all members instance members by default and only declare a member static if you have a specific reason to do so.

2. Make all members static by default and only make them instance members if you have a specific reason to do so.

In the first case, you'll end up with very few static members for the most part, although probably more in a Console app for the reason that @JohnH mentioned. In the second case, you'll probably still end up with a majority of instance members.
 
Back
Top Bottom