1

I'm new to C# and trying to get this code to work, but have been unsuccessful. It works in vb but for some reason in C# i getting this error: argument 1: cannot convert from object to string, the best overloaded method match for has some invalid arguments. Any help would be very helpful.

Here is my code

VB

If sqlObj.sel_all_airlines(row("COMPANY")).tables(0).rows.count > 1 Then
end if 

C#

if (sqlObj.sel_all_airlines(row["COMPANY"]).Tables[0].Rows.Count > 1){}
Reporter
  • 3,897
  • 5
  • 33
  • 47
Will
  • 1,084
  • 5
  • 20
  • 42
  • 3
    In VB, turn on Option Strict and Option Explicit, and get your VB code to compile. Then the C# conversion should be easier :) – JMK Jun 10 '13 at 12:28

6 Answers6

6

Try this:

if (sqlObj.sel_all_airlines(row["COMPANY"] as string).Tables[0].Rows.Count > 1){
}
Bill Gregg
  • 7,067
  • 2
  • 22
  • 39
  • Thanks this worked!!!. Could you explain the reason for this, just trying to get a better understanding? – Will Jun 10 '13 at 12:34
  • 1
    @Will VB attempts to automatically cast types. C# doesn't, so you have to tell it explicitly when to cast an object, and what to cast it to. – Nolonar Jun 10 '13 at 12:35
  • Vote up - this is an alternative version of my answer - `row["COMPANY"] as string` is same as my `(String)row["COMPANY"]`. – ElmoVanKielmo Jun 10 '13 at 12:39
  • 1
    @ElmoVanKielmo Actually [they're not the same](http://stackoverflow.com/a/132467/969613) – JMK Jun 10 '13 at 12:45
  • @JMK - you are right - there are differences but not relevant in this case. I prefer catching unexpected `null` and improper values (here it would be something other than string casted to `object`) and deal with them on my own. In most cases such things happen when we have a bug in our code or data is corrupted. What would be the purpose of calling `sqlObj.sell_all_airlines(null)` - `as string` would return `null` if `row["COMPANY"]` is not a string. – ElmoVanKielmo Jun 10 '13 at 12:55
1

You've probably got the implicit type converstion feature turned on in your VB.Net there, so a closer match to what it's doing would be:

if (sqlObj.sel_all_airlines(row["COMPANY"].ToString()).Tables[0].Rows.Count > 1){
}
Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
1

Try the above

if (sqlObj.sel_all_airlines(row["COMPANY"].Tostring()).Tables[0].Rows.Count > 1){
   }
kostas ch.
  • 1,960
  • 1
  • 17
  • 30
  • Does it really work as expected? If the value has been casted to `object` - then according to documentation http://msdn.microsoft.com/pl-pl/library/system.object.tostring.aspx it wouldn't cast to `String` but it would return `"Object"`. Am I missing something? – ElmoVanKielmo Jun 10 '13 at 12:37
1

Try explicit cast to String:

if (sqlObj.sel_all_airlines((String)row["COMPANY"]).Tables[0].Rows.Count > 1){
}
ElmoVanKielmo
  • 10,907
  • 2
  • 32
  • 46
  • 1
    +1. Please note that: `(String)row["COMPANY"]` will throw an exception if `row["Company"]` isn't a string, but it should be correct aside from that. – Nolonar Jun 10 '13 at 12:42
  • Yes, this is exactly how it works @Nolonar, as casting is possible in C# to: any of ancestors (note that every class derives from `object`), interface implemented by original type and finally (and obviously) back to original type. Otherwise you get exception. – ElmoVanKielmo Jun 10 '13 at 12:47
1
if (sqlObj.sel_all_airlines((string)row["COMPANY"]).Tables[0].Rows.Count > 1)
Kimtho6
  • 6,154
  • 9
  • 40
  • 56
Richard
  • 31
  • 3
0

I assume that the method If sqlObj.sel_all_airlines returns a DataSet. Then you want to check if there's at least one row in the first table (side-note: it's probably more appropriate to return a DataTable instead).

It's often easier to understand, maintain or extend (or just translate to C#) if you use a variable in between, for example:

VB.NET

Dim companyTable As DataTable = sqlObj.sel_all_airlines(row("COMPANY")).Tables(0)
If companyTable.Rows.Count > 1 Then
' ...

C#

DataTable companyTable = sqlObj.sel_all_airlines(row["COMPANY"]).Tables[0];
if(companyTable.Rows.Count > 1)
{
    // ...

You should also set Option Strict and Option Explicit to ON in the project's compiler settings. Then you have to fix several compiler errors and warning. But it's worth it because it provides strong typing, prevents unintended type conversions with data loss, disallows late binding, and improves performance, its use is strongly recommended.

The code is also much more like C# afterwards (apart from the syntax).

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939