How to return the name of an instance of a class?

Asked

Viewed 166 times

1

To avoid having to manually type the data after an update to the objects, I decided to create a method of type static to be accessed by passing the object as parameter.

    public static void Relatorio(Trem trem)
    { 

        Console.WriteLine($"{trem} - PESO ATUAL:       {trem.getPesoVagao()}");
        Console.WriteLine($"{trem} - PESO SUPORTADO:   {trem.getCargaMAX()}");
        Console.WriteLine($"{trem} - CARGA DISPONÍVEL: {trem.CargaRestante()}");

        Console.WriteLine();
        Console.WriteLine();
    }

Como aparece e como deveria aparecer

That would keep me from writing the report on Main() whenever you make a change to the object, but the returned name that should be "T2" is returning as SobrecargaDeMetodos.Trem.

I call the method as follows by passing the object to be analyzed as parameter. The part showing the result works perfectly, but the name does not.

Trem.Relatorio(T2);

2 answers

7

This is a misconception. First there is no instance name, there is variable name. It may even be that in this case a name is equal to the variable name, but it cannot always represent any name in a variable name. But there is also a misunderstanding of what is a variable. Variables are local and they don’t go from one context to another, so what you want doesn’t make sense, actually what you’re printing isn’t even what you’re imagining. The solution to this very simple:

public static void Relatorio(Trem trem, string nome) {
    WriteLine($"{nome} - PESO ATUAL:       {trem.getPesoVagao()}");

There it calls:

Trem.Relatorio(T2, "T2");

or if you think you can change that name (which seems to me something strange, but anyway, I will not discuss other possible mistakes of this design), can even use:

Trem.Relatorio(T2, nameof(T2));

I put in the Github for future reference.

This only ensures that if the variable name is changed it gives a build error in the passage of the string, so make you change your name there too.

Maybe there are other misconceptions in this.

-4


Another option is to add a property Nome to your class Trem. That way all the trains would have a Nome

public class Trem{
    public string Nome{get; set;}
    //...
}

And every time you create a Voce train you name it. var t = new Trem(){Nome = "t2"};. And in your report you would use your property:

Console.WriteLine($"{trem.Nome} - PESO ATUAL:       {trem.getPesoVagao()}");
//...
  • I think this is a lot of gambit to solve the problem, a property has to be something that belongs to the object. Looking at the code without looking at the problem, I understand that this property stores the Nome of a Trem.

  • @Robertodecampos I do not understand the reason. In my view it may even be one of the best options to solve the problem. Unless it doesn’t make any sense that the trains have a name.

  • So that’s the question, he doesn’t want to get the name of the train, but to get the name of the variable that stores the object Trem.

  • @Robertodecampos But this concept does not even make sense, since the variables may have the identifiers I want. Once the variable can be called trem and other times t2. I can also have several variables that point to the same object.

  • Exactly, it doesn’t make sense, but that’s exactly what @Ramonalmeida is asking! The question itself is probably already a gambit.

  • @Robertodecampos. I will leave my answer even, It continues to seem to me that it is a logical way to solve the problem, even if it is not "even to the letter" to the situation indicated by the AP.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.