VB.Net to C# conversion phenomenon

G

Guest

I had the following VB.Net code

ds.Tables.Add(BindData(camp_id, grid_id, cbo.SelectedValue, "CAMP"))
tbl = BindData(camp_id, grid_id, cbo.SelectedValue, "CHURN")

ds.Tables(0).ImportRow(tbl.Rows(0))

For i = 1 To ds.Tables(0).Columns.Count - 1
If IsDBNull(tbl.Rows(0).Item(i)) Or campCustomers = 0 Then
dr(i) = 0
dr1(i) = 0
Else
dr(i) = Format(tbl.Rows(0).Item(i) / CInt(campCustomers) * 100,
"0.00")
dr1(i) = Format(tbl.Rows(0).Item(i) / CInt(campCustomers) * 12 *
100, "0.00")
End If
next


Which I translated into C# like this

ds.Tables.Add(BindData(camp_id, grid_id, Convert.ToInt32(cbo.SelectedValue),
"CAMP"));
tbl = BindData(camp_id, grid_id, Convert.ToInt32(cbo.SelectedValue), "CHURN");

ds.Tables[0].ImportRow(tbl.Rows[0]);

for (int i = 1; i <= ds.Tables[0].Columns.Count - 1; i++)
{
if (ds.Tables[0].Rows[0] == DBNull.Value || campCustomers == 0)
{
dr = 0;
dr1 = 0;
}
else
{
dr = (Convert.ToDouble (tbl.Rows[0]) / campCustomers *
100).ToString("#0.00");
dr1 = (Convert.ToDouble(tbl.Rows[0]) / campCustomers * 12 *
100).ToString("#0.00");
}
}

The interesting part is that under some in C# “ds.Tables[0].Rows[0]†does
not contain the data but I could get the date to define
“ds.Tables[0].Rows[1]â€

And under some circumstances “ds.Tables.Add(BindData(camp_id, grid_id,
cbo.SelectedValue, "CAMP"))†returns a null value. Hence I have to access the
data with “ds.Tables[0].Rows[0]â€

In VB.Net that does not cause any problems it seems it always accesses the
“right†row even if “ds.Tables.Add(BindData(camp_id, grid_id,
cbo.SelectedValue, "CAMP"))†returns null.

I found a (dodgy) workaround in C# to test what value is in the row and then
define which rownumber to use.

If “ds.Tables.Add(BindData(camp_id, grid_id,
Convert.ToInt32(cbo.SelectedValue), "CAMP"));†did not return null I got
these values in the datasets rows

ds.Tables[0].Rows[0][0] "Campaign"
ds.Tables[0].Rows[1][0] "CHURN"

If it returned a null

ds.Tables[0].Rows[0][0] "Churn"
ds.Tables[0].Rows[1][0] error: object 'ds.Tables[0].Rows[1]' doesn't have
an indexer

So now I check

if (ds.Tables[0].Rows[0][0].ToString().Equals("Campaign"))
intRowId = 1;
else
intRowId = 0;

for (int i = 1; i <= ds.Tables[0].Columns.Count - 1; i++)
{
if (ds.Tables[0].Rows[intRowId] == DBNull.Value || campCustomers == 0)
{
dr = 0;
dr1 = 0;
……..

I am surprised that the C# translation does not behave in the same manner as
the VB.Net version. Can someone please explain why that is? Or did I
mistranslate?

I hope I did not confuse you guys to much….

Thanks a lot

Chris
 
G

Guest

I'm not sure if this is the problem, but I would use
"System.Convert.IsDBNull" as the replacement for the VB IsDBNull function.
--
David Anton
www.tangiblesoftwaresolutions.com
Home of:
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter

chris said:
I had the following VB.Net code

ds.Tables.Add(BindData(camp_id, grid_id, cbo.SelectedValue, "CAMP"))
tbl = BindData(camp_id, grid_id, cbo.SelectedValue, "CHURN")

ds.Tables(0).ImportRow(tbl.Rows(0))

For i = 1 To ds.Tables(0).Columns.Count - 1
If IsDBNull(tbl.Rows(0).Item(i)) Or campCustomers = 0 Then
dr(i) = 0
dr1(i) = 0
Else
dr(i) = Format(tbl.Rows(0).Item(i) / CInt(campCustomers) * 100,
"0.00")
dr1(i) = Format(tbl.Rows(0).Item(i) / CInt(campCustomers) * 12 *
100, "0.00")
End If
next


Which I translated into C# like this

ds.Tables.Add(BindData(camp_id, grid_id, Convert.ToInt32(cbo.SelectedValue),
"CAMP"));
tbl = BindData(camp_id, grid_id, Convert.ToInt32(cbo.SelectedValue), "CHURN");

ds.Tables[0].ImportRow(tbl.Rows[0]);

for (int i = 1; i <= ds.Tables[0].Columns.Count - 1; i++)
{
if (ds.Tables[0].Rows[0] == DBNull.Value || campCustomers == 0)
{
dr = 0;
dr1 = 0;
}
else
{
dr = (Convert.ToDouble (tbl.Rows[0]) / campCustomers *
100).ToString("#0.00");
dr1 = (Convert.ToDouble(tbl.Rows[0]) / campCustomers * 12 *
100).ToString("#0.00");
}
}

The interesting part is that under some in C# “ds.Tables[0].Rows[0]†does
not contain the data but I could get the date to define
“ds.Tables[0].Rows[1]â€

And under some circumstances “ds.Tables.Add(BindData(camp_id, grid_id,
cbo.SelectedValue, "CAMP"))†returns a null value. Hence I have to access the
data with “ds.Tables[0].Rows[0]â€

In VB.Net that does not cause any problems it seems it always accesses the
“right†row even if “ds.Tables.Add(BindData(camp_id, grid_id,
cbo.SelectedValue, "CAMP"))†returns null.

I found a (dodgy) workaround in C# to test what value is in the row and then
define which rownumber to use.

If “ds.Tables.Add(BindData(camp_id, grid_id,
Convert.ToInt32(cbo.SelectedValue), "CAMP"));†did not return null I got
these values in the datasets rows

ds.Tables[0].Rows[0][0] "Campaign"
ds.Tables[0].Rows[1][0] "CHURN"

If it returned a null

ds.Tables[0].Rows[0][0] "Churn"
ds.Tables[0].Rows[1][0] error: object 'ds.Tables[0].Rows[1]' doesn't have
an indexer

So now I check

if (ds.Tables[0].Rows[0][0].ToString().Equals("Campaign"))
intRowId = 1;
else
intRowId = 0;

for (int i = 1; i <= ds.Tables[0].Columns.Count - 1; i++)
{
if (ds.Tables[0].Rows[intRowId] == DBNull.Value || campCustomers == 0)
{
dr = 0;
dr1 = 0;
……..

I am surprised that the C# translation does not behave in the same manner as
the VB.Net version. Can someone please explain why that is? Or did I
mistranslate?

I hope I did not confuse you guys to much….

Thanks a lot

Chris
 
G

Guest

I am also not sure but i hope the modification suggested by David will solve
the problem. As then the code will be similar to what you have implemented
under VB.NET

Just to point out: the IsDDNull checks for System.TypeCode.DBNull whereas
Sytem.DBNull.Value is an object of class System.DBNull

I dont know the exact difference between these two but i guess this
difference is causing the difference in behavior.

--
Cheers,
Rahul Anand

David Anton said:
I'm not sure if this is the problem, but I would use
"System.Convert.IsDBNull" as the replacement for the VB IsDBNull function.
--
David Anton
www.tangiblesoftwaresolutions.com
Home of:
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter

chris said:
I had the following VB.Net code

ds.Tables.Add(BindData(camp_id, grid_id, cbo.SelectedValue, "CAMP"))
tbl = BindData(camp_id, grid_id, cbo.SelectedValue, "CHURN")

ds.Tables(0).ImportRow(tbl.Rows(0))

For i = 1 To ds.Tables(0).Columns.Count - 1
If IsDBNull(tbl.Rows(0).Item(i)) Or campCustomers = 0 Then
dr(i) = 0
dr1(i) = 0
Else
dr(i) = Format(tbl.Rows(0).Item(i) / CInt(campCustomers) * 100,
"0.00")
dr1(i) = Format(tbl.Rows(0).Item(i) / CInt(campCustomers) * 12 *
100, "0.00")
End If
next


Which I translated into C# like this

ds.Tables.Add(BindData(camp_id, grid_id, Convert.ToInt32(cbo.SelectedValue),
"CAMP"));
tbl = BindData(camp_id, grid_id, Convert.ToInt32(cbo.SelectedValue), "CHURN");

ds.Tables[0].ImportRow(tbl.Rows[0]);

for (int i = 1; i <= ds.Tables[0].Columns.Count - 1; i++)
{
if (ds.Tables[0].Rows[0] == DBNull.Value || campCustomers == 0)
{
dr = 0;
dr1 = 0;
}
else
{
dr = (Convert.ToDouble (tbl.Rows[0]) / campCustomers *
100).ToString("#0.00");
dr1 = (Convert.ToDouble(tbl.Rows[0]) / campCustomers * 12 *
100).ToString("#0.00");
}
}

The interesting part is that under some in C# “ds.Tables[0].Rows[0]†does
not contain the data but I could get the date to define
“ds.Tables[0].Rows[1]â€

And under some circumstances “ds.Tables.Add(BindData(camp_id, grid_id,
cbo.SelectedValue, "CAMP"))†returns a null value. Hence I have to access the
data with “ds.Tables[0].Rows[0]â€

In VB.Net that does not cause any problems it seems it always accesses the
“right†row even if “ds.Tables.Add(BindData(camp_id, grid_id,
cbo.SelectedValue, "CAMP"))†returns null.

I found a (dodgy) workaround in C# to test what value is in the row and then
define which rownumber to use.

If “ds.Tables.Add(BindData(camp_id, grid_id,
Convert.ToInt32(cbo.SelectedValue), "CAMP"));†did not return null I got
these values in the datasets rows

ds.Tables[0].Rows[0][0] "Campaign"
ds.Tables[0].Rows[1][0] "CHURN"

If it returned a null

ds.Tables[0].Rows[0][0] "Churn"
ds.Tables[0].Rows[1][0] error: object 'ds.Tables[0].Rows[1]' doesn't have
an indexer

So now I check

if (ds.Tables[0].Rows[0][0].ToString().Equals("Campaign"))
intRowId = 1;
else
intRowId = 0;

for (int i = 1; i <= ds.Tables[0].Columns.Count - 1; i++)
{
if (ds.Tables[0].Rows[intRowId] == DBNull.Value || campCustomers == 0)
{
dr = 0;
dr1 = 0;
……..

I am surprised that the C# translation does not behave in the same manner as
the VB.Net version. Can someone please explain why that is? Or did I
mistranslate?

I hope I did not confuse you guys to much….

Thanks a lot

Chris
 
G

Guest

I converted to c# from VB.Net. But you guys are right. I changed it to
System.Convert.IsDBNull in the VB code and behaves in the same manner as the
c# version. Can you say "IsDBNull" in C# as well. Maybe not since C# would
not allow ambiguous coding.

Thanks Chris

Rahul Anand said:
I am also not sure but i hope the modification suggested by David will solve
the problem. As then the code will be similar to what you have implemented
under VB.NET

Just to point out: the IsDDNull checks for System.TypeCode.DBNull whereas
Sytem.DBNull.Value is an object of class System.DBNull

I dont know the exact difference between these two but i guess this
difference is causing the difference in behavior.

--
Cheers,
Rahul Anand

David Anton said:
I'm not sure if this is the problem, but I would use
"System.Convert.IsDBNull" as the replacement for the VB IsDBNull function.
--
David Anton
www.tangiblesoftwaresolutions.com
Home of:
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter

chris said:
I had the following VB.Net code

ds.Tables.Add(BindData(camp_id, grid_id, cbo.SelectedValue, "CAMP"))
tbl = BindData(camp_id, grid_id, cbo.SelectedValue, "CHURN")

ds.Tables(0).ImportRow(tbl.Rows(0))

For i = 1 To ds.Tables(0).Columns.Count - 1
If IsDBNull(tbl.Rows(0).Item(i)) Or campCustomers = 0 Then
dr(i) = 0
dr1(i) = 0
Else
dr(i) = Format(tbl.Rows(0).Item(i) / CInt(campCustomers) * 100,
"0.00")
dr1(i) = Format(tbl.Rows(0).Item(i) / CInt(campCustomers) * 12 *
100, "0.00")
End If
next


Which I translated into C# like this

ds.Tables.Add(BindData(camp_id, grid_id, Convert.ToInt32(cbo.SelectedValue),
"CAMP"));
tbl = BindData(camp_id, grid_id, Convert.ToInt32(cbo.SelectedValue), "CHURN");

ds.Tables[0].ImportRow(tbl.Rows[0]);

for (int i = 1; i <= ds.Tables[0].Columns.Count - 1; i++)
{
if (ds.Tables[0].Rows[0] == DBNull.Value || campCustomers == 0)
{
dr = 0;
dr1 = 0;
}
else
{
dr = (Convert.ToDouble (tbl.Rows[0]) / campCustomers *
100).ToString("#0.00");
dr1 = (Convert.ToDouble(tbl.Rows[0]) / campCustomers * 12 *
100).ToString("#0.00");
}
}

The interesting part is that under some in C# “ds.Tables[0].Rows[0]†does
not contain the data but I could get the date to define
“ds.Tables[0].Rows[1]â€

And under some circumstances “ds.Tables.Add(BindData(camp_id, grid_id,
cbo.SelectedValue, "CAMP"))†returns a null value. Hence I have to access the
data with “ds.Tables[0].Rows[0]â€

In VB.Net that does not cause any problems it seems it always accesses the
“right†row even if “ds.Tables.Add(BindData(camp_id, grid_id,
cbo.SelectedValue, "CAMP"))†returns null.

I found a (dodgy) workaround in C# to test what value is in the row and then
define which rownumber to use.

If “ds.Tables.Add(BindData(camp_id, grid_id,
Convert.ToInt32(cbo.SelectedValue), "CAMP"));†did not return null I got
these values in the datasets rows

ds.Tables[0].Rows[0][0] "Campaign"
ds.Tables[0].Rows[1][0] "CHURN"

If it returned a null

ds.Tables[0].Rows[0][0] "Churn"
ds.Tables[0].Rows[1][0] error: object 'ds.Tables[0].Rows[1]' doesn't have
an indexer

So now I check

if (ds.Tables[0].Rows[0][0].ToString().Equals("Campaign"))
intRowId = 1;
else
intRowId = 0;

for (int i = 1; i <= ds.Tables[0].Columns.Count - 1; i++)
{
if (ds.Tables[0].Rows[intRowId] == DBNull.Value || campCustomers == 0)
{
dr = 0;
dr1 = 0;
……..

I am surprised that the C# translation does not behave in the same manner as
the VB.Net version. Can someone please explain why that is? Or did I
mistranslate?

I hope I did not confuse you guys to much….

Thanks a lot

Chris
 
G

Guest

If you have a "using System;" statement at the top of your file, then you can
use "Convert.IsDBNull", but you could never use "IsDBNull" alone. In C# you
always need to include the class qualifier for a static method. VB would
allow omitting the class qualifier since it allows "Import System.Convert" -
i.e., 'importing' a class.

--
David Anton
www.tangiblesoftwaresolutions.com
Home of:
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter

chris said:
I converted to c# from VB.Net. But you guys are right. I changed it to
System.Convert.IsDBNull in the VB code and behaves in the same manner as the
c# version. Can you say "IsDBNull" in C# as well. Maybe not since C# would
not allow ambiguous coding.

Thanks Chris

Rahul Anand said:
I am also not sure but i hope the modification suggested by David will solve
the problem. As then the code will be similar to what you have implemented
under VB.NET

Just to point out: the IsDDNull checks for System.TypeCode.DBNull whereas
Sytem.DBNull.Value is an object of class System.DBNull

I dont know the exact difference between these two but i guess this
difference is causing the difference in behavior.

--
Cheers,
Rahul Anand

David Anton said:
I'm not sure if this is the problem, but I would use
"System.Convert.IsDBNull" as the replacement for the VB IsDBNull function.
--
David Anton
www.tangiblesoftwaresolutions.com
Home of:
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter

:

I had the following VB.Net code

ds.Tables.Add(BindData(camp_id, grid_id, cbo.SelectedValue, "CAMP"))
tbl = BindData(camp_id, grid_id, cbo.SelectedValue, "CHURN")

ds.Tables(0).ImportRow(tbl.Rows(0))

For i = 1 To ds.Tables(0).Columns.Count - 1
If IsDBNull(tbl.Rows(0).Item(i)) Or campCustomers = 0 Then
dr(i) = 0
dr1(i) = 0
Else
dr(i) = Format(tbl.Rows(0).Item(i) / CInt(campCustomers) * 100,
"0.00")
dr1(i) = Format(tbl.Rows(0).Item(i) / CInt(campCustomers) * 12 *
100, "0.00")
End If
next


Which I translated into C# like this

ds.Tables.Add(BindData(camp_id, grid_id, Convert.ToInt32(cbo.SelectedValue),
"CAMP"));
tbl = BindData(camp_id, grid_id, Convert.ToInt32(cbo.SelectedValue), "CHURN");

ds.Tables[0].ImportRow(tbl.Rows[0]);

for (int i = 1; i <= ds.Tables[0].Columns.Count - 1; i++)
{
if (ds.Tables[0].Rows[0] == DBNull.Value || campCustomers == 0)
{
dr = 0;
dr1 = 0;
}
else
{
dr = (Convert.ToDouble (tbl.Rows[0]) / campCustomers *
100).ToString("#0.00");
dr1 = (Convert.ToDouble(tbl.Rows[0]) / campCustomers * 12 *
100).ToString("#0.00");
}
}

The interesting part is that under some in C# “ds.Tables[0].Rows[0]†does
not contain the data but I could get the date to define
“ds.Tables[0].Rows[1]â€

And under some circumstances “ds.Tables.Add(BindData(camp_id, grid_id,
cbo.SelectedValue, "CAMP"))†returns a null value. Hence I have to access the
data with “ds.Tables[0].Rows[0]â€

In VB.Net that does not cause any problems it seems it always accesses the
“right†row even if “ds.Tables.Add(BindData(camp_id, grid_id,
cbo.SelectedValue, "CAMP"))†returns null.

I found a (dodgy) workaround in C# to test what value is in the row and then
define which rownumber to use.

If “ds.Tables.Add(BindData(camp_id, grid_id,
Convert.ToInt32(cbo.SelectedValue), "CAMP"));†did not return null I got
these values in the datasets rows

ds.Tables[0].Rows[0][0] "Campaign"
ds.Tables[0].Rows[1][0] "CHURN"

If it returned a null

ds.Tables[0].Rows[0][0] "Churn"
ds.Tables[0].Rows[1][0] error: object 'ds.Tables[0].Rows[1]' doesn't have
an indexer

So now I check

if (ds.Tables[0].Rows[0][0].ToString().Equals("Campaign"))
intRowId = 1;
else
intRowId = 0;

for (int i = 1; i <= ds.Tables[0].Columns.Count - 1; i++)
{
if (ds.Tables[0].Rows[intRowId] == DBNull.Value || campCustomers == 0)
{
dr = 0;
dr1 = 0;
……..

I am surprised that the C# translation does not behave in the same manner as
the VB.Net version. Can someone please explain why that is? Or did I
mistranslate?

I hope I did not confuse you guys to much….

Thanks a lot

Chris
 

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