overloaded method works in VB - NOT in C# ??

P

Phil Streiff

I tried to convert this VB function to C#:

VB------------------------------------------------
Function GetSelectedIndex(CID as String) as Integer
Dim iLoop as Integer
Dim dt as DataTable = ddlDataSet.Tables("Categories")
For iLoop = 0 to dt.Rows.Count - 1
If Int32.Parse(CID) =
Int32.Parse(dt.Rows(iLoop)("FAQCategoryID")) then
Return iLoop
End If
Next iLoop
End Function
--------------------------------------------------

converted to C#-----------------------------------
public int GetSelectedIndex(string CID) {
int iLoop;
DataTable dt = ddlDataSet.Tables["Categories"];
for (iLoop = 0; iLoop <= dt.Rows.Count - 1; iLoop++)
{
if (Int32.Parse(CID) ==
Int32.Parse(dt.Rows[iLoop]["FAQCategoryID"]))
{
return iLoop;
}
}
}
--------------------------------------------------
but I get the following error on the "if" statement:

CS1502: The best overloaded method match for 'int.Parse(string)' has
some invalid arguments

Can anyone see what I am doing wrong and guide me in the right
direction?

This is in an inline .aspx page

Thanks in advance,
Phil
 
J

Jon Skeet [C# MVP]

CS1502: The best overloaded method match for 'int.Parse(string)' has
some invalid arguments

Can anyone see what I am doing wrong and guide me in the right
direction?

Yes - you're using Int32.Parse but passing it an object, not a string.
I'm surprised it works in VB.NET - do you have Option Strict On or not?

I suggest you cast the value from the row to a string - you may wish to
check that it *is* a string first, if you have a better error action to
take than the cast exception you'll get if you just cast it anyway.
 
P

pj

I suggest you cast the value from the row to a string

Jon:

Thanks for your reply. I embarrassed to say I need a little more
"hand-holding" since I'm a beginner with C#.

How exactly, might I do what you suggest above? Could you give me an
example?

Thanks in advance for your help.

Phil

-pj
 
A

Adam W Root

either like this:

Int32.Parse(dt.Rows[iLoop]["FAQCategoryID"].ToString())

or like this:

Int32.Parse(Convert.ToString(dt.Rows[iLoop]["FAQCategoryID"]))

or like this:

Int32.Parse((string)dt.Rows[iLoop]["FAQCategoryID"])

I prefer the third way because it implies that you know the object in the
row is a string already, and that you aren't trying to do a conversion. But
do as you want, you'll get the same result.

Adam
 
P

pj

Adam:

Thanks for your reply. I tried all 3 methods you suggested and get the
following compilation error:

CS0161: 'ASP.gridinsert_aspx.GetSelectedIndex(string)': not all code
paths return a value.

Source error:
public int GetSelectedIndex(string CID) {

(column 13)

here is the line I changed:
if (Int32.Parse(CID) ==
Int32.Parse(Convert.ToString(dt.Rows[iLoop]["FAQCategoryID"])))

Basically, I'm just trying to use a dropdownlist, populated from a
database, to select an ID to put into a table when a user clicks a
button performing an INSERT. If there is a better way to code this, I'm
open.

Any other ideas?

-pj
 
B

Brad Williams

dt.Rows[iLoop]["FAQCategoryID"] is of type object, you should CAST it to
it's actual or compatible type first.

ToString() would be semantically wrong here, because it means: "give me a
string that represents this object." Whereas a cast means (or should mean):
"give me a string ref to this object -- or throw an exception if that can't
be done". It is best to get exceptions when your assumptions are wrong.
 
P

pj

Brad:

Thanks for your reply. I got the same error message using the other 2
casting methods suggested by Adam. Specifically, the last one he
recommended:

Int32.Parse((string)dt.Rows[iLoop]["FAQCategoryID"])
you should CAST it to it's actual or compatible type first.

I'm sorry to say I'm dumb enough that I don't know what it's actual or
compatible type is, so I don't know how to do what you suggest, if it's
something other than what I've already tried.

Guess I'll give up on this and try a totally different approach.

-pj
 
P

pj

Thanks to Brad, Jon & Adam for your helpful responses. I did get my code
working finally, with your patient assistance.

-pj
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top