w0051977
w0051977

Reputation: 15817

.NET Interface signature

Is there a reason why you have to specify variable names in interfaces. For example, look at the code below:

 Public Class Class1
        Public Function Test(ByVal j As Integer)

        End Function End Class

    Public Interface int1
        Function Test(ByVal i As Integer)
      End Interface

The integer in the class is named j, but it is named i in the interface. Why wouldn't the interface be like this:

Public Interface int1
            Function Test(Integer)
          End Interface

I realise this is a basic question. I just find it curious.

Upvotes: 4

Views: 156

Answers (2)

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

Well one reason would be if you had

Public Interface int1
Function Test(Integer,Integer)           
End Interface 

How would you know when calling Test from an int1 variable, which integer was what....

Basically the compiler itself doesn't care about the argument name, we almost always do though.

After the comment.

Lets say you have two implementations of int1

Imp1.Test(A,B) and Imp2.Test(B,A)

You've done

Dim myInt1 as Int1

...

...

myInt1.Test( 

and now you are stuffed aren't you?. You'd have to test myInt1 to see if was an imp1 or an imp2, so the interface is a total waste of time...

Upvotes: 0

Sinaesthetic
Sinaesthetic

Reputation: 12211

At the very least, in 2.0, if the override didn't match the interface signature, then you weren't technically implementing it http://msdn.microsoft.com/en-us/library/ms182251(v=vs.80).aspx

I'm not sure about it nowadays. And as for why? I dunno. Are you coming from another language? If I recall right, other language header files only required the type in the signature but not a name.

Possible dupe of Why do we have to name interface method parameters? -this explains a couple other reasons that you might encounter.

Upvotes: 2

Related Questions