30

I have this code:

Type leftType = workItem[LeftFieldName].GetType();

I then want to declare a variable of that type:

leftType someVar;

Is that possible?

Vaccano
  • 78,325
  • 149
  • 468
  • 850
  • 2
    No, you don't. Think carefully about it, and you'll realize that such a variable would be utterly useless. – SLaks Jan 26 '11 at 00:45
  • What are you trying to accomplish? There's possibly another way to solve your actual problem. – Trinidad Jan 26 '11 at 01:11

6 Answers6

30

You can do something like these and cast them to a known interface.

var someVar = Convert.ChangeType(someOriginalValue, workItem[LeftFieldName].GetType());
var someVar = Activator.CreateInstance(workItem[LeftFieldName].GetType());

If you replace var with dynamic (and you are using .Net 4), you can call the methods you expect on the someVar object. If they don't exist, you'll just get a MissingMethodException.

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
  • Thanks! Just what I needed. – Hugo Delsing Aug 03 '16 at 07:49
  • It seems a bit of a work. Is it the best way to do things !? There isn't another (better) way to create variables of dynamic types ? – Mohe TheDreamy Sep 10 '16 at 20:56
  • 1
    @MoheTheDreamy Despite the fact that this is the **easiest way** for creating variables is: `var someVar = workItem[LeftFieldName]`, nevertheless **it's not safe**, because you can accidentally **alter** (in case of a *reference type*) **value** of the original object. I'd recommend you to stick with the strategy described in the [Austin](https://stackoverflow.com/questions/4800446/declare-a-variable-using-a-type-variable/4800501#4800501)'s answer (`var someVar = Activator.CreateInstance(workItem[LeftFieldName].GetType());`) – AlexMelw Jul 29 '17 at 18:47
  • Activator.CreateInstance does not work if there is no parameterless constructor for the type e.g. "string" – Tod Oct 19 '17 at 11:17
12

This is not possible.

Variable types are a compile-time concept; it would make no sense to declare a variable of a type which is not known until runtime.
You wouldn't be able to do anything with the variable, since you wouldn't know what type it is.

You're probably looking for the dynamic keyword.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • A possible use case: In my MVC project I have a `Form` object and I add the fields I want to edit using something like `new EditField(e => e.AddressId)`. I use the `MemberExpression` to create the right form element, but some types need a default value. EG `long` has default 0 and `DateTime` has a date as default. Instead of creating a lot of If statements to set the proper default value for every type, I simple create an Instance like Austin explained and use `.ToString()` to get the right default value. – Hugo Delsing Aug 03 '16 at 07:48
4

No, that is not possible. The type of a variable has to be known at compile time.

You can declare a variable of type object, it will be capable of storing any data type.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
3

You cannot do that.

You could use the dynamic type for .Net 4, but for earlier .Net versions, the only type that will fit is object, which you will need to manually cast later by again testing .GetType() on what you assigned to the object-typed variable.

Reading: SO link: whats-the-difference-between-dynamicc-4-and-var

Community
  • 1
  • 1
RichardTheKiwi
  • 105,798
  • 26
  • 196
  • 262
  • 4
    Not sure how this applies. The use of 'var' doesn't matter here. – Joe Jan 26 '11 at 00:46
  • You may notice I changed `var someVar;` to `var someVar = ` which assigns it then to implicitly type `someVar`. This gets around typing `someVar` specifically which is what the OP was after. Like not having to figure out a db column type. – RichardTheKiwi Jan 26 '11 at 00:48
  • 2
    i hope you know that *var* ALWAYS will be resolved to a specific type at compile time! – Pauli Østerø Jan 26 '11 at 00:52
3

object x = Activator.CreateInstance(Type) will let you create the object. Whether you can do much with it beyond that point, I'm not sure.

Joe
  • 41,484
  • 20
  • 104
  • 125
2

GetType is evaluated at run-time, and non-dynamic declaration is at compile-time (it does get more specific than that, yes), so no. Even var will require you to assign a value to it that is of unambiguous type.

Marc Bollinger
  • 3,109
  • 2
  • 27
  • 32