CoolArchTek
CoolArchTek

Reputation: 3829

C# Implicitly Typed Variable re-initialize

How do I re-initialize a Implicitly Typed Variable (var) in C#?

            var abc = new Class();
            if (a == 1)
            {
                abc = new Class1();
            }
            else if (a == 2)
            {
                abc = new Class2();
            }
            else if (a == 3)
            {
                abc = new Class3();
            }

Right now I have it like above and I am getting an error.

Cannot implicitly convert type 'Class1' to 'Class'

Upvotes: 3

Views: 1068

Answers (9)

John
John

Reputation: 111

A little late to the party but why not:

if (a == 1)
{
    var abc = new Class1();
}
else if (a == 2)
{
    var abc = new Class2();
}
else if (a == 3)
{
    var abc = new Class3();
}
else
{
    var abc = new Class();
}

Upvotes: -1

Omar
Omar

Reputation: 16623

You can do this var abc = new Class();, beacuse var is a generic type and when you do this you are doing exactly Class abc = new Class();. The problem is when you try to do re-initalize. But you wrong,after when you do abc = new Class1();, you're not reinitializing abc but you're passing the pointer of the new object of type Class. So the compiler say that you cannot do this convertion, beacuse the type of abc is Class.

But you can use object, as said Mark Backett, because the type object is the base class of all classes.

Also a solution could be something like this:

int a = Convert.ToInt(Console.ReadLine());

var abc = new Class();

if (a == 1)
    abc = new Class1();
else if (a == 2)
    abc = new Class2();
else if (a == 3)
    abc = new Class3();

public class Class { ... }
public class Class1:Class{ ... }
public class Class2:Class1{ ... }
public class Class3:Class2{ ... }

Upvotes: 2

rerun
rerun

Reputation: 25495

You Can't do what you ask but you can do this.

      object abc = new Class();
        if (a == 1)
        {
            abc = new Class1();
        }
        else if (a == 2)
        {
            abc = new Class2();
        }
        else if (a == 3)
        {
            abc = new Class3();
        }

While this doesn't provide a lot of value. If all of the classes inherit from a common class or interface that does some work you need to do it may be what you are going for. So for example

        Animal abc  = new Animal();
        if (a == 1)
        {
            abc = new Dog();
        }
        else if (a == 2)
        {
            abc = new Cat();
        }
        else if (a == 3)
        {
            abc = new Person();
        }
        abc.sleep() 

The sleep would then invoke the correct function for which ever type it accentual was.

Upvotes: 2

Mark Brackett
Mark Brackett

Reputation: 85625

An implicitly typed variable still respects polymorphism - so your inferred variable is of type Class:

Class abc = new Class();

Since Class1 does not (presumably) inherit from Class, it's invalid to reassign. If you want to type an inferred variable to a less restrictive base type (eg., object), you can either write out the declaration (preferred) or cast the right hand side so that the compiler infers a different type:

object abc = new Class(); // Preferred
var abc = (object)new Class(); // Works

Upvotes: 0

Joachim Isaksson
Joachim Isaksson

Reputation: 180867

Implicit typing with var is still static typing

var abc = new Class();

is exactly equivalent to

Class abc = new Class();

Unless Class1, Class2 and Class3 extend Class, you can't assign them to abc.

Upvotes: 7

jason
jason

Reputation: 241583

You can't. var doesn't work like that. var means "look, I'm too lazy to type out the real statically typed name of this variable, can you just figure it out for me?" That's what implicit typing means.

So, on the first line, the compiler decides that abc is typed as a reference to Class. Then, you later try to assign a reference to Class1 to abc. Of course that's impossible unless there's a conversion from Class1 to Class.

To stress, your code is as if you'd written

Class abc = new Class();
if (a == 1) {
    abc = new Class1();
}
else if (a == 2) {
    abc = new Class2();
}
else if (a == 3) {
    abc = new Class3();
}

In fact, it's semantically identical. But now your error is obvious. You're trying to assign a reference to Class1 to abc but abc can't accept that unless there's an implicit conversion from Class1 to Class.

Upvotes: 7

James Michael Hare
James Michael Hare

Reputation: 38397

You don't. Implicit initialization is determined by the compiler at the point of assignment, thus your first line:

var abc = new Class();

Is equivalent to:

Class abc = new Class();

So once it's determined to be of type Class it can't be changed any more than the explicit declaration can.

Perhaps you were wanting to do dynamic typing instead?

Upvotes: 2

Jon
Jon

Reputation: 437326

You cannot do this. It's just an equivalent way of writing

Class abc = new Class(); 
if (a == 1) 
{ 
    abc = new Class1(); // fails or requires implicit conversion
} 

which obviously would not work in the general case.

Upvotes: 0

Matthew
Matthew

Reputation: 25743

You can't, in that scenario you can only do that if Class1/2/3 explicitly inherit from Class.

Upvotes: 3

Related Questions