Reputation: 5617
I'm new to C# but familiar with vb.net
my setVendor functions expects an int and a string
why does this work
shopify.setVendor(System.Convert.ToInt32(reader["ProductID"]), System.Convert.ToString(reader["Vendor"]));
but this fails for both parameters:
shopify.setVendor(int.Parse(reader["ProductID"]), reader["Vendor"].ToString);
very confused. It wants a string and I give it a string but it doesn't accept it . . . error converting string to int
Upvotes: 0
Views: 138
Reputation: 11
int productId;
if(int.TryParse(reader["ProductID"].ToString(), out productId))
shopify.setVendor(productId, reader["Vendor"].ToString());
Would be a safe way to do it.
Upvotes: 1
Reputation: 872
The ToString part in your second code-snippet requires parentheses () because it is a method, not a member or property.
Upvotes: 1
Reputation: 32197
Well, for your first question
System.Convert.ToInt32(...)
and System.Convert.ToString(...)
convert the supplied arguments to int
and string
respectively which is in the correct format as expected by your code.
Secondly, it should be ToString()
not ToString
since you want to make a call to the method:
reader["Vendor"].ToString()
Upvotes: 1
Reputation: 1503529
There's an overload of Convert.ToInt32
which accepts object
. There's no such overload for int.Parse
. The argument must be a string
at compile time. You would need:
shopify.setVendor(int.Parse(reader["ProductID"].ToString()),
reader["Vendor"].ToString());
(Note the change from ToString
to ToString()
for the second argument... previously you were specifying the ToString
method group which is used to create delegates; with the change you're calling ToString
instead.)
Or:
// This only works if the value *is* a string
shopify.setVendor(int.Parse((string) reader["ProductID"]),
reader["Vendor"].ToString());
Ideally, however, you'd get back the values in the correct forms already, so you could use:
shopify.setVendor((int) reader["ProductID"], (string) reader["Vendor"]);
Or:
// Set up productIdColumn and vendorColumn first
shopify.setVendor(reader.GetInt32(productIdColumn), reader.GetString(vendorColumn));
Also note that setVendor
is not a conventional .NET method name.
Upvotes: 6