473,385 Members | 1,752 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,385 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 4085
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.