C# newbie part2

  • Thread starter Thread starter Craig
  • Start date Start date
C

Craig

I mixed up things in the previous post, sorry about that

I was looking for the equivalents in C#

1) Dim myRow As DataRow
For Each myRow In DataSetGroupSQL1.tblProfileGroups.Rows
'Test for ALL group
If myRow("GroupName") = "ALL" Then blnGroupALL = True
Next


2) With DataSetThoughtsForm1.tblYourThoughts(0)
.ProfileID = Session("UserID")
.ThoughtName = txtThoughtName.Text
.ThoughtText = txtThoughtText.Text
End With
 
For the general solution it is similar to VB:

foreach (DataRow myRow in DataSetGroupSQL1.tblProfileGroups.Rows)
{
//Do logic here, myRow is readonly
if (myRow("GroupName") = "ALL")
bInGroupAll = true;
};

When you need to write to an element then use a for loop instead.

For your specific case of a DataSet however I'd use the following faster
logic (the var assignment is simply for clarity, I'd combine the 2 statements
into 1):

DataRow[] rows = DataSetGroupSQL1.tblProfileGroups.Select("GroupName =
'ALL'");
bInGroupAll = (rows.Length > 0);

Michael Taylor - 9/19/05
 
TaylorMichaelL said:
For the general solution it is similar to VB:

foreach (DataRow myRow in DataSetGroupSQL1.tblProfileGroups.Rows)
{
//Do logic here, myRow is readonly
if (myRow("GroupName") = "ALL")
that's wrong, should be:
if (myRow["GroupName"] == "ALL")
bInGroupAll = true;
};

When you need to write to an element then use a for loop instead.

For your specific case of a DataSet however I'd use the following faster
logic (the var assignment is simply for clarity, I'd combine the 2
statements
into 1):

DataRow[] rows = DataSetGroupSQL1.tblProfileGroups.Select("GroupName =
'ALL'");
bInGroupAll = (rows.Length > 0);

Michael Taylor - 9/19/05

Craig said:
I mixed up things in the previous post, sorry about that

I was looking for the equivalents in C#

1) Dim myRow As DataRow
For Each myRow In DataSetGroupSQL1.tblProfileGroups.Rows
'Test for ALL group
If myRow("GroupName") = "ALL" Then blnGroupALL = True
Next


2) With DataSetThoughtsForm1.tblYourThoughts(0)
.ProfileID = Session("UserID")
.ThoughtName = txtThoughtName.Text
.ThoughtText = txtThoughtText.Text
End With
 
Hi Craig,

1) foreach(DataRow myRow in DataSetGroupSQL1.tblProfileGroups.Rows)
{
if(myRow["GroupName"].ToString() == "All")
blnGroupALL = true;
}

2) No equivalent, you ned to set each property using the full name:

DataSetThoughtsForm1.tblYourThoughts[0].ProfileID = (int)Session["UserID"];
DataSetThoughtsForm1.tblYourThoughts[0].txtThoughtName = txtThoughtName.Text;
DataSetThoughtsForm1.tblYourThoughts[0].txtThoughtText = txtThoughtText.Text;

Note that type conversion is much stricter in C#. Where VB.Net uses () for indexing, C# uses [].
 
Hi Craig,

1) foreach(DataRow myRow in DataSetGroupSQL1.tblProfileGroups.Rows)
{
if(myRow["GroupName"].ToString() == "All")
blnGroupAll = true;
}

2) No equivalent, you need to set all properties individually

DataSetThoughtsForm1.tblYourThoughts(0).ProfileID = (int)Session["UserID"];
DataSetThoughtsForm1.tblYourThoughts(0).ThoughtName = txtThoughtName.Text;
DataSetThoughtsForm1.tblYourThoughts(0).ThoughtText = txtThoughtText.Text;

Note that type conversion is much stricter in C#.
 
2) No equivalent, you need to set all properties individually

DataSetThoughtsForm1.tblYourThoughts(0).ProfileID =
(int)Session["UserID"];
DataSetThoughtsForm1.tblYourThoughts(0).ThoughtName = txtThoughtName.Text;
DataSetThoughtsForm1.tblYourThoughts(0).ThoughtText = txtThoughtText.Text;
<snip>
Better is:
tblYourThoughtsRow row = DataSetThoughtsForm1.tblYourThoughts[0];
row.ProfileID = (int)Session["UserID"];
row.ThoughtName = txtThoughtName.Text;
row.ThoughtText = txtThoughtText.Text;

where tblYourThoughtsRow is the type of
DataSetThoughtsForm1.tblYourThought[0].
 
Craig said:
I mixed up things in the previous post, sorry about that

I was looking for the equivalents in C#

1) Dim myRow As DataRow
For Each myRow In DataSetGroupSQL1.tblProfileGroups.Rows
'Test for ALL group
If myRow("GroupName") = "ALL" Then blnGroupALL = True
Next

In addidtion to the other answers: if "blnGroupALL" is you function name,
then you should use "return true;". In VB you assign the return value to
the function, in C# you use the "return" statement to return a value
(and immediately stop executing this method)

Hans Kesting
 
Back
Top