Generate variable name on the fly

S

Steve Wasser

I've got 44 different terminals serving four grades of product each, and
it's been a bitch referencing them. I have one SQL table I'm pulling out
beginning inventory for each location. I've got another stored procedure
that pulls out inventory, ultimately I have to subtract by location, by
product.

Example
opening inventory table stores like:
terminal, starting_unl, starting_prem, starting_diesel, starting_ethanol

record one from the SP looks like:
transaction #, date, terminal, ethanol, premium, regular, diesel

So, I have case statements that look like this to keep a running tally of
what inventory has been pulled out:

case 210:
g210 =
g210+(decimal)dr["LiftingUnl"]+(decimal)dr["DeliveriesUnl"]+(decimal)dr["Adj
ustmentUnl"];
p210 =
p210+(decimal)dr["LiftingPrem"]+(decimal)dr["DeliveriesPrem"]+(decimal)dr["A
djustmentPrem"];
d210 =
d210+(decimal)dr["LiftingDiesel"]+(decimal)dr["DeliveriesDiesel"]+(decimal)d
r["AdjustmentDiesel"];
e210 =
e210+(decimal)dr["LiftingEth"]+(decimal)dr["DeliveriesEth"]+(decimal)dr["Adj
ustmentEth"];
prod[0] = g210+p210+d210+e210;
break;

Reason: the records the SP is pulling aren't sequential, they could be from
any terminal at any time, so I can't use a loop, I have to switch based on
the terminal field, then assign it to it's own products. This example is for
terminal 210. Fine. However, I was thinking for the final calculation it
would have been easy to set up an array with 44 location numbers (stored as
strings), then I could have calculated everything in a final for loop with a
nested foreach like
for (int x, x<44, x++)
{
foreach (string trm in terms)
{
finalunl[x]=finalunl[x]-"g"+trm;
}
}

Trying to make the variable name "g210" (as seen above).Thinking I could
avoid using MORE case statements, and form the name of the grade variable on
the fly. Of course, it didn't work, it just saw me trying to perform math
between a decimal and two strings. Is there a way to build a variable name
on the fly? It would condense 44 MORE case statements down to one nested
loop, and I'd be done.
 
N

Nicholas Paldino [.NET/C# MVP]

Steve,

I'm not exactly sure what you are doing here, but why not use a
Hashtable? You can store whatever you want in it, and key it off the
product ("210" for example). It should allow you to reduce your calcs down
to one set, assuming it is the same for all grades.

I would create a class that stores the d, p, g, and e values, create an
instance, set the fields, and then place them in the hashtable.

Also, I am curious, why don't you order the result set (either in the
stored procedure or the data set, using a DataView), so that it makes it
easier to perform the calcs?

Hope this helps.

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

Steve Wasser said:
I've got 44 different terminals serving four grades of product each, and
it's been a bitch referencing them. I have one SQL table I'm pulling out
beginning inventory for each location. I've got another stored procedure
that pulls out inventory, ultimately I have to subtract by location, by
product.

Example
opening inventory table stores like:
terminal, starting_unl, starting_prem, starting_diesel, starting_ethanol

record one from the SP looks like:
transaction #, date, terminal, ethanol, premium, regular, diesel

So, I have case statements that look like this to keep a running tally of
what inventory has been pulled out:

case 210:
g210 =
g210+(decimal)dr["LiftingUnl"]+(decimal)dr["DeliveriesUnl"]+(decimal)dr["Adj
ustmentUnl"];
p210 =
p210+(decimal)dr["LiftingPrem"]+(decimal)dr["DeliveriesPrem"]+(decimal)dr["A
djustmentPrem"];
d210 =
d210+(decimal)dr["LiftingDiesel"]+(decimal)dr["DeliveriesDiesel"]+(decimal)d
r["AdjustmentDiesel"];
e210 =
e210+(decimal)dr["LiftingEth"]+(decimal)dr["DeliveriesEth"]+(decimal)dr["Adj
ustmentEth"];
prod[0] = g210+p210+d210+e210;
break;

Reason: the records the SP is pulling aren't sequential, they could be from
any terminal at any time, so I can't use a loop, I have to switch based on
the terminal field, then assign it to it's own products. This example is for
terminal 210. Fine. However, I was thinking for the final calculation it
would have been easy to set up an array with 44 location numbers (stored as
strings), then I could have calculated everything in a final for loop with a
nested foreach like
for (int x, x<44, x++)
{
foreach (string trm in terms)
{
finalunl[x]=finalunl[x]-"g"+trm;
}
}

Trying to make the variable name "g210" (as seen above).Thinking I could
avoid using MORE case statements, and form the name of the grade variable on
the fly. Of course, it didn't work, it just saw me trying to perform math
between a decimal and two strings. Is there a way to build a variable name
on the fly? It would condense 44 MORE case statements down to one nested
loop, and I'd be done.
 
S

Steve Wasser

Does a dictionary allow me to store more than one value in the record? It
sounds like a good idea (or using an Array list), but everything I've read
looks like it is a key-data pair. Can I key to a whole recordset of data?
Let me explain and you, or someone might know the best way of doing this,
since my parameters have expanded a bit since my last post.

I am building a web app, and one component will keep track of petroleum
inventory. Users will enter an ending date, then a stored procedure will
pull a recordset of all the liftings (depletions or additions) that have
taken place at our terminals up to that date. There's 44 of terminals.
Calculations will then be done against a Day-1 inventory baseline and spit
out a report that shows the accountant a) how much was lifted on each day b)
give the inventory up to the current day specified. So really, each "page"
will be devoted to a single terminal, with a chart that breaks down activity
by day. So I am calculating a recordset pulled from a stored procedure
(liftings) against a query that pulls the recordset of beginning inventory.
Definitely sounds like a candidate for an Array List or dictionary, because
after I calculate, I still have to present it as daily activity, not just
grand totals (my old post was just trying to total up inventory, something
to work from)

Thanks,

--
Steve Wasser
http://xdissent.com
the journal of contrarian social discourse and neurotic opinion
Nicholas Paldino said:
Steve,

I'm not exactly sure what you are doing here, but why not use a
Hashtable? You can store whatever you want in it, and key it off the
product ("210" for example). It should allow you to reduce your calcs down
to one set, assuming it is the same for all grades.

I would create a class that stores the d, p, g, and e values, create an
instance, set the fields, and then place them in the hashtable.

Also, I am curious, why don't you order the result set (either in the
stored procedure or the data set, using a DataView), so that it makes it
easier to perform the calcs?

Hope this helps.

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

Steve Wasser said:
I've got 44 different terminals serving four grades of product each, and
it's been a bitch referencing them. I have one SQL table I'm pulling out
beginning inventory for each location. I've got another stored procedure
that pulls out inventory, ultimately I have to subtract by location, by
product.

Example
opening inventory table stores like:
terminal, starting_unl, starting_prem, starting_diesel, starting_ethanol

record one from the SP looks like:
transaction #, date, terminal, ethanol, premium, regular, diesel

So, I have case statements that look like this to keep a running tally of
what inventory has been pulled out:

case 210:
g210 =
g210+(decimal)dr["LiftingUnl"]+(decimal)dr["DeliveriesUnl"]+(decimal)dr["Adj
ustmentUnl"];
p210 =
p210+(decimal)dr["LiftingPrem"]+(decimal)dr["DeliveriesPrem"]+(decimal)dr["A
djustmentPrem"];
d210 =
d210+(decimal)dr["LiftingDiesel"]+(decimal)dr["DeliveriesDiesel"]+(decimal)d
r["AdjustmentDiesel"];
e210 =
e210+(decimal)dr["LiftingEth"]+(decimal)dr["DeliveriesEth"]+(decimal)dr["Adj
ustmentEth"];
prod[0] = g210+p210+d210+e210;
break;

Reason: the records the SP is pulling aren't sequential, they could be from
any terminal at any time, so I can't use a loop, I have to switch based on
the terminal field, then assign it to it's own products. This example is for
terminal 210. Fine. However, I was thinking for the final calculation it
would have been easy to set up an array with 44 location numbers (stored as
strings), then I could have calculated everything in a final for loop
with
a
nested foreach like
for (int x, x<44, x++)
{
foreach (string trm in terms)
{
finalunl[x]=finalunl[x]-"g"+trm;
}
}

Trying to make the variable name "g210" (as seen above).Thinking I could
avoid using MORE case statements, and form the name of the grade
variable
on
the fly. Of course, it didn't work, it just saw me trying to perform math
between a decimal and two strings. Is there a way to build a variable name
on the fly? It would condense 44 MORE case statements down to one nested
loop, and I'd be done.
 
B

Bob Grommes

Steve,

The value is of type Object, so you can put anything in there ... I have for
example one application that has SortedLists of Hashtables of Strings. All
you have to do is cast it back to the correct type when you extract it,
e.g., DataTable dt = (DataTable)myArrayList[intKey];

--Bob

Steve Wasser said:
Does a dictionary allow me to store more than one value in the record? It
sounds like a good idea (or using an Array list), but everything I've read
looks like it is a key-data pair. Can I key to a whole recordset of data?
Let me explain and you, or someone might know the best way of doing this,
since my parameters have expanded a bit since my last post.

I am building a web app, and one component will keep track of petroleum
inventory. Users will enter an ending date, then a stored procedure will
pull a recordset of all the liftings (depletions or additions) that have
taken place at our terminals up to that date. There's 44 of terminals.
Calculations will then be done against a Day-1 inventory baseline and spit
out a report that shows the accountant a) how much was lifted on each day b)
give the inventory up to the current day specified. So really, each "page"
will be devoted to a single terminal, with a chart that breaks down activity
by day. So I am calculating a recordset pulled from a stored procedure
(liftings) against a query that pulls the recordset of beginning inventory.
Definitely sounds like a candidate for an Array List or dictionary, because
after I calculate, I still have to present it as daily activity, not just
grand totals (my old post was just trying to total up inventory, something
to work from)

Thanks,

--
Steve Wasser
http://xdissent.com
the journal of contrarian social discourse and neurotic opinion
message news:%[email protected]...
Steve,

I'm not exactly sure what you are doing here, but why not use a
Hashtable? You can store whatever you want in it, and key it off the
product ("210" for example). It should allow you to reduce your calcs down
to one set, assuming it is the same for all grades.

I would create a class that stores the d, p, g, and e values, create an
instance, set the fields, and then place them in the hashtable.

Also, I am curious, why don't you order the result set (either in the
stored procedure or the data set, using a DataView), so that it makes it
easier to perform the calcs?

Hope this helps.

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

Steve Wasser said:
I've got 44 different terminals serving four grades of product each, and
it's been a bitch referencing them. I have one SQL table I'm pulling out
beginning inventory for each location. I've got another stored procedure
that pulls out inventory, ultimately I have to subtract by location, by
product.

Example
opening inventory table stores like:
terminal, starting_unl, starting_prem, starting_diesel, starting_ethanol

record one from the SP looks like:
transaction #, date, terminal, ethanol, premium, regular, diesel

So, I have case statements that look like this to keep a running tally of
what inventory has been pulled out:

case 210:
g210 =
g210+(decimal)dr["LiftingUnl"]+(decimal)dr["DeliveriesUnl"]+(decimal)dr["Adj
ustmentUnl"];
p210 =
p210+(decimal)dr["LiftingPrem"]+(decimal)dr["DeliveriesPrem"]+(decimal)dr["A
djustmentPrem"];
d210 =
d210+(decimal)dr["LiftingDiesel"]+(decimal)dr["DeliveriesDiesel"]+(decimal)d
r["AdjustmentDiesel"];
e210 =
e210+(decimal)dr["LiftingEth"]+(decimal)dr["DeliveriesEth"]+(decimal)dr["Adj
ustmentEth"];
prod[0] = g210+p210+d210+e210;
break;

Reason: the records the SP is pulling aren't sequential, they could be from
any terminal at any time, so I can't use a loop, I have to switch
based
on
the terminal field, then assign it to it's own products. This example
is
for
terminal 210. Fine. However, I was thinking for the final calculation it
would have been easy to set up an array with 44 location numbers
(stored
as
strings), then I could have calculated everything in a final for loop
with
a
nested foreach like
for (int x, x<44, x++)
{
foreach (string trm in terms)
{
finalunl[x]=finalunl[x]-"g"+trm;
}
}

Trying to make the variable name "g210" (as seen above).Thinking I could
avoid using MORE case statements, and form the name of the grade
variable
on
the fly. Of course, it didn't work, it just saw me trying to perform math
between a decimal and two strings. Is there a way to build a variable name
on the fly? It would condense 44 MORE case statements down to one nested
loop, and I'd be done.
 
S

Steve Wasser

Because I'm learning as I go...I'm new to c#

--
Steve Wasser
http://xdissent.com
the journal of contrarian social discourse and neurotic opinion
Nicholas Paldino said:
Steve,

I'm not exactly sure what you are doing here, but why not use a
Hashtable? You can store whatever you want in it, and key it off the
product ("210" for example). It should allow you to reduce your calcs down
to one set, assuming it is the same for all grades.

I would create a class that stores the d, p, g, and e values, create an
instance, set the fields, and then place them in the hashtable.

Also, I am curious, why don't you order the result set (either in the
stored procedure or the data set, using a DataView), so that it makes it
easier to perform the calcs?

Hope this helps.

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

Steve Wasser said:
I've got 44 different terminals serving four grades of product each, and
it's been a bitch referencing them. I have one SQL table I'm pulling out
beginning inventory for each location. I've got another stored procedure
that pulls out inventory, ultimately I have to subtract by location, by
product.

Example
opening inventory table stores like:
terminal, starting_unl, starting_prem, starting_diesel, starting_ethanol

record one from the SP looks like:
transaction #, date, terminal, ethanol, premium, regular, diesel

So, I have case statements that look like this to keep a running tally of
what inventory has been pulled out:

case 210:
g210 =
g210+(decimal)dr["LiftingUnl"]+(decimal)dr["DeliveriesUnl"]+(decimal)dr["Adj
ustmentUnl"];
p210 =
p210+(decimal)dr["LiftingPrem"]+(decimal)dr["DeliveriesPrem"]+(decimal)dr["A
djustmentPrem"];
d210 =
d210+(decimal)dr["LiftingDiesel"]+(decimal)dr["DeliveriesDiesel"]+(decimal)d
r["AdjustmentDiesel"];
e210 =
e210+(decimal)dr["LiftingEth"]+(decimal)dr["DeliveriesEth"]+(decimal)dr["Adj
ustmentEth"];
prod[0] = g210+p210+d210+e210;
break;

Reason: the records the SP is pulling aren't sequential, they could be from
any terminal at any time, so I can't use a loop, I have to switch based on
the terminal field, then assign it to it's own products. This example is for
terminal 210. Fine. However, I was thinking for the final calculation it
would have been easy to set up an array with 44 location numbers (stored as
strings), then I could have calculated everything in a final for loop
with
a
nested foreach like
for (int x, x<44, x++)
{
foreach (string trm in terms)
{
finalunl[x]=finalunl[x]-"g"+trm;
}
}

Trying to make the variable name "g210" (as seen above).Thinking I could
avoid using MORE case statements, and form the name of the grade
variable
on
the fly. Of course, it didn't work, it just saw me trying to perform math
between a decimal and two strings. Is there a way to build a variable name
on the fly? It would condense 44 MORE case statements down to one nested
loop, and I'd be done.
 
Top