Static public variables in C #

4

How can I organize my code? When programming variables come up which I'm leaving in the same code at the top as:

namespace myData.MySql
{
    public partial class teste
    {
        public static string M_CPF_PROCURADOR;
        public static string strM_NirfFormatado;
        private static string m_datainicio = "";
        (...)

Would it be ideal for me to create a class and discriminate and call them when necessary? If so, could you tell me how to do it?

    
asked by anonymous 18.07.2016 / 21:39

1 answer

4

I start by saying that I am afraid that this partial is really necessary.

Static variables are of the class and not of the instance. That is, there is only one object in them, there can not be several. Is this what you want? It does not look like it, but I may be wrong, the passage I can not infer. If it is to have the data in each object created for this class, the variables can not be static.

If you need the variables accessible in various methods, you simply declare them as private. There is no reason to be public. They would only need to be public if there was a need for direct access to them out of class. That does not seem to be the case.

It is always better to use parameters than to access internal variables. But it is not always the best to do, it depends on each case. Ideally, low coupling , but we can not always or should not always do this (the cost may be too high).

I do not see a need to create another class according to what was demonstrated in the question, but it may be ideal if the concrete case determines something else that is not in the question. Cohesion is a good thing to achieve, but it can only be determined with sufficient data.

A identifier naming looks well outside of .Net. .

If the question is improved, I improve the answer. Ideally, you should have a concrete case. With an artificial code, everything can be valid.

    
18.07.2016 / 22:48