views:

86

answers:

3

What am I doing wrong?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Box2D.XNA;
//...
Type.Parse("GameObjectModel");

Compilation error:

'System.Type' does not contain a definition for 'Parse'

I am trying to get the type of a class name from a string, so I can instantiate an instance of that class.

+4  A: 

It looks like you're trying to get a type from a string, in that case I think you're after Type.GetType("GameObjectModel") see here for usage.

Nick Craver
well it depends on what he is trying to do :-) - he didnt give much of a clue, but its a good guess
pm100
@pm100 - For user requirement meetings coming up, I figure I better practice guessing :)
Nick Craver
A: 

If you're trying to turn a string into a System.Type, perhaps the Assembly.GetType function is what you're looking for?

If you're trying to get an instance, you don't need a Type at all. Activator.CreateInstance has overloads that just take the string name of the type directly.

Ben Voigt
Not my downvote, but I'd work on the civility a bit.
Nick Craver
I have to agree with Ben a bit, here. It sounds like the OP needs to learn more about how to use .NET than the answer to his question. And one major thing he needs to learn is that he can't just try and call methods that do not exist.
Jaxidian
Another thing being done wrong is... well, Raymond Chen says it quite eloquently http://blogs.msdn.com/oldnewthing/archive/2010/03/24/9983984.aspx
Ben Voigt
@Jaxidian - When googling, this is one of the first things that comes up: [Type.parse](http://msdn.microsoft.com/en-us/library/bb397503.aspx) I wouldn't assume they didn't look and tried just anything. If you're not familiar with searching through or are new to MSDN, *which* .Net API you're looking at isn't always straight forward at first.
Nick Craver
Nick Craver is correct, actually. I googled it, and was trying to get that to work.
Rosarch
Moral of my story was that Rosarch should learn a bit more about 1) Intellisense and 2) Namespace References. If he knew some basics about these, the question wouldn't be centered around an error message that says, in English, "This function does not exist." I'm NOT saying the root of the question of "How do I parse a type" is a bad one, I'm just saying that there are some major fundamentals that @Rosarch should learn that will go a LONG way in writing .NET code. That's what was behind what Ben said, he just said it poorly. That's why I agreed "a bit" with him. ;-)
Jaxidian
+1  A: 

You should write typeof(GameObjectModel).

If "GameObjectModel" is a string known only at runtime, you should call Type.GetType.

To instantiate a class of a type known only at runtime, write the following:

object myInstance = Activator.CreateInstance(Type.GetType(typeName));

However, it will be slow and difficult to work with. (you'd need to use reflection or cast to a known base type or interface)
What are you trying to do?

SLaks
It is indeed a string only known at runtime.
Rosarch
I can't believe I wrote that.
SLaks
@SLaks - I say that every time I look at old code :)
Nick Craver