Object operations: C# vs. VB

V

Vik

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.
 
M

Marina Levit [MVP]

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.
 
S

sloan

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.
 
V

Vik

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
 
G

Guest

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
 
M

Michael Cummings

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.
 
G

Guest

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
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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);
}
 

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