469,898 Members | 2,259 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

convert function to csharp

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;
}
}
Nov 17 '05 #1
5 3889
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" <an**@sutorius.com> wrote in message
news:dQ********************@twister.southeast.rr.c om...
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;
}
}

Nov 17 '05 #2
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 )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:u9**************@TK2MSFTNGP10.phx.gbl...
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" <an**@sutorius.com> wrote in message
news:dQ********************@twister.southeast.rr.c om...
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;
}
}


Nov 17 '05 #3

"Andy Sutorius" <an**@sutorius.com> wrote in message
news:Ht********************@twister.southeast.rr.c om...
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:u9**************@TK2MSFTNGP10.phx.gbl...
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" <an**@sutorius.com> wrote in message
news:dQ********************@twister.southeast.rr.c om...
> 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;
> }
> }
>
>



Nov 17 '05 #4
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
Nov 17 '05 #5
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

"Andy Sutorius" wrote:
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;
}
}

Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Tamir Khason | last post: by
3 posts views Thread by Mike | last post: by
1 post views Thread by dongxm | last post: by
5 posts views Thread by moondaddy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.