Reputation: 57
whats the correct way to use public functions/variables? I mean should i create a new class filled with everything that is public? like a PublicFunctionsAndVariables
class?
namespace TestProgram
{
class PublicMethodsAndVariables
{
public int SomeVar;
public float TimesPI(float number){
float result;
result = number*3.14159265359;
return result;
}
}
}
I have already made a class, but don't know if that is the convention in c#. The above is just a quick example i made.
I have a win form application where in different forms i need to use some of the same code, like a function that filter search results etc.
Thanks in advance.
Upvotes: 0
Views: 118
Reputation: 14379
If you will follow the right path of arriving at an Object Oriented design, you will not have to create new classes for public members. All of your classes will have 0 or more public and private members naturally.
Does any object of type PublicFunctionsAndVariables
exist in your application in reality? I'm quite sure, No. It is like saying that this particular object has nothing private. All other entities can access everything this object has. And that is totally off the direction of how "Object"-oriented design should be. Object oriented design should be - oriented towards defining actual "Objects".
You are trying to not define, but classify a class wrt its level of privacy. Instead, the classes are defined according to what they really do. If some of their actions are of the type that nobody needs to know about them, they will be private. Their actions which are to be let known by anyone else interacting with them, should be public/protected.
Upvotes: 0
Reputation: 1969
The general concept is to keep your variables, properties, methods, ... as private as possible. Only those elements that require access from outside your class should be considered public. But if you have too many public properties and methods, you're probably not doing it right (unless you are creating a helper or utility class). So I suggest you grab yourself a book and start reading, the Head Start C# book is really comprehensible.
Upvotes: 1