VB equivelant for C#'s default(T)

C

cougaristic

I am trying to convert an C# application to VB and have one issue which is
converting the generic value to the default.
C# uses return default(T) as the return value how would I translate this in
VB.Net

Thanks
 
D

David Anton

'Nothing'.
This keyword is more general than the C# 'null', handling reference types,
value types, and generic types.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++/CLI
 
M

Mythran

David Anton said:
'Nothing'.
This keyword is more general than the C# 'null', handling reference types,
value types, and generic types.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++/CLI

From what I've read, default(T) just gets the default value of the type.
So, in Visual Basic, as David Anton explains before me, the value of
"Nothing" should accomplish this same goal (reference types have a reference
to null (or Nothing in VB.Net) by default. Value types, when you set them
to Nothing, will be set to their default types (although, I think you
shouldn't be able to set a value type to Nothing....but that's just my
opinion :D ).

HTH,
Mythran
 
C

cougaristic

Just to learn more about this situation I would like to know how to handle
the following to return the 'actual' default for the generic type used.

When I am using Guid as the generic value like the following:
GetMessageHeader(Of Guid)(message,name,ns) it currently returns nothing if
the else statement is reached? Wouldn't the default of a guid be a guid type
containing all zeros?

Here is the converted code in question:

Public Shared Function GetMessageHeader(Of T)(ByVal message As Message,
ByVal name As String, ByVal ns As String) As T
Dim index As Integer = message.Headers.FindHeader(name, ns)
If (index > -1) Then
Dim val As T = message.Headers.GetHeader(Of T)(name, ns)
Return val
Else
'The following is what this question is all about
'C# = default(T)
Return Nothing
End If
End Function

Thanks
 
K

Kerry Moorman

cougaristic,

Isn't a guid an integer? Then assigning Nothing to it will result in the
default value for an integer, which is all zeroes.

Kerry Moorman
 
C

cougaristic

For the sake of argument why should I always believe that the default for
anything is always null/or nothing?
If I was using a specialized business object for the generic object and it
had a default string field named MyDefault with a default of 'This is the
default value' then why should the method return null/nothing instead of
returning the value 'This is the default value'?

This is just a question to help me understand what the real difference is
between the default(t) vs returning Null/Nothing. If default(t) always
returns null/nothing as shown in my example code then why not just return
null/nothing all of the time?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top