Connecting Tech Pros Worldwide Forums | Help | Site Map

Object operations: C# vs. VB

Vik
Guest
 
Posts: n/a
#1: Jun 20 '06
I need to make some calculations, e.g. addition, on the objects x and y
which are numbers (of the same or different types) and may be DBNulls.

In VB, the following code is correct:
Public Shared Function XYZ(ByVal x, ByVal y)
XYZ = x + y
End Function

In C#, the identical code is wrong:
public static object XYZ(object x, object y) {
object z = x + y; //cannot add objects
return z;
}

Should I write the different procedures in C# for the different data types
or is there a better way?

Thanks.



Michael Cummings
Guest
 
Posts: n/a
#2: Jun 20 '06

re: Object operations: C# vs. VB


I ran across this blog entry today, which could help if you are using
generics.
http://weblogs.asp.net/cnagel/archiv...06/386147.aspx

Otherwise you'll need to set up type specific XYZ methods.

Michael Cummings
michaelc AT NOSPAM magenic DOT com
Magenic Technologies

"Vik" <viktorum@==yahoo.com==> wrote in message
news:uoN0l3IlGHA.4104@TK2MSFTNGP04.phx.gbl...[color=blue]
>I need to make some calculations, e.g. addition, on the objects x and y
>which are numbers (of the same or different types) and may be DBNulls.
>
> In VB, the following code is correct:
> Public Shared Function XYZ(ByVal x, ByVal y)
> XYZ = x + y
> End Function
>
> In C#, the identical code is wrong:
> public static object XYZ(object x, object y) {
> object z = x + y; //cannot add objects
> return z;
> }
>
> Should I write the different procedures in C# for the different data types
> or is there a better way?
>
> Thanks.
>
>[/color]


Marina Levit [MVP]
Guest
 
Posts: n/a
#3: Jun 20 '06

re: Object operations: C# vs. VB


That is because VB is a different language.

It has something called Option Strict. When it is off, VB does almost no
type safety checking. It will do something called late binding, which means
figuring out the data types of objects at run time, and casting them then.
This means you can have something like:

Dim obj as new Object()

obj.SomeFunction()

At run time, this will fail, since the Object class has no SomeFunction
method. However, this will compile with option strict off. And only at run
time will you get an error once the runtime figures out that there is no
SomeFunction class here. If 'obj' was pointing to an instance of a class
that had such a method, then it would be able to run it.

In general, I always recommend keeping Option Strict On. Generally, it will
allow you to catch errors at compile time rather then at run time.

"Vik" <viktorum@==yahoo.com==> wrote in message
news:uoN0l3IlGHA.4104@TK2MSFTNGP04.phx.gbl...[color=blue]
>I need to make some calculations, e.g. addition, on the objects x and y
>which are numbers (of the same or different types) and may be DBNulls.
>
> In VB, the following code is correct:
> Public Shared Function XYZ(ByVal x, ByVal y)
> XYZ = x + y
> End Function
>
> In C#, the identical code is wrong:
> public static object XYZ(object x, object y) {
> object z = x + y; //cannot add objects
> return z;
> }
>
> Should I write the different procedures in C# for the different data types
> or is there a better way?
>
> Thanks.
>
>[/color]


sloan
Guest
 
Posts: n/a
#4: Jun 20 '06

re: Object operations: C# vs. VB



As illuded to earlier:

If you put these 2 items at the top of each and every .vb (Vb.net class)
file you have:

Option Strict On

Option Explicit On



then that makes it much more like c# as it can get.






"Vik" <viktorum@==yahoo.com==> wrote in message
news:uoN0l3IlGHA.4104@TK2MSFTNGP04.phx.gbl...[color=blue]
> I need to make some calculations, e.g. addition, on the objects x and y
> which are numbers (of the same or different types) and may be DBNulls.
>
> In VB, the following code is correct:
> Public Shared Function XYZ(ByVal x, ByVal y)
> XYZ = x + y
> End Function
>
> In C#, the identical code is wrong:
> public static object XYZ(object x, object y) {
> object z = x + y; //cannot add objects
> return z;
> }
>
> Should I write the different procedures in C# for the different data types
> or is there a better way?
>
> Thanks.
>
>[/color]


Vik
Guest
 
Posts: n/a
#5: Jun 20 '06

re: Object operations: C# vs. VB


Thank you.

Are generics available in .NET 1.1?

The following code does not work (many errors on the function declaration):
using System;
public class GenericMethod {
public static E XYZ< E >( E x, E y ) {
E z = x + y;
return z;
}
}

Vik

"Michael Cummings" <michaelc@magenic.com> wrote in message
news:%23%234ORBJlGHA.1640@TK2MSFTNGP02.phx.gbl...[color=blue]
>I ran across this blog entry today, which could help if you are using
>generics.
> http://weblogs.asp.net/cnagel/archiv...06/386147.aspx
>
> Otherwise you'll need to set up type specific XYZ methods.
>
> Michael Cummings
> michaelc AT NOSPAM magenic DOT com
> Magenic Technologies
>
> "Vik" <viktorum@==yahoo.com==> wrote in message
> news:uoN0l3IlGHA.4104@TK2MSFTNGP04.phx.gbl...[color=green]
>>I need to make some calculations, e.g. addition, on the objects x and y
>>which are numbers (of the same or different types) and may be DBNulls.
>>
>> In VB, the following code is correct:
>> Public Shared Function XYZ(ByVal x, ByVal y)
>> XYZ = x + y
>> End Function
>>
>> In C#, the identical code is wrong:
>> public static object XYZ(object x, object y) {
>> object z = x + y; //cannot add objects
>> return z;
>> }
>>
>> Should I write the different procedures in C# for the different data
>> types or is there a better way?
>>
>> Thanks.
>>
>>[/color]
>
>[/color]


Peter Bromberg [C# MVP]
Guest
 
Posts: n/a
#6: Jun 20 '06

re: Object operations: C# vs. VB


If the objects may be numbers but of differenty types and especially if they
could be DbNull.Value then you are going to have to do some type checking in
the method. Above all, as mentioned by other posters, with Option Strict and
Option Explicit turned on as they always should be, your VB.NET method is not
equivalent to the C# one, it's signature should be

Public Shared Function XYZ(ByVal x as Object, ByVal y as Object)

-- to be equivalent to the C# version.

In general, I would have to agree that using VB.NET's weak GetObjectByValue
semantics to avoid strict type checking is almost never a good idea.

Peter



--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




"Vik" wrote:
[color=blue]
> I need to make some calculations, e.g. addition, on the objects x and y
> which are numbers (of the same or different types) and may be DBNulls.
>
> In VB, the following code is correct:
> Public Shared Function XYZ(ByVal x, ByVal y)
> XYZ = x + y
> End Function
>
> In C#, the identical code is wrong:
> public static object XYZ(object x, object y) {
> object z = x + y; //cannot add objects
> return z;
> }
>
> Should I write the different procedures in C# for the different data types
> or is there a better way?
>
> Thanks.
>
>
>[/color]
Michael Cummings
Guest
 
Posts: n/a
#7: Jun 20 '06

re: Object operations: C# vs. VB


No, generics are only available in .Net 2.0.
If you are using .Net 1.1, you'll need to use different methods for the
different types you need to support.

"Vik" <viktorum@==yahoo.com==> wrote in message
news:O676INKlGHA.3512@TK2MSFTNGP03.phx.gbl...[color=blue]
> Thank you.
>
> Are generics available in .NET 1.1?
>
> The following code does not work (many errors on the function
> declaration):
> using System;
> public class GenericMethod {
> public static E XYZ< E >( E x, E y ) {
> E z = x + y;
> return z;
> }
> }
>
> Vik
>
> "Michael Cummings" <michaelc@magenic.com> wrote in message
> news:%23%234ORBJlGHA.1640@TK2MSFTNGP02.phx.gbl...[color=green]
>>I ran across this blog entry today, which could help if you are using
>>generics.
>> http://weblogs.asp.net/cnagel/archiv...06/386147.aspx
>>
>> Otherwise you'll need to set up type specific XYZ methods.
>>
>> Michael Cummings
>> michaelc AT NOSPAM magenic DOT com
>> Magenic Technologies
>>
>> "Vik" <viktorum@==yahoo.com==> wrote in message
>> news:uoN0l3IlGHA.4104@TK2MSFTNGP04.phx.gbl...[color=darkred]
>>>I need to make some calculations, e.g. addition, on the objects x and y
>>>which are numbers (of the same or different types) and may be DBNulls.
>>>
>>> In VB, the following code is correct:
>>> Public Shared Function XYZ(ByVal x, ByVal y)
>>> XYZ = x + y
>>> End Function
>>>
>>> In C#, the identical code is wrong:
>>> public static object XYZ(object x, object y) {
>>> object z = x + y; //cannot add objects
>>> return z;
>>> }
>>>
>>> Should I write the different procedures in C# for the different data
>>> types or is there a better way?
>>>
>>> Thanks.
>>>
>>>[/color]
>>
>>[/color]
>
>[/color]


David Anton
Guest
 
Posts: n/a
#8: Jun 21 '06

re: Object operations: C# vs. VB


Not really.
Public Shared Function XYZ(ByVal x, ByVal y)
is just a shorthand in VB of writing
Public Shared Function XYZ(ByVal x As Object, ByVal y As Object) As Object

(yet another questionable 'feature' of VB...)
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter
Instant C++: VB to C++ converter


"Peter Bromberg [C# MVP]" wrote:
[color=blue]
> If the objects may be numbers but of differenty types and especially if they
> could be DbNull.Value then you are going to have to do some type checking in
> the method. Above all, as mentioned by other posters, with Option Strict and
> Option Explicit turned on as they always should be, your VB.NET method is not
> equivalent to the C# one, it's signature should be
>
> Public Shared Function XYZ(ByVal x as Object, ByVal y as Object)
>
> -- to be equivalent to the C# version.
>
> In general, I would have to agree that using VB.NET's weak GetObjectByValue
> semantics to avoid strict type checking is almost never a good idea.
>
> Peter
>
>
>
> --
> Co-founder, Eggheadcafe.com developer portal:
> http://www.eggheadcafe.com
> UnBlog:
> http://petesbloggerama.blogspot.com
>
>
>
>
> "Vik" wrote:
>[color=green]
> > I need to make some calculations, e.g. addition, on the objects x and y
> > which are numbers (of the same or different types) and may be DBNulls.
> >
> > In VB, the following code is correct:
> > Public Shared Function XYZ(ByVal x, ByVal y)
> > XYZ = x + y
> > End Function
> >
> > In C#, the identical code is wrong:
> > public static object XYZ(object x, object y) {
> > object z = x + y; //cannot add objects
> > return z;
> > }
> >
> > Should I write the different procedures in C# for the different data types
> > or is there a better way?
> >
> > Thanks.
> >
> >
> >[/color][/color]
Göran Andersson
Guest
 
Posts: n/a
#9: Jun 21 '06

re: Object operations: C# vs. VB


VB does automatic conversion to a data type that handles the largest
conceivable value for the operands. The equivalent C# code would be:

public static object XYZ(object x, object y) {
object z = Convert.ToDouble(x) + Convert.ToDouble(y);
return z;
}

To handle DBNull values you have to check if the type of each parameter
is DBNull before you try to convert them, and return an appropriate value:

public static object XYZ(object x, object y) {
if (x is DBNull) return DBNull.Value;
if (y is DBNull) return DBNull.Value;
return Convert.ToDouble(x) + Convert.ToDouble(y);
}

Vik wrote:[color=blue]
> I need to make some calculations, e.g. addition, on the objects x and y
> which are numbers (of the same or different types) and may be DBNulls.
>
> In VB, the following code is correct:
> Public Shared Function XYZ(ByVal x, ByVal y)
> XYZ = x + y
> End Function
>
> In C#, the identical code is wrong:
> public static object XYZ(object x, object y) {
> object z = x + y; //cannot add objects
> return z;
> }
>
> Should I write the different procedures in C# for the different data types
> or is there a better way?
>
> Thanks.
>
>[/color]
Closed Thread