C# to VB translation problem

B

B. Chernick

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?
 
C

Chris Dunaway

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
 
M

Martin Honnen

B. Chernick said:
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
 
F

Family Tree Mike

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

B. Chernick

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?)
 
R

Rich P

Try

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

Rich
 
B

B. Chernick

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

xap xap

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

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

Similar Threads


Top