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?

2
  • 2
    No, you don't. Think carefully about it, and you'll realize that such a variable would be utterly useless. Commented Jan 26, 2011 at 0:45
  • What are you trying to accomplish? There's possibly another way to solve your actual problem. Commented Jan 26, 2011 at 1:11

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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 ?
@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's answer (var someVar = Activator.CreateInstance(workItem[LeftFieldName].GetType());)
Activator.CreateInstance does not work if there is no parameterless constructor for the type e.g. "string"
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.

1 Comment

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<MemberAddress>(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.
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.

Comments

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.

1 Comment

That will create an instance of that type, not a variable of that type.
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

3 Comments

Not sure how this applies. The use of 'var' doesn't matter here.
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.
i hope you know that var ALWAYS will be resolved to a specific type at compile time!
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.