0

frmMainPage is a Form in my project.

This is correct

var myType = typeof(frmMainPage);

This is incorrect

Form frm = (Form)Activator.CreateInstance(Type.GetType("frmMainPage"), _userName);

var myType = typeof(frm);

How can I use typeof with a variable?

EkremAbi
  • 11
  • 4
  • 1
    Use `frm.GetType()` instead. Do note that Type.GetType() already provided you with the type, so you just get back what you put in. Smells like a vb.net conversion problem, but the whiff isn't strong enough to diagnose its evil default instance feature. – Hans Passant Jun 06 '20 at 12:09
  • frm.GetType() is your friend.. – Frenchy Jun 06 '20 at 12:10
  • 2
    Possible duplicate of [How can I get the data type of a variable in C#?](https://stackoverflow.com/questions/11634079/how-can-i-get-the-data-type-of-a-variable-in-c) – GSerg Jun 06 '20 at 12:13

1 Answers1

0

typeof takes a type name you specify at compile time.

GetType gets the runtime type of an instance.

Frenchy
  • 16,386
  • 3
  • 16
  • 39