472,780 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

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 4053
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: hunterb | last post by:
I have a file which has no BOM and contains mostly single byte chars. There are numerous double byte chars (Japanese) which appear throughout. I need to take the resulting Unicode and store it in a...
11
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
1
by: Tamir Khason | last post by:
Somebody knows how to convert Bounds to Point ??? Thnx
3
by: Mike | last post by:
Hi, Does anyone know of reliable programs that convert C# to Java and viceversa? Thanks Mike
2
by: Remi Caron | last post by:
Hi, I took over an Visual Object project (Visual Clipper) in that language there is a function to: Convert a string containing a 32-bit binary date to a date data type. That function is called...
1
by: dongxm | last post by:
Is there a function can convert "abc" to "\u0097\u0098\u0099" in dotnet(c#)
5
by: moondaddy | last post by:
How do I get the string representation of an int? for example int var1 = 2; string var2 = var1.ToString; I'm wanting var2 to be "2" I get the compile error: Error 1 Cannot convert...
1
by: Robert Dufour | last post by:
I am trying to sort the membership list of the membership provider for ASP.NET the code has retrieved the list and now we need to sort it by a parameter if it was passed. So in C# the code is....
18
by: MrVS | last post by:
Hi, I have a C++ CLR class method that takes System::Byte *b as parameter argument. I want the CSharp caller pass a byte * to this function. But in the CSharp prorgram, I only managed to create a...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.