passing object as value problem

  • Thread starter Thread starter J055
  • Start date Start date
J

J055

Hi

I'm a little confused by this so would appreciate some advice on ways to
deal with the following. When I call the CodeMaker.Update static method for
the second time I want the dt parameter to have the same value as when
called the first time. What would be the best approach (and simplest) way to
deal with this?

Cheers
Andrew
(still quite new to C# and DOTNET)

class mainclass
{
private void BuildProcsOutputText(DataTable dt)
{
StringBuilder sb = new StringBuilder();
sb.Append(CodeMaker.Update(dt));
sb.Append(CodeMaker.Update(dt));

Output.Text = sb.ToString();
}
}

class CodeMaker
{
public static string Update(DataTable dt, string tableName)
{
dt.Rows.Remove(dt.Rows[0]);

// do something with the DataTable and return a string

return str;
}
}
 
When you say you want it to have the same value, do you mean that it
must be the same object?

I'd think that a simple check in the beginning of Update would do it..
if ( dt == lastDt), where lastDt is a static field in the CodeMaker
class.
 
in your CodeMaker.Update method, copy the dt to a local variable, then
your dt reference will be unchanged

public static string Update(DataTable dt, string tableName) {
DataTable tempDT = dt;
tempDT.Rows.Remove(dt.Rows[0]);
//do something
return str;
}

This way, you are working on a copy of the variable and not the
reference
 
J055 said:
Hi

I'm a little confused by this so would appreciate some advice on ways to
deal with the following. When I call the CodeMaker.Update static method
for the second time I want the dt parameter to have the same value as when
called the first time. What would be the best approach (and simplest) way
to deal with this?

Cheers
Andrew
(still quite new to C# and DOTNET)

class mainclass
{
private void BuildProcsOutputText(DataTable dt)
{
StringBuilder sb = new StringBuilder();
sb.Append(CodeMaker.Update(dt));
sb.Append(CodeMaker.Update(dt));

Output.Text = sb.ToString();
}
}

class CodeMaker
{
public static string Update(DataTable dt, string tableName)
{
dt.Rows.Remove(dt.Rows[0]);

// do something with the DataTable and return a string

return str;
}
}

Pass in a copy of the table. Ex:

DataTable tmpDT = dt.Copy();
sb.Append(CodeMaker.Update(tmpDT);
 
Hi

I've done exactly this but tempDT still seems to be a reference to dt.

Still confused.
Sorry
Andrew
 
J055 said:
I'm a little confused by this so would appreciate some advice on ways to
deal with the following. When I call the CodeMaker.Update static method for
the second time I want the dt parameter to have the same value as when
called the first time. What would be the best approach (and simplest) way to
deal with this?

Well, strictly speaking the dt parameter *will* have the same value -
in that it's the same reference. Because DataTable is a reference type,
the value of dt is a reference. That reference is passed by value. It's
very important that you understand this - that the value of dt is *not*
an object itself. Once you understand that, life becomes a lot simpler
:)

As Tom said, you need the DataTable.Copy method to create a copy of the
table itself.

See http://www.pobox.com/~skeet/csharp/parameters.html for more about
parameter passing.
 
It's unclear to me exactly what it is you want here. The code snippets
you gave do exactly what you asked: the second call to Update uses the
same table as the first call to Update, because your
BuildProcsOutputText method passed the same data table twice.

Do you mean that you want to call Update successive times and each time
remove a row from the table and return a string, in other words return
a string for each row in the table? There are several easier ways to do
this, but which one is right for you depends upon whether you really
have to do it in multiple calls, and whether removing a row from the
table each time is really central to your algorithm, or merely a way of
making sure that you get a new row each time.

If all you want is to generate a string for each row in the table, I
would much rather do it like this:

public static string[] Update(DataTable dt)
{
string[] result = new string[dt.Rows.Count];

... process the entire table and return the string array...
}

On the other hand, if you really have to call Update multiple times and
process one row at a time, then it makes more sense not to make it
static, but to wrap the DataTable in some sort of processing object,
thus:

public class ProcessDataTable
{
private DataTable _table;

public ProcessDataTable(DataTable dt)
{
this._table = dt;
}

public string Update()
{
... do something with table this._table and return a string...
}
}

That way your caller doesn't have to make sure to pass the same data
table each time. Instead, the caller does this:

ProcessDataTable proc = new ProcessDataTable(dt);
proc.Update();
proc.Update();
.... etc ...

until it returns a null string or something to indicate (no more rows).
Even this is a rather odd construction, it being more usual to have the
main program handle the looping throught the rows, like this:

foreach (DataRow row in dt.Rows)
{
CodeMaker.Update(row);
}

or something similar.

As you can see, there are a lot of different solutions. It all depends
upon exactly what your requirements are.
 
Thanks for all the help.

I only wanted to manipulate the DataTable within the CodeMaker.Update method
so I've added

DataTable tmpDT = dt.Copy();

to it , as Tom suggested, and it does what I want now. I now need to read up
on value and reference types to get a better understanding.

CHeers
Andrew
 

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

Back
Top