0

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.

2 Answers2

1

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.

hbulens
  • 1,872
  • 3
  • 24
  • 45
0

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.

displayName
  • 13,888
  • 8
  • 60
  • 75
  • Tanks you for the nice input. I started watching some videos from MVA, hoping it will give me a clue about the way its done, tho i must admit i find OOP kinda hard to put into the context of my code. I can understand it with animals(witch seem to often be the examples in these videos) :) – Nicolai Svendsen Sep 20 '15 at 17:48
  • @NicolaiSvendsen: These are two of my favs. Your knowledge will make a big jump if you can get your hands on them. 1) http://www.lynda.com/Programming-tutorials/Foundations-Programming-Object-Oriented-Design/96949-2.html 2) http://www.lynda.com/Developer-Programming-Foundations-tutorials/Foundations-Programming-Refactoring-Code/122457-2.html Disclaimer: I'm not connected with Lynda.com in any way. – displayName Sep 20 '15 at 17:53
  • @NicolaiSvendsen: If you are ok with reading a book - Code Complete 2 by Steve McConnell will permanently dent (for the better) the way you code. – displayName Sep 20 '15 at 18:04