Computer
Computer

Reputation: 2227

Understanding some code

I have an application which was built a few years ago. I came across a section of code that baffled me as the functionality this provides throughout the ASP .Net application is great but i just dont understand it. Perhaps its the [] throwing me off but i think it could be some C# code converted to VB .Net.... Not sure but wondered if anyone understands this and if so could they share what its doing

The code in an NotInheritable class

Public Overloads Function [Get](Of B)() As B
    Dim myType = GetType(B)
    Return DirectCast([Get](myType), B)
End Function

I understand it overloads a function but

  1. Why are the [] there for and what do they mean? When would you use them? If i remove them i have a compiler error.
  2. Get in VB .Net is used in properties so is this some shortcut access to a property somewhere? Or
  3. where could i view which method its overloading?
  4. I've used code similar to List(Of Customer), IQueryable(of Customer) but how has (Of B) allowed in this manner?

I have read up on MSDN and researched around. The only thing that comes to mind is either some C# syntax conversion or some old VB6 syntax which the original developer must have used whilst creating the application.

Appreciate any clarification on this.

Upvotes: 0

Views: 75

Answers (2)

Mike Cheel
Mike Cheel

Reputation: 13116

1) Brackets allow you to use reserved words as identifiers (like the ampersand in c#).

2) It appears to be a bad naming decision. If they wanted to hide an existing member they could have used the Shadows keyword.

3) You'll need to examine the inheritance hierarchy. Start with the most recent parent.

4) It is calling a different overload of Get in the implementation but the Of B is trying to contrain it to B for some reason.

Upvotes: 0

Black Frog
Black Frog

Reputation: 11725

Because Get is part of Visual Basic Language Keywords. You need the bracket to indicate you want to use them as a method/property name.

Here is an excerpt from Microsoft on Keywords as Element Names in Code (Visual Basic):

Any program element — such as a variable, class, or member — can have the same name as a restricted keyword. For example, you can create a variable named Loop. However, to refer to your version of it — which has the same name as the restricted Loop keyword — you must either precede it with a full qualification string or enclose it in square brackets ([ ]), as the following example shows.

Upvotes: 4

Related Questions