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

Correct Conversion

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
Aug 14 '07 #1
3 1242
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

On Aug 14, 9:27 am, Mick Walker <mick.wal...@privacy.netwrote:
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

Aug 14 '07 #2
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
"Mick Walker" wrote:
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
Aug 14 '07 #3
Mick,

This should do it...

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

hth
--
Gregg Walker
Aug 14 '07 #4

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

Similar topics

6
by: David Opstad | last post by:
I have a question about text rendering I'm hoping someone here can answer. Is there a way of doing linguistically correct rendering of Unicode strings in Python? In simple cases like Latin or...
1
by: amitbadgi | last post by:
I am gettign this error, while migration an app to asp.net Exception Details: System.FormatException: Input string was not in a correct format. Source Error: Line 19: Dim enddate =...
1
by: Marek Lewczuk | last post by:
Hello, I would like to ask if my problem with sequence is a proper behavior or this is a bug (probably not)... I have a table: CREATE TABLE "testtable" ( "serialfield" SERIAL,...
12
by: Jim Langston | last post by:
I have a template I call StrmConvert, which uses std::stringstream to convert from any type to any other type that can be used by stringstring. This is what it looks like: template<typename T,...
10
by: lovecreatesbeauty | last post by:
Is parameter type conversion required for the 2nd argument on printf("%p", (void *)&i); ? But one would never call memcpy like: memcpy((void *)pi, (void *)pj, sizeof *pj); /*memcpy((void *)pi,...
24
by: temper3243 | last post by:
Hi, Many people have used this code in my project. It works because b is using the extra memory for other 4 variables.I access obj max. well here are a few questions 1) Where does it fail. I...
4
by: alisbub | last post by:
1. Write an interactive program that asks the user to input the length and width of a rectangular lawn. The dimensions should be in yards. Your program should compute the area of the lawn in square...
6
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I found a really strange quirk in the C# compiler, which I can't beleive is proper behaviour. If I define a class thus: public class MyClass { public override string ToString() { return...
11
by: jyck91 | last post by:
// Base Conversion // Aim: This program is to convert an inputted number // from base M into base N. Display the converted // number in base N. #include <stdio.h> #include <stdlib.h>...
6
by: Peter Lee | last post by:
what's the correct behaver about the following code ? ( C++ standard ) I got a very strange result.... class MyClass { public: MyClass(const char* p) { printf("ctor p=%s\n", p);
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.