Easy VB.NET > C# port question

N

nime

I tried to port from VB to C# and now I'm stuck. I couldn't convert the VB line

readdata = dt.Rows(0)(data)

as C#:

return dt.Rows[0][data];

Error detail:

Error 1 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)


The code simply emulates ini file with XML:

VB
*****************************************************************
Function readdata(ByVal data As String, ByVal file As String, ByVal node As String) As String
Try
Dim ds As New DataSet()
ds.ReadXml(file)
Dim dt As DataTable = ds.Tables(node)
readdata = dt.Rows(0)(data)
Catch e1 As Exception
readdata = "?"
End Try
End Function

C#
*****************************************************************
private string readdata(string data, string file, string node)
{
try {
DataSet ds = new DataSet();
ds.ReadXml(file);
DataTable dt = ds.Tables(node);
return dt.Rows[0][data];
} catch (Exception e1) {
return "?";
}
}
 
T

Tom Porterfield

nime said:
I tried to port from VB to C# and now I'm stuck. I couldn't convert the
VB line
readdata = dt.Rows(0)(data)

as C#:

return dt.Rows[0][data];

Error detail:

Error 1 Cannot implicitly convert type 'object' to 'string'. An
explicit conversion exists (are you missing a cast?)

The code simply emulates ini file with XML:

VB
*****************************************************************
Function readdata(ByVal data As String, ByVal file As String, ByVal node
As String) As String Try
Dim ds As New DataSet()
ds.ReadXml(file)
Dim dt As DataTable = ds.Tables(node)
readdata = dt.Rows(0)(data)
Catch e1 As Exception
readdata = "?"
End Try
End Function

C#
*****************************************************************
private string readdata(string data, string file, string node)
{
try {
DataSet ds = new DataSet();
ds.ReadXml(file);
DataTable dt = ds.Tables(node);
return dt.Rows[0][data];
} catch (Exception e1) {
return "?";
}
}

Assuming the DataType for the data column is a string data type, use the
Convert class. Ex:

string strdata = Convert.ToString(dt.Rows[0][data];
return strdata;
 
N

nime

Thank you a lot, it worked, VB doesn't need a type conversion...


Tom Porterfield said:
nime said:
I tried to port from VB to C# and now I'm stuck. I couldn't convert the
VB line
readdata = dt.Rows(0)(data)

as C#:

return dt.Rows[0][data];

Error detail:

Error 1 Cannot implicitly convert type 'object' to 'string'. An
explicit conversion exists (are you missing a cast?)

The code simply emulates ini file with XML:

VB
*****************************************************************
Function readdata(ByVal data As String, ByVal file As String, ByVal node
As String) As String Try
Dim ds As New DataSet()
ds.ReadXml(file)
Dim dt As DataTable = ds.Tables(node)
readdata = dt.Rows(0)(data)
Catch e1 As Exception
readdata = "?"
End Try
End Function

C#
*****************************************************************
private string readdata(string data, string file, string node)
{
try {
DataSet ds = new DataSet();
ds.ReadXml(file);
DataTable dt = ds.Tables(node);
return dt.Rows[0][data];
} catch (Exception e1) {
return "?";
}
}

Assuming the DataType for the data column is a string data type, use the Convert class. Ex:

string strdata = Convert.ToString(dt.Rows[0][data];
return strdata;
 
M

Martin Z

C# has "Option Strict" on - and if this is a serious application (not
just some personal batch script you're throwing together) so should
VB.Net. You can't silently convert an object into a string - you have
to use the ToString() method to do that.

Thus, the missing statement is
return dt.Rows[0][data].ToString();
 
M

Martin Z

Google "option strict" to understand the difference. C# conversions =
VB.Net conversions if VB.Net has the "option strict" flag turned ON.
Thank you a lot, it worked, VB doesn't need a type conversion...


Tom Porterfield said:
nime said:
I tried to port from VB to C# and now I'm stuck. I couldn't convert the
VB line
readdata = dt.Rows(0)(data)

as C#:

return dt.Rows[0][data];

Error detail:

Error 1 Cannot implicitly convert type 'object' to 'string'. An
explicit conversion exists (are you missing a cast?)

The code simply emulates ini file with XML:

VB
*****************************************************************
Function readdata(ByVal data As String, ByVal file As String, ByVal node
As String) As String Try
Dim ds As New DataSet()
ds.ReadXml(file)
Dim dt As DataTable = ds.Tables(node)
readdata = dt.Rows(0)(data)
Catch e1 As Exception
readdata = "?"
End Try
End Function

C#
*****************************************************************
private string readdata(string data, string file, string node)
{
try {
DataSet ds = new DataSet();
ds.ReadXml(file);
DataTable dt = ds.Tables(node);
return dt.Rows[0][data];
} catch (Exception e1) {
return "?";
}
}

Assuming the DataType for the data column is a string data type, use the Convert class. Ex:

string strdata = Convert.ToString(dt.Rows[0][data];
return strdata;
 
T

Tom Porterfield

nime said:
Thank you a lot, it worked, VB doesn't need a type conversion...

It does if you set Option Strict On, which is the setting I recommend.
 
N

nime

Actually I knew. VB.Net - I call as VB# - doesn't support automatic type conversions
and default properties like thing, instead of VB6.

But in my example there was no conversion and I didn't noticed Option Strict.

Thank you for all comments and details.
 
N

nime

return dt.Rows[0][data].ToString();

I didn't help. Instead, I used

string strdata = Convert.ToString(dt.Rows[0][data]);
return strdata;

as Tom Porterfield said...

Strange ...
 

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