StrataFrame Forum

I'm stuck - Hope to get some help!

http://forum.strataframe.net/Topic9307.aspx

By Ben Hayat - 6/4/2007

I'm stuck with a compile error and can't see it.



I have a simple method that starts a given form:

private void StartForm(System.Type MyFormType)

{

Form loForm;

loForm = Activator.CreateInstance(MyFormType) as Form;

loForm.Show();

}





Then for each item from the menu, I call this method and pass the class name of my form:

private void tsmStore_Click(object sender, EventArgs e)

{

// frmStore is the class name for store

StartForm(GetType(frmStore));

}





I then get this error:

'frmStore' is a 'type' but is used like a 'variable'





I'm passing the type of class name, but still like it.



What am I missing?



Thanks!
By Chan - 6/4/2007

Hi,

Try



private void tsmStore_Click(object sender, EventArgs e)

{

// frmStore is the class name for store

StartForm(Type.GetType("MyNameSpace.frmStore"));

}




HTH
By Ben Hayat - 6/4/2007

Thanks Chan;



But adding Type gives me this error:

'frmStore' is a 'type', which is not valid in the given context
By Ben Hayat - 6/4/2007

Ahh, never mind Chan; I had forgotten to put Double Quotes around. Thank you very much!
By StrataFrame Team - 6/5/2007

Ah, the reason that it's giving you that is you're using GetType in C#.  If you use Type.GetType() (the static method on the System.Type class), it expects a string name for the type.  It's confusing because in VB, Type.GetType("MyFormName") and GetType(MyFormName) are named the same thing, but the first is a static method of System.Type and the second one is a language construct that compiles the raw type into the application.

The equivalent language construct in C# is typeof(), so you should call it like this:

StartForm(typeof(frmStore));

not

StartForm(GetType(frmStore));

The second one only works in VB (without the ; Wink)

P.S. Of course you're going to get some help Smile

By Ben Hayat - 6/5/2007

It's confusing because in VB, Type.GetType("MyFormName") and GetType(MyFormName) are named the same thing, but the first is a static method of System.Type and the second one is a language construct that compiles the raw type into the application.
I'm going to kill this VB. I looked at the CRM example (which only comes in VB by the way), and started from there. But killed three hours of my sleep over it, until Chan jumped in and helped.



But your point is well taken.

Ben, a long time ago, you had mentioned you had gotten a new [good] program that converts VB to C#. Could you please convert the CRM Application to C# and attach the solution here. There are actually several samples in VB that are not in C# samples.



The equivalent language construct in C# is typeof(), so you should call it like this:



StartForm(typeof(frmStore));



not



StartForm(GetType(frmStore));
I just tried you way and works perfect. You're "D" man! Smile



P.S. Of course you're going to get some help
You're not only an scholar but a Gentleman as well! Wink



Thanks for the great support!