Correct Conversion

M

Mick Walker

Hi all,

I am typically a VB.Net programmer, however a current project requires
me to work in C#. So I am attempting to better my understanding of C# by
converting an existing application. Im doing ok really, but the
following is bugging me.

What would the equivellent to this code be in C#:

For Each myDataRow as DataRow In myDataset.Tables(0).Rows
If myDataRow.Item("UserID") IsNot DBNull.Value Then
UserInfo.UserID = myDataRow.Item("UserID")
End If
Next

I tried using a online converter, but this gives me the following

foreach (DataRow myDataRow in myDataset.Tables(0).Rows) {
if (!object.ReferenceEquals(myDataRow.Item("UserID"), DBNull.Value))
{
UserInfo.UserID = myDataRow.Item("UserID");
}
}

This seems very long winded. Is this the correct way to do what I aim to do?

Regards
Mick
 
R

raylopez99

I would say, since you're still learning, that if it works, and
therefore ain't broke, don't fix it. Optimizing code, unless you
really know what you're doing, is the hobgobblin of little minds. Let
the compiler sort it out.

BTW IEnumerator class allows, like in VB, "for each myClassMember in
myClassSet" type terminology, which is intuitive, but I like using the
raw code behind IEnumerator myself--get a couple of sample codes and
tutorials on this and figure it out. You can add the two classes
needed by IEnumerator and make any of your classes "indexable" by
IEnumerator, to allow the "for each" language.

RL
 
G

Guest

That seems a little long-winded.
Instead, you should be able to do this (from Instant C#):

foreach (DataRow myDataRow in myDataset.Tables[0].Rows)
{
if (myDataRow["UserID"] != DBNull.Value)
{
UserInfo.UserID = myDataRow["UserID"];
}
}

--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, and C++
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
 
G

Gregg Walker

Mick,

This should do it...

foreach(DataRow myDataRow in myDataset.Tables[0].Rows)
{
if(!myDataRow.IsNull("UserID"))
{
UserInfo.UserID = myDataRow["UserID"];
}
}

hth
 

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