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

C# to VB translation problem

I have a little problem I'm not clear on. I program mostly in VB, I am
somewhat familiar with C#, and I've been told to translate a program. I was
under the general impression that since C# and VB are both Dot Net languages,
it was more or less possible to directly translate everything, aside from a
few individual language quirks.

In the original C# I have the following:
foreach (AccountInfo item in Accounts)
{
if (item.LastChangedBy != null)
{
... do some stuff here.
}
}

where LastChangedBy is an integer property of the object AccountInfo

My freeware translator has rendered this in VB as:
For Each item As AccountInfo In Accounts
If item.LastChangedBy IsNot Nothing Then

VS does not like this and gives an error: 'IsNot' requires operands that
have reference types, but this operand has the value type 'Integer'.

How would you translate this?
Aug 22 '08 #1
8 1208
On Aug 22, 8:18 am, B. Chernick <BChern...@discussions.microsoft.com>
wrote:
I have a little problem I'm not clear on. I program mostly in VB, I am
somewhat familiar with C#, and I've been told to translate a program. I was
under the general impression that since C# and VB are both Dot Net languages,
it was more or less possible to directly translate everything, aside from a
few individual language quirks.

In the original C# I have the following:
foreach (AccountInfo item in Accounts)
{
if (item.LastChangedBy != null)
{
... do some stuff here.
}

}

where LastChangedBy is an integer property of the object AccountInfo

My freeware translator has rendered this in VB as:
For Each item As AccountInfo In Accounts
If item.LastChangedBy IsNot Nothing Then

VS does not like this and gives an error: 'IsNot' requires operands that
have reference types, but this operand has the value type 'Integer'.

How would you translate this?
Can you verify the type of LastChangedBy? Even in C# you can't
compare an integer to null. You would get a similar compile error in
C# as well. LastChangedBy must be something other than integer.

Chris
Aug 22 '08 #2
B. Chernick wrote:
I have a little problem I'm not clear on. I program mostly in VB, I am
somewhat familiar with C#, and I've been told to translate a program. I was
under the general impression that since C# and VB are both Dot Net languages,
it was more or less possible to directly translate everything, aside from a
few individual language quirks.

In the original C# I have the following:
foreach (AccountInfo item in Accounts)
{
if (item.LastChangedBy != null)
{
... do some stuff here.
}
}

where LastChangedBy is an integer property of the object AccountInfo

My freeware translator has rendered this in VB as:
For Each item As AccountInfo In Accounts
If item.LastChangedBy IsNot Nothing Then

VS does not like this and gives an error: 'IsNot' requires operands that
have reference types, but this operand has the value type 'Integer'.

How would you translate this?
If LastChangedBy is indeed an int then item.LastChangedBy != null is
always true so you could simply remove the if check and only include the
block e.g.
foreach (AccountInfo item in Accounts)
{
{
... do some stuff here.
}
}

which then becomes

For Each item As AccountInfo In Accounts
... do some stuff here
Next

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 22 '08 #3
I guess that you have in c# a nullable int delclared: int? LastChangedBy

If so, then the property in vb should be declared as: Nullable(Of Integer)

"B. Chernick" wrote:
I have a little problem I'm not clear on. I program mostly in VB, I am
somewhat familiar with C#, and I've been told to translate a program. I was
under the general impression that since C# and VB are both Dot Net languages,
it was more or less possible to directly translate everything, aside from a
few individual language quirks.

In the original C# I have the following:
foreach (AccountInfo item in Accounts)
{
if (item.LastChangedBy != null)
{
... do some stuff here.
}
}

where LastChangedBy is an integer property of the object AccountInfo

My freeware translator has rendered this in VB as:
For Each item As AccountInfo In Accounts
If item.LastChangedBy IsNot Nothing Then

VS does not like this and gives an error: 'IsNot' requires operands that
have reference types, but this operand has the value type 'Integer'.

How would you translate this?
Aug 22 '08 #4
Actually no. The original local variable is declared:
private int _lastChangedBy = 0;
The property itself looks conventional.

However I should explain that the overall application is written using CSLA
2.1.4 and the programmer who wrote it just left on a long vacation without
leaving a scrap of documentation. Then management told me to translate it
using whatever freeware I could find. I barely knew CSLA existed before this
week. I love this job.

(Let me make a guess here. In the original C#, the class itself is declared
public class AccountInfo : Csla.BusinessBase<AccountInfo>
Could this have something to do with the ability of C# to test for nulls
with an integer property in this situation? But this won't translate to VB?)

"Family Tree Mike" wrote:
I guess that you have in c# a nullable int delclared: int? LastChangedBy

If so, then the property in vb should be declared as: Nullable(Of Integer)

"B. Chernick" wrote:
I have a little problem I'm not clear on. I program mostly in VB, I am
somewhat familiar with C#, and I've been told to translate a program. I was
under the general impression that since C# and VB are both Dot Net languages,
it was more or less possible to directly translate everything, aside from a
few individual language quirks.

In the original C# I have the following:
foreach (AccountInfo item in Accounts)
{
if (item.LastChangedBy != null)
{
... do some stuff here.
}
}

where LastChangedBy is an integer property of the object AccountInfo

My freeware translator has rendered this in VB as:
For Each item As AccountInfo In Accounts
If item.LastChangedBy IsNot Nothing Then

VS does not like this and gives an error: 'IsNot' requires operands that
have reference types, but this operand has the value type 'Integer'.

How would you translate this?
Aug 22 '08 #5
On 2008-08-22, B Chernick <BC*******@discussions.microsoft.comwrote:
Actually no. The original local variable is declared:
private int _lastChangedBy = 0;
The property itself looks conventional.

However I should explain that the overall application is written using CSLA
2.1.4 and the programmer who wrote it just left on a long vacation without
leaving a scrap of documentation. Then management told me to translate it
using whatever freeware I could find. I barely knew CSLA existed before this
week. I love this job.

(Let me make a guess here. In the original C#, the class itself is declared
public class AccountInfo : Csla.BusinessBase<AccountInfo>
Could this have something to do with the ability of C# to test for nulls
with an integer property in this situation? But this won't translate to VB?)
This code, if it is really an int is going to cause issues in C# as well.
It's a bug in the C# code. It would generate a compiler warning about the
expression always being true because an int can never be null.

The only way that test would work in reality is if the int was int?...
--
Tom Shelton
Aug 22 '08 #6
Try

For Each item As AccountInfo In Accounts
If item.LastChangedBy <DBNull.Value Then
...

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Aug 22 '08 #7
Looks like you're right. I compiled the original C# code and got a warning
for the same line stating that 'The result of the expression is always 'true'
since a value of type 'int' is never equal to 'null' of type 'int?'.

I'm going to treat this as a 'political' problem, if you know what I mean....

"Tom Shelton" wrote:
On 2008-08-22, B Chernick <BC*******@discussions.microsoft.comwrote:
Actually no. The original local variable is declared:
private int _lastChangedBy = 0;
The property itself looks conventional.

However I should explain that the overall application is written using CSLA
2.1.4 and the programmer who wrote it just left on a long vacation without
leaving a scrap of documentation. Then management told me to translate it
using whatever freeware I could find. I barely knew CSLA existed before this
week. I love this job.

(Let me make a guess here. In the original C#, the class itself is declared
public class AccountInfo : Csla.BusinessBase<AccountInfo>
Could this have something to do with the ability of C# to test for nulls
with an integer property in this situation? But this won't translate to VB?)

This code, if it is really an int is going to cause issues in C# as well.
It's a bug in the C# code. It would generate a compiler warning about the
expression always being true because an int can never be null.

The only way that test would work in reality is if the int was int?...
--
Tom Shelton
Aug 22 '08 #8

You could you use:
if item.LastChangedBy.HasValue Then
... do some stuff here.
end if

*** Sent via Developersdex http://www.developersdex.com ***
Aug 23 '08 #9

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

Similar topics

0
by: Matt | last post by:
Short Description: The Translation Hub is not picking up the TNS names file. I'm using Oracle 9ids. It doesn't work for any users and it has never worked since it's been installed. Long...
3
by: Dick Zeeman | last post by:
Hi, I have a problem regarding the perfomance of a stp in combination with character translation. The following happens. We have an automated installation script (nt command file) for...
3
by: Michael | last post by:
Our web application currently uses MSXML 4.0 to perform XSL translation of our XML document into HTML which is then delivered to the client's browser. By specifying the XSL file used for...
40
by: Chiwa | last post by:
Hey, Expression: Math.floor(x * 100) / 100 x= 4.1 gives 4.09, why in gods name? While other values for x don't give a problem. Thx in advance
7
by: Steven T. Hatton | last post by:
Is there anything that gives a good description of how source code is converted into a translation unit, then object code, and then linked. I'm particularly interested in understanding why putting...
1
by: Jean-Marc Blaise | last post by:
Hi, I've noticed that on my ESE v8.1 DB2 install on W2K (french), there is a translation error on the service: "Serveur DB2 remote command". In fact, all services start with "DB2", and have a...
2
by: James | last post by:
In oracle I can do the following: select CONVERT(COLUMNOFNAMES,'US7ASCII') from mytable -- It will take a name like Albrecht Dürer and change it into Albrecht Durer and is useful in where...
4
by: gw7rib | last post by:
If a class has more than one member function, is it possible for the code for one to be in one translation unit (ie file) and the code for another to be in a different translation unit? I would...
0
by: Stef Mientki | last post by:
thanks Stefan, both lxml and threading works perfect. One small problem, "with_tail" was not recognized as a valid keyword. cheers, Stef Stefan Behnel wrote:
1
by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post by:
I'm getting a little confused here. I have a C# class that I'm trying to translate to VB. The C# class is essentially: public static class Class1 { ..... some private static variables and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.