1

How to place the object quantity into a _StoredStock? The quantity is an object which I find it hard to convert it to int to place the value into _StoredStock.

class EachStock: Stock
{

    public override void bookStock(Quantity quantity)
    {
        if (quantity.GetType() == typeof(EachStockQuantity))
        {
            //How to placethe object quantity into a _BookedStock
            // the quantity is an object which I find it hard to convert it to int for _StoredStock
        }
    }

}

3 Answers3

1

You're trying to store a class instance in your integer variable. I think part of the issue is that the parameter name is misleading, as it's not a quantity, but a class with a Quantity property.

Access the property you need, in this case Quantity:

public override void AdjustStock(StockQuantity stockQty)
{
    if (stockQty.GetType() == typeof(DiscreteStockQuantity))
        _StoredStock = ((DiscreteStockQuantity)stockQty).Quantity;
}

Alternatively, and possibly slightly more readable (but that's a matter of opinion):

public override void AdjustStock(StockQuantity stockQty)
{
    var discStockQty = stockQty as DiscreteStockQuantity;

    if (discStockQty != null)
        _StoredStock = discStockQty.Quantity;
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

There are different ways to know the type and do the actual conversion.

Here is a lazy example:

class Program
{
    static void Main(string[] args)
    {

        object obj = new Foo();


        Type objType = obj.GetType();

        if(objType == typeof(Foo))
        {
            var foobar = (Foo)obj;

            Console.WriteLine(foobar.Bar);
        }
        else if(objType == typeof(int))
        {
            // Do something
        }
        else
        {
            // Do something
        }

        // If you are brave enough you can use dynamic

        dynamic bar = obj;

        Console.WriteLine(bar.Bar);

        Console.ReadLine();
    }
}

class Foo
{
    public string Bar { get; set; }

    public Foo()
    {
        Bar = "FooBar";
    }
}

This outputs:

FooBar
FooBar

Update:

Here's an example similar to your problem

class Program
{
    static void Main(string[] args)
    {
        int i = FooBar(new Foo());

        Console.WriteLine(i);

        Console.ReadLine();
    }

    public static int FooBar(FooBase fooBase)
    {
        int val = -1;

        Type type = fooBase.GetType();

        if(type == typeof(Foo))
        {
            Foo foo = (Foo)fooBase;

            val = foo.Bar;
        }
        else
        {
            // Do something
        }

        return val;
    }
}

class Foo : FooBase
{
    public int Bar { get; set; }

    public Foo()
    {
        Bar = 999;
    }
}

class FooBase
{

}

Outputs:

999
jegtugado
  • 5,081
  • 1
  • 12
  • 35
0

Try this:

public override void AdjustStock(StockQuantity quantity)
    {
        if (quantity is DiscreteStockQuantity)
        {
            var dq = quantity as DiscreteStockQuantity;
            _StoredStock = dq.Quantity;
        }
    }
skofman
  • 41
  • 3