4

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.

w0051977
  • 15,099
  • 32
  • 152
  • 329

2 Answers2

2

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.

Community
  • 1
  • 1
Sinaesthetic
  • 11,426
  • 28
  • 107
  • 176
  • +1 for SO link, I think the answer there from Scott Rippey has a very valid reason. As per why it is required, its part of the language specification – Habib Jul 25 '12 at 17:42
  • I think your second link has answered my question. I am working on a project alone at the moment. If I was working in a team and other developers were using my interfaces, then I believe the parameter names would be useful to them. – w0051977 Jul 25 '12 at 17:45
0

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...

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • Thanks, I don't understand your point. The order of the integers would allow you to distinguish between them? – w0051977 Jul 25 '12 at 17:28
  • Thanks, but I still don't understand what you are trying to say. I realise the class implementing the interface would require local variable names. – w0051977 Jul 26 '12 at 22:39
  • The whole point of an interface is teh calss using doest need to know anything about how it's implemented. If you had a dll with int1 in it, and you were using it how would you which parameter was which without discovering what implemented it and looking at it. It would be no different to having no argument names in any method definition. – Tony Hopkinson Jul 27 '12 at 15:13