3

hi i have a var variable and i need initialize it based on if statement this is my code:

var series = new ColumnSeries{};
if(integer == 0)
  series = new LineSeries{};
else if (integer == 1)
  series = new PieSeries{};
else if (integer == 2)
  series = new AreaSeries{};

but it get error that i cant initialize variable more than once, so how i can initialize this variable more than once? i need change my chart type based on if statement so i tried this way.

  • 5
    You can initialize it more than once, but it has to be of the same type. So if `LineSeries`,`PieSeries`,`AreaSeries`are not `ColumnSeries` this won't compile. – Tim Schmelter May 18 '18 at 15:21
  • @TimSchmelter so what should I do? –  May 18 '18 at 15:23
  • 1
    They all look to be under the abstract class LiveCharts.Wpf.Series – Andrew May 18 '18 at 15:24
  • You could make a series base class that ColumnSeries, LineSeries, PieSeries and AeraSeries extend. Cast the base var to the extension class as needed. – Lee Toffolo May 18 '18 at 15:25

5 Answers5

7

var is implicit typing; in your case it assumes that it should be a ColumnSeries.

To use a base class (so other derived classes can be assigned to it) you could cast the initialized object or just don't use var.

Series series = new ColumnSeries(); //Preferred. var is just a bad choice here
var series = (Series)new ColumnSeries();
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
0

They need to be the same type. Program to a common Interface for all of them, something like ISeries.

alwill
  • 21
  • 1
  • 6
0

By instantiating it as a ColumnSeries to begin with, you will be unable to assign it to a different type later (unless it's a type that implements ColumnSeries).

Instead, you can declare it as a common base type (or interface) and then later assign it to any type that implements that base class or interface:

Series series = 
    integer == 0
        ? new LineSeries()
        : integer == 1
            ? new PieSeries()
            : integer == 2
                ? new AreaSeries()
                :  new ColumnSeries();
Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

Though I recommend against this, you can use the type dynamic which can change it's type on usage, so you can do:

dynamic series = new ColumnSeries{};

if(integer == 0)
   series = new LineSeries{};
else if (integer == 1)
   series = new PieSeries{};
else if (integer == 2)
   series = new AreaSeries{};

And it's type will change to LineSeries(), PieSeries, and AreaSeries respectively. This also means that the compiler won't know what functions it has when compiling, so you get a lot less safety and more execution errors, and less IDE/Auto-complete support too.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
0

Although this is not the best use of var and object, I think we could do something like this:

var series = new object();

Keep in mind though that after you assign something to it, it will remain an object and you will have to cast it to the type you want.

if(integer == 0)
    series = new LineSeries{};
else if (integer == 1)
    series = new PieSeries{};
else if (integer == 2)
    series = new AreaSeries{};

Lets say the first condition is satisfied in order to get access to series properties you need to cast it to a LineSeries.

You could also do:

dynamic series = new object();

In this case once you have assigned something to it, it wont be an object so no need for casting but as Austin Bryan mentioned in his answer above you loose other compiler and IDE benefits.

devtoka
  • 411
  • 3
  • 13