473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

VB equivelant for C#'s default(T)

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
Sep 8 '08 #1
7 4219
'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
"cougaristic" wrote:
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
Sep 8 '08 #2


"David Anton" <Da********@discussions.microsoft.comwrote in message
news:BD**********************************@microsof t.com...
'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
"cougaristic" wrote:
>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
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
Sep 8 '08 #3
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

"Mythran" wrote:
>

"David Anton" <Da********@discussions.microsoft.comwrote in message
news:BD**********************************@microsof t.com...
'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
"cougaristic" wrote:
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

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

Sep 8 '08 #4
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
"cougaristic" wrote:
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

"Mythran" wrote:


"David Anton" <Da********@discussions.microsoft.comwrote in message
news:BD**********************************@microsof t.com...
'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
>
>
"cougaristic" wrote:
>
>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
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
Sep 8 '08 #5
How do you know it returns Nothing ?

I tried :

Class Program
Class Test
Shared Function GetDefaultValue(Of t)() As t
Return Nothing
End Function
End Class
Shared Sub Main()
MsgBox(Test.GetDefaultValue(Of Guid).ToString)
End Sub
End Class

And it looks like it returns an empty GUID.

IMO your best bet would to exoplain us what is the effect you have that
makes you think the returned value is not correct. Have you tried to check
IsNothing(MyValue) ?

--
Patrice
"cougaristic" <co*********@discussions.microsoft.coma écrit dans le
message de groupe de discussion :
F1**********************************@microsoft.com...
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

"Mythran" wrote:
>>

"David Anton" <Da********@discussions.microsoft.comwrote in message
news:BD**********************************@microso ft.com...
'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
"cougaristic" wrote:

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

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

Sep 9 '08 #6
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?

"Patrice" wrote:
How do you know it returns Nothing ?

I tried :

Class Program
Class Test
Shared Function GetDefaultValue(Of t)() As t
Return Nothing
End Function
End Class
Shared Sub Main()
MsgBox(Test.GetDefaultValue(Of Guid).ToString)
End Sub
End Class

And it looks like it returns an empty GUID.

IMO your best bet would to exoplain us what is the effect you have that
makes you think the returned value is not correct. Have you tried to check
IsNothing(MyValue) ?

--
Patrice
"cougaristic" <co*********@discussions.microsoft.coma crit dans le
message de groupe de discussion :
F1**********************************@microsoft.com...
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

"Mythran" wrote:
>

"David Anton" <Da********@discussions.microsoft.comwrote in message
news:BD**********************************@microsof t.com...
'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
"cougaristic" wrote:

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

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

Sep 9 '08 #7
For the sake of argument why should I always believe that the default for
anything is always null/or nothing?
This is just a notation ie. :

Dim i As Integer=Nothing

will just affect the default value for the integer datatype which is 0. Note
that the actual value for the i variable is *NOT* "Nothing" but 0 which is
the default value for integers. Actually my personal preference would be to
avoid this confusing notation (you could have Dim DefaultValue As T and
later Return DefaultValue in your code, it will return the default value
without using the confusing Nothing notation). In particular it certainly
doesn't apply to C#. This is just how VB handles Nothing affectation...
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'?
Which method ? Generally this kind of default value is defined using an
attribute and could be retrieved by reading this attribute (for example this
is done for default values when creating controls whose values are set using
the property grid dialog). Here we are talking about the language default
value for a particular type that knows only about the type 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?
Not sure what you meant. My code was confusing ? It was to show that for the
GUID type returning Nothing will actually produce an empty GUID which is the
default value for this type. So the code (and your code) works as you wish ?

So IMO you should restart fresh . For now my understanding is that you just
don't like to translate "default (T)" by "Return Nothing" because you think
that the GUID value would be then "Nothing" rather than an empty GUID. This
is just not the case.

You won't have this problem. Affecting Nothing to a variable just set the
variable to its default value (which is not necessarily the "real" Nothing
value).

If you run your code, does it produce the result you want ? (my
understanding is that the problem is the confusing Nothing affectation that
doesn't really affect "Nothing" the value but whatever default value for the
assigned variable).
--
Patrice



Sep 9 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Richard | last post by:
What is the equivelant thing in php for the javascript onclick event? Specifically, when a visitor clicks on an image.
3
by: Alex Hunsley | last post by:
Pretty simple seeming question, but can't find answer via google or docs... I am using urllib2 as follows: handle = urlopen(req, postdata) # and URL to return a handle on ... print...
0
by: hsifelbmur | last post by:
does anyone know a product equivelant to MS SQL Server Reporting Services, but for MySQL or PostGreSQL?
2
by: Chris LaJoie | last post by:
I'm converting some APIs to C# and I'm wondering what the equivelant of a DWORD is. I'm thinking it's a uint, but I'm uncertiain. Can anyone confirm that? Chris
2
by: ad | last post by:
There are InStr in VB What is the C# equivelant of the function InStr in VB? ---------------------------------------------------------------------------- ------------------ SearchString...
47
by: Nathan Sokalski | last post by:
VB.NET has a function, Asc(), that gets the Ascii value of a character. I am attempting to find an equivelant function for C#. Can somebody help me here? -- Nathan Sokalski njsokalski@hotmail.com...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.