convert function to csharp

  • Thread starter Thread starter Andy Sutorius
  • Start date Start date
A

Andy Sutorius

Hi,

I am attempting to convert this vb function to csharp but I am getting stuck
on the if statement dt.Rows(iLoop)("FAQCategoryID")). The compiler says
"method name expected" and underlines dt.Rows[iLoop]. I don't understand.
Thanks for your help.

Function GetSelIndex(CatID as String) as Integer
Dim iLoop as Integer

'Loop through each row in the DataSet
Dim dt as DataTable = ddlDataSet.Tables("Categories")
For iLoop = 0 to dt.Rows.Count - 1
If Int32.Parse(CatID) = Int32.Parse(dt.Rows(iLoop)("FAQCategoryID"))
then
Return iLoop
End If
Next iLoop
End Function

public int GetSelIndex(string CatID)
{
int iLoop;

// Loop through each row in the DataSet
DataTable dt = ddlDataSet.Tables["Categories"];
int rowCount = dt.Rows.Count-1;

for (iLoop=0; rowCount <= iLoop; iLoop++)
if (Int32.Parse(CatID) = Int32.Parse(dt.Rows[iLoop]("id")))
{
return iLoop;
}
}
 
hi


for (iLoop=0; rowCount <= iLoop; iLoop++)
if (Int32.Parse(CatID) = Int32.Parse(dt.Rows[iLoop]["id"]))
{
return iLoop;
}
}

or a better way:
int catID = Int32.Parse(CatID);
int index= 0;
foreach( DataRow row in ddlDataSet.Tables["Categories"].Rows)
if ( catID == (int)row["CatID"])
return index;
else
index++;


cheers,
 
Hi,

Thank you for the code. I am now using your better way. When I compile I
receive "not all code paths return a value" and the function name,
GetSelIndex, is underlined.

I don't understand what the compiler is saying. Code Path?

Thanks,

Andy


Ignacio Machin ( .NET/ C# MVP ) said:
hi


for (iLoop=0; rowCount <= iLoop; iLoop++)
if (Int32.Parse(CatID) = Int32.Parse(dt.Rows[iLoop]["id"]))
{
return iLoop;
}
}

or a better way:
int catID = Int32.Parse(CatID);
int index= 0;
foreach( DataRow row in ddlDataSet.Tables["Categories"].Rows)
if ( catID == (int)row["CatID"])
return index;
else
index++;


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation






Andy Sutorius said:
Hi,

I am attempting to convert this vb function to csharp but I am getting
stuck
on the if statement dt.Rows(iLoop)("FAQCategoryID")). The compiler says
"method name expected" and underlines dt.Rows[iLoop]. I don't understand.
Thanks for your help.

Function GetSelIndex(CatID as String) as Integer
Dim iLoop as Integer

'Loop through each row in the DataSet
Dim dt as DataTable = ddlDataSet.Tables("Categories")
For iLoop = 0 to dt.Rows.Count - 1
If Int32.Parse(CatID) = Int32.Parse(dt.Rows(iLoop)("FAQCategoryID"))
then
Return iLoop
End If
Next iLoop
End Function

public int GetSelIndex(string CatID)
{
int iLoop;

// Loop through each row in the DataSet
DataTable dt = ddlDataSet.Tables["Categories"];
int rowCount = dt.Rows.Count-1;

for (iLoop=0; rowCount <= iLoop; iLoop++)
if (Int32.Parse(CatID) = Int32.Parse(dt.Rows[iLoop]("id")))
{
return iLoop;
}
}
 
Andy Sutorius said:
Hi,

Thank you for the code. I am now using your better way. When I compile I
receive "not all code paths return a value" and the function name,
GetSelIndex, is underlined.

I don't understand what the compiler is saying. Code Path?

Code paths are all the possible paths of execution through your code, that
is each possible combination of conditions. You must return or throw an
exception at the end of those paths(return really *IS* the end of those
paths). In your case , while in your foreach loop you are only returning if
a condition is met, therefore it is possible for the loop to end without
returning. And thus possible for execution to reach the end of the method
without returning.

Just add an appropriate return statement in at the end of your method.

Something like:

public int GetSelIndex(string CatID)
{
int iLoop;

// Loop through each row in the DataSet
DataTable dt = ddlDataSet.Tables["Categories"];
int rowCount = dt.Rows.Count-1;

int catID = Int32.Parse(CatID);
int index= 0;
foreach( DataRow row in ddlDataSet.Tables["Categories"].Rows)
{
if ( catID == (int)row["CatID"])
return index;
else
index++;
}

return -1; //note the return here, before it was possible to reach the end
of the method without returning.
}
Thanks,

Andy


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote
in message news:[email protected]...
hi


for (iLoop=0; rowCount <= iLoop; iLoop++)
if (Int32.Parse(CatID) = Int32.Parse(dt.Rows[iLoop]["id"]))
{
return iLoop;
}
}

or a better way:
int catID = Int32.Parse(CatID);
int index= 0;
foreach( DataRow row in ddlDataSet.Tables["Categories"].Rows)
if ( catID == (int)row["CatID"])
return index;
else
index++;


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation






Andy Sutorius said:
Hi,

I am attempting to convert this vb function to csharp but I am getting
stuck
on the if statement dt.Rows(iLoop)("FAQCategoryID")). The compiler says
"method name expected" and underlines dt.Rows[iLoop]. I don't understand.
Thanks for your help.

Function GetSelIndex(CatID as String) as Integer
Dim iLoop as Integer

'Loop through each row in the DataSet
Dim dt as DataTable = ddlDataSet.Tables("Categories")
For iLoop = 0 to dt.Rows.Count - 1
If Int32.Parse(CatID) = Int32.Parse(dt.Rows(iLoop)("FAQCategoryID"))
then
Return iLoop
End If
Next iLoop
End Function

public int GetSelIndex(string CatID)
{
int iLoop;

// Loop through each row in the DataSet
DataTable dt = ddlDataSet.Tables["Categories"];
int rowCount = dt.Rows.Count-1;

for (iLoop=0; rowCount <= iLoop; iLoop++)
if (Int32.Parse(CatID) = Int32.Parse(dt.Rows[iLoop]("id")))
{
return iLoop;
}
}
 
Andy,

When we bring back first your VBNet code to something more reasonable.

\\\
Private Function GetSelIndex(Byvalue CatID as String) as Integer
Dim dt as DataTable = ddlDataSet.Tables("Categories")
For i as Integer = 0 to dt.Rows.Count - 1
If CatID = dt.Rows(i)("FAQCategoryID").ToString)
Return i
End If
End Function
///
\\\
private integer GetSelIndex(string CatID){
DataTable dt = ddlDataSet.Tables["Categories"];
for (int i = 0;i> dt.Rows.Count;i++){
if (CatID == dt.Rows(i)["FAQCategoryID"].ToString)){
return i;}}
}
///

Not so much difference in my opinion.

Cor
 
Our VB to C# converter (Instant C#) produces the following - note that:
1. Although in your example you could safely ignore it, in general you have
to account for the fact that the ending condition of a VB for loop is checked
only once while the ending condition of a C# for loop is checked every
iteration.
2. You need to return a value for every code path in C# - usually C#
developers will just add the default statement at the end of the function
when they get this warning (VB does this for you behind the scenes).

public int GetSelIndex(string CatID)
{
int iLoop = 0;

//Loop through each row in the DataSet
DataTable dt = ddlDataSet.Tables["Categories"];
int tempFor1 = dt.Rows.Count;
for (iLoop = 0; iLoop < tempFor1; iLoop++)
{
if (Int32.Parse(CatID) ==
Int32.Parse(dt.Rows[iLoop]["FAQCategoryID"]))
{
return iLoop;
}
}
//INSTANT C# NOTE: Inserted the following 'return' since all code paths
must return a value in C#:
return 0;
}


David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter and the Instant VB C# to
VB.NET converter
 
Back
Top