views:

1171

answers:

6
public static T GetValueOrDefault<T>(this IDataReader reader, string columnName)

 T returnValue = default(T);

Hello, I want to implement something like this

to check DBNull. I can follow the code fine but i don't quite understand what is static T is in VB.net. Can someone please explain a bit?

Thank

Jack

A: 

What are looking at is not "static T" but two separate parts.

  • public denotes the method as publicly visible.
  • static denotes the method as static. This means it runs for the class, not for an instance.
  • T - is the return type.

More information on static function.

Static functions in vb.net are know as shared functions.

More information on shared functions.

Adam Peck
I believe the asker wants to know the VB.Net equivalent to static T.
Samuel
Oh that was confusing as hell.
Adam Peck
Yeah, he could have definitely done a better job at wording his question.
Samuel
Yeah, there are atleast 3 questions being asked at once ;-)
Arjan Einbu
Hi, Sorry about the confusion.
Jack
A: 

C#'s static keyword is the same as VB's Shared keyword.

Arjan Einbu
A: 

He's creating an extension method. In C#, that's done by creating a static method (Shared in Visual Basic).

The mechanism for creating extension methods in Visual Basic appears to be much different than how you do it in C#. You'll probably want to read the MSDN entry about extension methods, here: http://msdn.microsoft.com/en-us/library/bb384936.aspx

Jim Mischel
A: 

T in your example is the type-parameter in your generic method.

In VB:

Public Function GetValueOrDefault(Of T)(ByVal reader as IDataReader, ByVal columnName as string) as T

Means that when you call the method, you supply a type-parameter (telling what type T will be for the call to the method)

Not sure about the VB syntax for creating an extension method, though. (This is what the "this" keyword on your first parameter denotes.)

Arjan Einbu
close, but you forgot "Public Function", and that the original is an extension method.
Joel Coehoorn
You're right. VB isn't my primary language... I'll fix it!
Arjan Einbu
+5  A: 

Hi,

The equivalent of Static in VB in Shared. Shared methods are usually put in Helper classes because they do not require an instance of the class to run.

The type T is indicates that this is a generic method (this is a new feature in VB9 and C# 3). A generic method effectively takes a type as an argument or returns a generic type.

Extension methods are also new in VB9/c#3, these allow you to extend an existing type by adding methods. All you need is a Shared method which is available in the same namespace as your code, in VB the code has to be in a module, not a normal class. A module is a class that can't be instantiated and (therefore) only has shared methods. It is declared with the Module keyword in place of the class keyword. Here is your code in VB.

(Also for those that know what's going on "under the covers" strangely setting a value type to nothing does compile in VB and is the supported way to get the default value of a value type).

Imports System.Runtime.CompilerServices 
<Extension()> _
Public Shared Function GetValueOrDefault(Of T)(ByVal reader As IDataReader, ByVal columnName As String) As T  
Dim returnValue As T = Nothing

End Function
Christopher Edwards
Very close, however you forgot to show the extension method syntax.
Joel Coehoorn
Yes, thanks for adding the forgotten attribute in the code snippet Joel.
Christopher Edwards
+1  A: 

This is what the method would look like in VB:

Imports System.Runtime.CompilerServices 

<Extension()> _
Public Shared Function GetValueOrDefault(Of T)(ByVal reader As IDataReader, ByVal columnName As String) as T
    Dim returnvalue As T = Nothing
End Function

I'm not sure how to do default(T) in VB so I left it out.

Nathan W
Just set it = Nothing
Joel Coehoorn
Thanks, I moved to C# a while ago and have forgotten everything about VB. I have fixed it up.
Nathan W