473,394 Members | 1,802 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,394 software developers and data experts.

Object operations: C# vs. VB

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.
Jun 20 '06 #1
8 3312
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:uo**************@TK2MSFTNGP04.phx.gbl...
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.

Jun 20 '06 #2
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:uo**************@TK2MSFTNGP04.phx.gbl...
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.

Jun 20 '06 #3

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:uo**************@TK2MSFTNGP04.phx.gbl...
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.

Jun 20 '06 #4
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

"Michael Cummings" <mi******@magenic.com> wrote in message
news:%2******************@TK2MSFTNGP02.phx.gbl...
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:uo**************@TK2MSFTNGP04.phx.gbl...
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.


Jun 20 '06 #5
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:
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.

Jun 20 '06 #6
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:O6**************@TK2MSFTNGP03.phx.gbl...
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" <mi******@magenic.com> wrote in message
news:%2******************@TK2MSFTNGP02.phx.gbl...
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:uo**************@TK2MSFTNGP04.phx.gbl...
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.



Jun 20 '06 #7
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:
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:
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.

Jun 21 '06 #8
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:
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.

Jun 21 '06 #9

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

Similar topics

5
by: Shu-Hsien Sheu | last post by:
Hi, Does the seek method would close the file object after using a for loop? My program looks like this: f = open('somefile', 'r') for lines in f: some operations f.seek(0)
18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
44
by: Steven T. Hatton | last post by:
This may seem like such a simple question, I should be embarrassed to ask it. The FAQ says an object is "A region of storage with associated semantics." OK, what exactly is meant by "associated...
6
by: Luke | last post by:
Here is my emails to Danny Goodman (but probably he is very busy so he didn't answered it). First email(simple): Subject: JavaScript Arrays " We all know the array can act like HashMap, but is...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
2
by: John Mullin | last post by:
We are having a problem which appears similar to a previous posting: http://groups.google.com/groups?hl=en&lr=&frame=right&th=d97f552e10f8c94c&seekm=OZw33z9EDHA.2312%40TK2MSFTNGP10.phx.gbl#link1 ...
2
by: Brad | last post by:
Receive the following error when stepping into the FILL (see last line). Being a newbie I cannot see anything obvious. Error message: An unhandled exception of type...
73
by: Water Cooler v2 | last post by:
I am new to PHP, just one day new. But I am otherwise a seasoned programmer on many other languages like C, VB 6, C#, VB.NET, Win32 platform and have some tiny bit experience in MFC, C++, Python. ...
5
by: TheSeeker | last post by:
Hi, I have run into something I would like to do, but am not sure how to code it up. I would like to perform 'set-like' operations (union, intersection, etc) on a set of objects, but have the...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.