Search code examples
vb.netcompiler-errorsvariable-assignmentassignment-operator

VB.NET: Operator '=' is not defined ... for a variable and object of THE SAME TYPE?


Okay, I am TOTALLY confused here. I have a class... say MyClass. It has several properties of another class of my type, say MyHelperClass (along with other properties).

I am doing the following:

Dim inst As MyClass = New MyClass() With {
    .p1 = sv1, 
    .p2 = sv2, 
    .h1 = getHelperClass(a1), 
    .p3 = sv3, 
    .p4 = sv4, 
    .h2 = getHelperClass(a2), 
    .p5 = sv5, 
    ...
    .pN = svN
}

*where .p# is some property, .sv# is some valid value. .h# is a property of type MyHelperClass and getHelperClass(a#) returns an instance of said class.

Now, I have the odd thing here, where the assignment statement for h1 works perfect. No problems. The assignment statement for h2 however, it is giving me the following blue-squiggle error:

Operator '=' is not defined for types myLib.MyHelperClass and myLib.MyHelperClass.

I just do not get this error at all! I don't even know where to start to figure this out. HELP!

201105.06 0305: The signature for h1's type is List(Of myLib.Address), where Address is a very basic class with typical address fields (name, address, city, state, zip, etc.). The return type of getHelperClass is also List(Of myLib.Address).

As SSS hinted at in his answer, I would expect = to not work in the "natural" way if I was using it for equality testing on a class without operators, however I am using it as an assignment operator, not equality, which I can't see any problem with. I am expecting the result of getHelperClass to be assigned to h2. But instead it's telling me = is not defined for the type. Is it possible that for some reason, the compiler is interpreting it as =(EQUALS) instead of =(ASSIGN)?

As for commenting out that line and it happening on the first one, I'll need to wait till I'm back in the office tomorrow to check that. Will report back.


Solution

  • Ah, yeah, sorry didn't read your OP properly. The assignment must be being misinterpreted as an comparison. Maybe you are missing a comma? For example in the statement "a = b = c" the first equals sign is an assignment, the second is a comparison.