easy problem about ToString

T

Tony

Hello!

Below is a method called GenerateGarmentDataSet which is passed a DataSet.
In this DataSet we have a DataTable with some columns and a couples of
DataRows.

Now to a strange thing. When I execute this statement(the row below) I have
to use ToString() before I can use Substring
newRow["BatchNo"] = (row["BatchRowId"]).ToString().Substring(4, 3);
but I have checked with this statement
Type type = row["BatchRowId"].GetType();
and the type is string so why do I need to use ToString when I already have
a string ?


private DataSet GenerateGarmentDataSet(DataSet ds)
{
DataSet nDs = new DataSet();
DataTable dt = CreateGarmentdDataTable();

foreach (DataRow row in ds.Tables[0].Rows)
{
DataRow newRow = dt.NewRow();

Type type = row["BatchRowId"].GetType();
newRow["BatchNo"] =
(row["BatchRowId"]).ToString().Substring(4, 3);
newRow["Trolley"] =
(row["BatchRowId"]).ToString().Substring(7,3);
newRow["Status"] = row["Status"];
newRow["OperationStep"] = row["Step"];
newRow["User"] = row["FirstName"].ToString() + " " +
row["LastName"].ToString();
newRow["StartTime"] = row["StartTime"];
dt.Rows.Add(newRow);
}
nDs.Tables.Add(dt);
return nDs;
}

//Tony
 
A

Alberto Poblacion

Tony said:
Now to a strange thing. When I execute this statement(the row below) I
have to use ToString() before I can use Substring
newRow["BatchNo"] = (row["BatchRowId"]).ToString().Substring(4, 3);
but I have checked with this statement
Type type = row["BatchRowId"].GetType();
and the type is string so why do I need to use ToString when I already
have a string ?

row["..."] is of type Object. You can have anything inside an object,
including a String, but at compile time the compiler doesn't know what it is
going to contain at runtime. So if you try to call .Substring, it will
complain because Object does not have a Substring method.

If YOU know that your row[...] is always going to contain a String (the
compiler is unable to know it) you can inform it to the compiler with a
cast, and then it will be possible to invoke Substring:

result = ((string)row["BatchRowId"]).Substring(4, 3);
 
L

Leon

Hello!

Below is a method called GenerateGarmentDataSet which is passed a DataSet.
In this DataSet we have a DataTable with some columns and a couples of
DataRows.

Now to a strange thing. When I execute this statement(the row below) I have
to use ToString() before I can use Substring
newRow["BatchNo"] = (row["BatchRowId"]).ToString().Substring(4, 3);
but I have checked with this statement
Type type = row["BatchRowId"].GetType();
and the type is string so why do I need to use ToString when I already have
a string ?

[snip code]

//Tony

Hi Tony,

You're refering the to DataColumn - which, in this case, has the
underlaying Valuetype string.
And since the Value property for the DataColumn could have any type
(or object actually) you'll need that specific cast to String.

The GetType() operation is useless btw, since you're not refering to
it after you've requested it - you just get the Type to "type" and
you're not doing anything with it. (unless you've added it for
debugging purposes ofcourse)

gr.L
 
A

amdrit

Something else to consider is how many cycles you are consuming with your
code. Granted the code you are running is rather simple, the habit would be
to minimize cycles where you can. Convert your data to the target type once
and then work against that, rather than converting each time you wish to
access it. A datareader (VS '05 or greater) here might prove to be useful
here too.

Note, this is just my take on it and does not mean it is the best or only
way to go. Obviously, you are having some success with your implementation.


public DataSet GenerateGarmentDataSet(DataSet ds)
{
DataTable sourceTable = ds.Tables[0];
DataSet nDs = new DataSet();
DataTable dt = CreateGarmentdDataTable();

using (DataTableReader dtr = sourceTable.CreateDataReader())
{
while (dtr.Read())
{
string tempString = string.Empty;
DataRow newRow = dt.NewRow();
int ordinal = -1;

ordinal = dtr.GetOrdinal("BatchRowId");
if (!dtr.IsDBNull(ordinal))
{
tempString = dtr.GetString(ordinal);

newRow["BatchNo"] = tempString.Substring(4, 3);
newRow["Trolley"] = tempString.Substring(7, 3);
}

ordinal = dtr.GetOrdinal("Status");
if (!dtr.IsDBNull(ordinal))
{
newRow["Status"] = dtr.GetString(ordinal);
}

ordinal = dtr.GetOrdinal("Step");
if (!dtr.IsDBNull(ordinal))
{
newRow["OperationStep"] = dtr.GetString(ordinal);
}

tempString = string.Empty;

ordinal = dtr.GetOrdinal("FirstName");
if (!dtr.IsDBNull(ordinal))
{
tempString = dtr.GetString(ordinal) + " ";
}

ordinal = dtr.GetOrdinal("LastName");
if (!dtr.IsDBNull(ordinal))
{
tempString += dtr.GetString(ordinal);
}

newRow["User"] = tempString;

ordinal = dtr.GetOrdinal("StartTime");
if (!dtr.IsDBNull(ordinal))
{
newRow["StartTime"] = dtr.GetDateTime(ordinal);
}

dt.Rows.Add(newRow);

}
}

nDs.Tables.Add(dt);
return nDs;
}


Tony said:
Hello!

Below is a method called GenerateGarmentDataSet which is passed a DataSet.
In this DataSet we have a DataTable with some columns and a couples of
DataRows.

Now to a strange thing. When I execute this statement(the row below) I
have to use ToString() before I can use Substring
newRow["BatchNo"] = (row["BatchRowId"]).ToString().Substring(4, 3);
but I have checked with this statement
Type type = row["BatchRowId"].GetType();
and the type is string so why do I need to use ToString when I already
have a string ?


private DataSet GenerateGarmentDataSet(DataSet ds)
{
DataSet nDs = new DataSet();
DataTable dt = CreateGarmentdDataTable();

foreach (DataRow row in ds.Tables[0].Rows)
{
DataRow newRow = dt.NewRow();

Type type = row["BatchRowId"].GetType();
newRow["BatchNo"] =
(row["BatchRowId"]).ToString().Substring(4, 3);
newRow["Trolley"] =
(row["BatchRowId"]).ToString().Substring(7,3);
newRow["Status"] = row["Status"];
newRow["OperationStep"] = row["Step"];
newRow["User"] = row["FirstName"].ToString() + " " +
row["LastName"].ToString();
newRow["StartTime"] = row["StartTime"];
dt.Rows.Add(newRow);
}
nDs.Tables.Add(dt);
return nDs;
}

//Tony
 

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