4
I’m having difficulties p/ retrieve an object to get a specific attribute.
I want to overwrite the method Equals
in my class, to compare whether the title of objects are identical. In this case, it does not matter whether the type and other attributes are different. I just want to compare the attribute Title
of both are equal.
My code is like this:
namespace Kitty.Core.Blocks
{
class Block<T>
{
public string Title { get; private set; }
public T Contents { get; private set; }
public Block(string title, T contents)
{
this.Title = title;
this.Contents = contents;
}
public override bool Equals(object obj)
{
Block<?> other = obj; // ?
return string.Equals(this.Title, other.Title);
}
}
}
The problem is in this section:
public override bool Equals(object obj)
{
Block<?> other = obj; // Como recupero "obj"?
return string.Equals(this.Title, other.Title);
}
I tried so:
Block<?> other = (Block<?>) obj;
And so:
Block other = (Block) obj;
But I’m forced to specify the type:
Using the Generic type '
Block<T>
' requires 1 type Arguments.
As I am overwriting, it is no use to change the method signature and exchange object
by the same class name.
So how can I get that back obj
, keeping its generic type (which I don’t know what it will be) to compare only the attribute Title
of both?
That’s right, I was reading about
IEquatable
but it won’t be necessary, I didn’t know theas
. About namespace, you meant that instead ofnamespace Kitty.Core.Blocks
would only benamespace Blocks
?– Renan Gomes
Could, or could be
KittyCoreBlocks
. Note that I’m not saying that you can’t use it with the dots, or even that you shouldn’t make more sense. It depends on what you want. It may be that I need a clear separation, I have no way of knowing this. It’s just a semantic question of the code, it doesn’t change anything to language. What I mean is that Java "teaches" the programmer to dot the package name and C# encourages you to avoid this unless you have a reason. For example this is an excrescence in C#:br.org.empresa.produto.alguma.coisa
. Not the point as if it were space or underline.– Maniero
Thank you so much for the recommendations. About the answer: I tested with equal titles but using a different type of
string
, the result wasfalse
: https://dotnetfiddle.net/JgY1H6 I missed something?– Renan Gomes
And this behavior seems correct to me, doesn’t it? There are two questions. The equality test of a list, either by the operator, or by the Equals method()
, vai testar se a referência é a mesma e não se o conteúdo é igual. Se precisa de outro comportamento, é um problema do objeto usado e não desta classe. Se não pode aceitar isto teria que fazer uma restrição nos tipos que ´T
can accept (withwhere
) or even spread with aif
(is not ideal, but there is case that is not very good). In the example you compared a string with a list. It’s so different (up to type). You expect it to givetrue
?– Maniero
So, but I compared only the field
Title
, that of both is a string, isn’t it? What I’m trying to do is: compare only the fieldTitle
of both, the type of<T>
does not matter in this comparison. For example:title: foo, contents: string
would equaltitle: foo, contents: lista de string
. It is possible to do this on one’s ownEquals
or I would have to retrieve the title of each block later and make the comparison byif(bloco1.Title == bloco2.Title){}
?– Renan Gomes
It’s true. I actually realized that there’s an algorithm problem that I hadn’t even questioned, so I activate the code by wanting to do what I wanted. If the comparison is always with the
Title
and he’s alwaysstring
, that cast forBlock<T>
doesn’t make sense. It would have to be forstring
. If I understand correctly it is the only necessary change, if I conform I can change it (even if it misrepresents the original intention of the question).– Maniero
It’s just that I’m learning c# now and maybe I’m confused about how to ask. I’d like a solution to that if possible :)
– Renan Gomes
I actually wrote some nonsense there in the comment. But I will edit the answer to show that this behavior seems to be the right one. It doesn’t make sense about the list being different, because it doesn’t compare lists, the cast also needs to be done pq is done on the object and not in the field. Works if the type is the same: https://dotnetfiddle.net/cMB4AZ
– Maniero
I think I understand why it doesn’t work. Is that my Java method is written like this. I thought it was possible to do something similar in C#.
– Renan Gomes
I do not know if I understood. The codes were not equivalent. But what I did left equivalent. At least I think, from what I understood yours would give the same result as mine, after all if the generic type is different, you’re saying to return
false
explicitly. In C# no need because the cast withas
failing will not generate an exception. But if you want to make a port identical to Java, you can do it (it’s written in the answer), I just find a less elegant solution. It is interesting to put the check of thenull
in C# code as well. If you pass 1 null to the method, you will give equal exception to Java would– Maniero
Really the problem was that I was checking different guys, it was bad for taking your time. I had seen the wrong test class in Java, so I was trying to do this madness of comparing different objects.
– Renan Gomes
No way. We all learned something here.
– Maniero