0

I'm referring to my snippet Inheriting the stack class

After looong loong searches and many tries and tests, I finally came up with a version that works as I wanted to.

Stacker can now handle any type of object, even with arethmetic methods in place. The virtual way as proposed by JOShealV would work as well, but then you had to derive from Stacker and do some overriding.

Still two questions though:

1-- How should I handle nullable value types?

2-- What could be returned by the arithmetic methods? Nothing like now, should I throw an exception?

Any suggestions more than welcome, thanks in advance. :)

```
//Here is my changed code for reference
namespace StackTest
{
class Program
{
struct MyStruct
{
public int i;
public int MyMet()
{
return 42;
}
}
class Person
{
public string Name;
public int ID;
}
static void Main(string[] args)
{
Person P1 = new Person { Name = "Tom", ID = 123 };
Person P2 = new Person { Name = "Mary", ID = 124 };
Stacker<Person> SP = new Stacker<Person>();
SP.Push(P1);
SP.Push(P2);
SP.ADD();//no more exception !!
Stacker<double> SD = new Stacker<double>();
SD.Push(2);
SD.Push(4.0);
SD.Push(3.0);
SD.DUP();
SD.MUL();
Console.WriteLine("The square of 3 is {0}.", SD.Peek());
SD.ADD();
Console.WriteLine("The sum is {0}.", SD.Peek());
Stacker<MyStruct> SStr = new Stacker<MyStruct>();
MyStruct S1;
MyStruct S2;
S1.i = 123;
S2.i = 456;
SStr.Push(S1);
SStr.Push(S2);
Console.WriteLine("before {0}.", SStr.Peek().i);
SStr.ADD(); //no more exception
Console.WriteLine("The sum is {0}.", SStr.Peek().i);//just check everything is the same
Console.ReadKey();
}
}
}
//==============================================================================
using System.Collections.Generic;
using System;
using Microsoft.CSharp.RuntimeBinder;
namespace StackTest
{
class Stacker<T> : Stack<T>
{
public Stacker()
{ }
/// <summary>
/// Check generic type if it is numeric
/// for the arithmetic stack methods
/// Notice the use of default here
/// See:https://msdn.microsoft.com/en-us/library/xwth0h0d.aspx
/// </summary>
/// <returns></returns>
bool IsNumeric()
{
try
{
dynamic temp = default(T);
bool b = temp == 0;
return b;
}
// Include Microsoft.CSharp in references
// and using Microsoft.CSharp.RuntimeBinder
catch (RuntimeBinderException)//handle stacks
{
return false;
}
}
/// <summary>
/// Duplicate top of stack: n -- nn
/// </summary>
public void DUP()
{
T v = this.Pop(); this.Push(v); this.Push(v);
}
/// <summary>
/// Multiply(if possible) the two top items on the stack
/// replace with the result
/// </summary>
public void MUL()
{
if ((Count >= 2) && IsNumeric())
{
dynamic v1 = Pop();
dynamic v2 = Pop();
Push(v1 * v2);
}
else
{
}
}
/// <summary>
/// Sum(if possible) the two top items on the stack
/// replace with the result
/// </summary>
public void ADD()
{
if ((Count >= 2) && IsNumeric())
{
dynamic v1 = Pop();
dynamic v2 = Pop();
Push(v1 + v2);
}
else
{
}
}
/// <summary>
/// Substract(if possible) the two top items on the stack
/// replace with the result
/// </summary>
public void SUB()
{
if ((Count >= 2) && IsNumeric())
{
dynamic v1 = Pop();
dynamic v2 = Pop();
Push(v2 - v1);
}
else
{
}
}
/// <summary>
/// Divide(if possible) the two top items on the stack
/// replace with the result
/// </summary>
public void DIV()
{
if ((Count >= 2) && IsNumeric())
{
dynamic v1 = Pop();
dynamic v2 = Pop();
Push(v1 / v2);
}
else
{
}
}
/// <summary>
/// Negate(if possible) the top item on the stack
/// replace with the result
/// </summary>
public void NEG()
{
dynamic v1 = Pop();
Push(-v1);
}
/// <summary>
/// Rotate the 3 th item to the top of the stack: n1 n2 n3 -- n2 n3 n1
/// </summary>
public void ROT()
{
if (Count >= 3)
{
dynamic v3 = Pop();
dynamic v2 = Pop();
dynamic v1 = Pop();
Push(v2);
Push(v3);
Push(v1);
}
}
/// <summary>
/// Remove top item
/// </summary>
public void DROP()
{
T v = Pop();
}
/// <summary>
/// Swap the two topmost items on the stack:n1 n2 -- n2 n1
/// </summary>
public void SWAP()
{
if (Count >= 2)
{
T v1 = Pop();
T v2 = Pop();
Push(v1);
Push(v2);
}
}
/// <summary>
/// Copy the second item and make it the topmost:n1 n2 -- n1 n2 n1
/// </summary>
public void OVER()
{
if (Count >= 2)
{
T v1 = Pop();
T v2 = Pop();
Push(v1);
Push(v2);
Push(v1);
}
}
}
}
```