Efficient stack usage

G

Guest

Dear NG,

I am making progress processing trees. I can "span" a tree if I make wise
use of the C# stack methods, push() and pop(). What I want to place on the
stack are rows from a data table. The columns (about a dozen) have string,
decimal, and int data in them. The following code concatinates (tab
delimited) a row and places it on the stack sucessfully. Is there any way to
do this without packing and unpacking the columns into and out of a string?
In other words, place the whole row on the stack? Thanks, Bob

private void button1_Click(object sender, System.EventArgs e)
{
SqlConnection mcs3Connection = MCS3_DB.GetConnection();
string selectStmt = "SELECT * FROM ProdStrt WHERE ps_parent = '500-000'
ORDER BY ps_parent";
SqlCommand selectCmd = new SqlCommand(selectStmt, mcs3Connection);
SqlDataAdapter dataAdapter = new SqlDataAdapter(selectCmd);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet, "PS"); //dataSet has 1 DataTable IM
// Moving through a table:
int nRows = dataSet.Tables["PS"].Rows.Count;
string strRows = nRows.ToString();
Stack myStack = new Stack();
string strTemp;
MessageBox.Show("Number of rows: " + strRows);
for (int i = 0; i < nRows; i++)
{
strTemp = dataSet.Tables["PS"].Rows["ps_component"].ToString() + "\t" +
dataSet.Tables["PS"].Rows["ps_parent"].ToString();
myStack.Push(strTemp);
MessageBox.Show("entry: " + strTemp, "Info");
}
for (int i = 0; i < nRows; i++)
{
strTemp = (string) myStack.Pop();
MessageBox.Show("exit: " + strTemp, "Info");
}

}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I really do not understand what you are trying to do, what a tree has to do
with a datatable or a stack?

if you want to store a "row" in a stack, why dont you get it as a single
field from the DB and just store this value in the stack?

beware that the char you use as column delimiter may appear inside a string
column.

Cheers,
 
N

Nicholas Paldino [.NET/C# MVP]

Robert,

There is no reason why you shouldn't be able to store the DataRow itself
to the stack.

However, what I don't understand is why you don't just use the DataTable
class, and add a new row. Add a column which is a number, and when you add,
populate it with the number of rows currently in the table. Then, when you
pop off the stack, just remove the row at count - 1.

Hope this helps.
 
G

Guest

Hi Nick,

Thank you for the reply. I tried to push(dataSet.Tables["PS"].Rows) on
to the stack, but I recieved an error. Forgot what it was, but if you think
that is the right thing, I will try again. I am not sure what you are
suggesting with your idea to: "just use the DataTable class, and add a new
row. Add a column which is a number, and when you add, populate it with the
number of rows currently in the table."

I need to push and pop certain rows on and off the stack to "span" the tree.
My example does not show the tree processing yet. While I am waiting your
suggestion, I am developing the added processing using the string approach.
Later on I can go for the more efficient method. Thanks for your help. Bob

Nicholas Paldino said:
Robert,

There is no reason why you shouldn't be able to store the DataRow itself
to the stack.

However, what I don't understand is why you don't just use the DataTable
class, and add a new row. Add a column which is a number, and when you add,
populate it with the number of rows currently in the table. Then, when you
pop off the stack, just remove the row at count - 1.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Robert Schuldenfrei said:
Dear NG,

I am making progress processing trees. I can "span" a tree if I make wise
use of the C# stack methods, push() and pop(). What I want to place on
the
stack are rows from a data table. The columns (about a dozen) have
string,
decimal, and int data in them. The following code concatinates (tab
delimited) a row and places it on the stack sucessfully. Is there any way
to
do this without packing and unpacking the columns into and out of a
string?
In other words, place the whole row on the stack? Thanks, Bob

private void button1_Click(object sender, System.EventArgs e)
{
SqlConnection mcs3Connection = MCS3_DB.GetConnection();
string selectStmt = "SELECT * FROM ProdStrt WHERE ps_parent = '500-000'
ORDER BY ps_parent";
SqlCommand selectCmd = new SqlCommand(selectStmt, mcs3Connection);
SqlDataAdapter dataAdapter = new SqlDataAdapter(selectCmd);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet, "PS"); //dataSet has 1 DataTable IM
// Moving through a table:
int nRows = dataSet.Tables["PS"].Rows.Count;
string strRows = nRows.ToString();
Stack myStack = new Stack();
string strTemp;
MessageBox.Show("Number of rows: " + strRows);
for (int i = 0; i < nRows; i++)
{
strTemp = dataSet.Tables["PS"].Rows["ps_component"].ToString() + "\t" +
dataSet.Tables["PS"].Rows["ps_parent"].ToString();
myStack.Push(strTemp);
MessageBox.Show("entry: " + strTemp, "Info");
}
for (int i = 0; i < nRows; i++)
{
strTemp = (string) myStack.Pop();
MessageBox.Show("exit: " + strTemp, "Info");
}

}

 

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