How to know if there is content in a class object?

Asked

Viewed 1,314 times

0

I need to know if I have content in a class or if it’s empty, how do I get that information?

Classe usuario = new Classe();

I wish I could consult if there’s content in this class of mine usuario. In Delphi there is something like Count for example.

  • 1

    Looking at her?

  • You want to know methods, properties ?

  • Class is a prototype. You know nothing about it until it is instantiated into an Object. To understand why your question does not make sense, consider "class" as the floor plan of a house and you want to know if there is someone in the room and the house has not even been built yet...

  • Sorry, I’m still a beginner, but I modified the question to see if it helps.

  • 1

    You can check if the object was instantiated by checking if it is different from null

  • 2

    usuario != null ????

  • @Deividsouza you want to know if there is content in some object property?

  • Jean, Orge and Maniero answered my question, but I’m going to ask a question in the @Maniero answer because I tested it and it didn’t work as expected with the list. Thanks.

Show 3 more comments

1 answer

3


Let’s set things right. What you want is to know if you have content in the object and not in the class. Class is just a model.

You can check if the object is null:

if (usuario == null)

Otherwise it depends on the object. It may be that the object has content is empty, but not null, but depends on the object semantics to know how to verify this.

For example, you say you have a class Usuario with a property called Ativo that is bool can you verify:

if (usuario.Ativo)

If it is a array or a list (any enumerable) is possible to use the Count you’re looking for:

if (lista.Count() > 0)

assuming you know that lista is not null, otherwise it would be better:

if (lista != null && lista.Count > 0)

But it is almost certain that a Usuario it’s not a list, and if it is, there’s something very wrong there.

If it is a string may be that it works a check if it is different from empty:

if (texto != "")

or

if(!string.IsNullOrWhiteSpace(texto))

I put in the Github for future reference.

First you need to know what you’re dealing with and then find a solution.

Although the methods are different, in essence it is equal to Delphi. There is no universal solution.

  • I think the AP is wondering if there are values in the properties of the class type object. Maybe I may have misinterpreted his point, but that seems to be it.

  • Maybe, but then only he can say.

  • It was exactly what I needed, again sorry for misexpressing, the lack of knowledge if (lista.Count > 0) and he gives me the following return: O operador ">" não pode ser aplicado a operandos dos tipos "grupo de métodos" e "int". Note: My list have used with Task.

  • 1

    @Deividsouza I had forgotten the parentheses.

  • Finally, Classe and Lista with Task worked now, thank you.

Browser other questions tagged

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