Instantiate class with static method

1
namespace Classes
{
    class Program
    {
        static void Main(string[] args)
        {
            Editora ed = new Editora();

            Program p = new Program();

            ed = p.EntradaDeDados();

            ed.InserirProvedorSql(ed);

        }

        public Editora EntradaDeDados() {
            Editora ed = new Editora();

            System.Console.WriteLine("Digite o nome da editora:");
            ed.Nome = System.Console.ReadLine();

            System.Console.WriteLine("Digite o E-MAIL da editora:");
            ed.Email = System.Console.ReadLine();

            ed.DataRegistro = DateTime.Now;

            return ed;
        }
    }
}

The Program class was automatically created next to the static void Main method. I made a method for entering data in this class. When I was calling this method in static void Main method, I had to instantiate the Program class inside itself. I found this very strange. I noticed this happens when it's a static method. Why does the static method force us to instantiate the class itself?

I modified the method Main from static to public , but gave error at the time of executing the project, said it does not contain static Main . Why is it mandatory to have a static method? I also removed that "string [] args" which I do not know what it is for.

    
asked by anonymous 11.03.2018 / 07:02

2 answers

7

There are some conceptual errors in the question and the AP comment on the other question is important, but on the contrary.

  

The Program class was automatically created next to the static void Main method

Learn to program, do not learn what any tool helps you. C # does not create anything, so work on this idea.

  

I made a method for entering data in this class

In real codes we do not usually do this, each with its own responsibility.

  

When I was calling this method in the static void Main method, I needed to instantiate the Program class inside itself

In the way it was done, it does.

  

I noticed this happens when it's a static method. Why does the static method force you to instantiate the class itself?

No, the problem is having a method that is not static . Static methods are available for the application, instance methods, which is the case of EntradaDeDados() , require that an instance be created. So the question does not fit.

  

I modified the Main method of static to public

public and static are not related, so you could have both, do not need to change.

Main() needs to be called by .NET, that is, it must be available to the application without instantiating anything. So it must be static. The Main() itself is not so special, you can determine the entry point of the application, but it always has to be static. Main() is an interesting convention.

This has all been answered in Why is the application entry point a static method? . So technically the question is a duplicate, but it has a small detail that is not in the question that makes it interesting.

  

I just do not know what to use for static

The correct question should be: why not use static ?

What do you get in not using static ?

The gain is that you can create instances of that, you can generate an object based on that model. In fact not only can, requires, after all if a member is available to the instance you can only access it by instance.

The simpler the better . Creating an instance without necessity does not make sense. It is true that in a real application this method would not even be there, but since it is, there is no reason why it should not be static . This is simple. Even in the right place it would only make sense in another context, it would have to be a class that handles the publisher's data entry and created it in a way that the instance would be more appropriate than the static form.

  

But I can access methods that are not static within static, I need to instantiate an object of the class that has this method

Power can, but for what?

One of the simplified example problems to learn is that it does not teach structuring an application. It is great for learning simple, timely concepts that many programmers ignore and this is to skip a step, so most programmers can not program, you are going in the right way, just do not think you are now learning to structure the whole application, This is a lot harder than people think. Learn the punctual first and the whole afterwards. Just keep in mind that while learning the punctual is not structuring correctly. This is a common mistake in beginners, see an example and think that is correct when in fact it is only a simplification to show the point being taught. Nor will I enter into the merit that there are those who do not know how to do and try to teach.

When you do something that requires an instance there we can help you this way, the solution to your problem at this point is to make the method static.

Instantiate class or use public methods?

    
11.03.2018 / 12:07
2

We can not modify the signature of the Main method, since it is special. It marks the starting point and is called automatically when we run our program. See more here .

Static methods do not force us to instantiate the class itself.

What happens is that you can not access instance members from within them, ie any variable or method that is not static, can not be accessed from a static method.

Otherwise, static members can be accessed normally from non-static members.

A less strange solution would be to make the EntradaDeDados() method static as well.

public static Editora EntradaDeDados() { /* ... */ }
    
11.03.2018 / 08:33