Andrew H
Andrew H

Reputation: 463

Converting c# to vb.net. What to do with comparison operators and Interfaces?

I am converting a humongous project to vb from c#. One of the issues I've come across is that in c#, I can do things like this:

    private ITreeModel _model;
    [Category("Data")]
    public ITreeModel Model
    {
        get { return _model; }
        set
        {
            if (_model != value)
            {
                if (_model != null)
                    UnbindModelEvents();
                _model = value;
                CreateNodes();
                FullUpdate();
                if (_model != null)
                    BindModelEvents();
            }
        }
    }

Specifically, I can:

If _model != value Then

However, because the property is an Interface, when I attempt to convert this to vb.net, it tells me I cannot use the <> operator. I even overloaded the = and <> operators in all the classes that implement ITreeModel, but to no avail.

I cannot use 'Is', as the compiler wants me to do, because of COURSE it is implementing the ITreeModel interface, otherwise it would never get to this property! It is asking if the two are not equal, and overloading the = and <> does not make the error go away.

Any thoughts from the geniuses here on how I can address this issue? I am looking for the equivalent code in vb.net that will accomplish the same thing the c# code is...

To clear up any possible confusion. The C# code tests for equality. It is asking, 'are these two things equal?' I realize that I can do a a.Equals(b), but I am not sure if this is what the c# code is doing, because != might mean different things in c#.

Upvotes: 3

Views: 178

Answers (2)

shadow
shadow

Reputation: 1903

== Operator (C# Reference)

For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings.


Is Operator (Visual Basic)

Compares two object reference variables. The Is operator determines if two object references refer to the same object. However, it does not perform value comparisons. If object1 and object2 both refer to the exact same object instance, result is True; if they do not, result is False.

Is can also be used with the TypeOf keyword to make a TypeOf...Is expression, which tests whether an object variable is compatible with a data type.


Object.Equals Method (Object)

If the current instance is a reference type, the Equals(Object) method tests for reference equality, and a call to the Equals(Object) method is equivalent to a call to the ReferenceEquals method. Reference equality means that the object variables that are compared refer to the same object

... You can compare the current object to another object for reference equality by calling the ReferenceEquals method. In Visual Basic, you can also use the Is keyword (for example, If Me Is otherObject Then ...).


So you see that these two operators do the same thing. So the answer to your question would be something like this:

Public Interface ISomeInterface

    End Interface

    Public Class SomeObject
        Private _someInterface As ISomeInterface



        Public Property SomeInterface As ISomeInterface
            Get
                Return _someInterface
            End Get
            Set(value As ISomeInterface)
                If (Not _someInterface Is value) Then
                    If (_someInterface IsNot Nothing) Then
                        DoSomething()
                    End If

                    _someInterface = value

                    If (_someInterface IsNot Nothing) Then
                        DoSomethingElse()
                    End If
                End If
            End Set
        End Property



        Public Sub DoSomething()

        End Sub

        Public Sub DoSomethingElse()

        End Sub
    End Class

Upvotes: 2

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239674

You can switch the logic the other way around and just exit early from the property:

If ReferenceEquals(_model,value) Then Return

(And, of course, the most direct part of the answer - use ReferenceEquals for your comparison)


Although I'm not sure what your point about Is was in your question. The following compiles just fine also:

Public Interface IDoSomething
    Sub DoNothing()

End Interface
Public Class Class1
    Private _zyx As IDoSomething
    Public Property Xyz As IDoSomething
        Get
            Return _zyx
        End Get
        Set(value As IDoSomething)
            If value Is _zyx Then Return

            If _zyx IsNot Nothing Then
                _zyx.DoNothing()
            End If
            _zyx = value

            If _zyx IsNot Nothing Then
                _zyx.DoNothing()
            End If
        End Set
    End Property

End Class

Upvotes: 1

Related Questions