-1

Getting The following Error...

Error 1 Cannot implicitly convert type 'object' to 'CSV_OOP_Convert.FileConverter'. An explicit conversion exists (are you missing a cast?) \server\UserShares\DBell\VSC\CSV_OOP_Convert\CSV_OOP_Convert\Form1.cs 44 39 CSV_OOP_Convert

String className = cmbConversionAlgorithm.Text;
string namespaceName = "CSV_OOP_Convert";

FileConverter myObj =Activator.CreateInstance(Type.GetType(namespaceName + "." + className));
ConvertFile(myObj);

the cmbConversionAlgorithm contains the the correct name of the class I wish to create an instance of.

it works fine when I create it normally.

CSV_OOP_Convert.TFConverter tfc = new CSV_OOP_Convert.TFConverter();
Cœur
  • 37,241
  • 25
  • 195
  • 267
user3755946
  • 804
  • 1
  • 10
  • 22
  • 2
    `CreateInstance` returns an `object`, you need to cast: `FileConverter myObj =(FileConverter)Activator.CreateInstance(...)` – Lee Oct 29 '15 at 10:58

3 Answers3

2

Activator.CreateInstance returns an object of type object, so it’s a completely unspecific type. In order to assign that to a variable of a more specific type, you need to make an explicit type cast:

FileConverter myObj = (FileConverter)Activator.CreateInstance(type);
poke
  • 369,085
  • 72
  • 557
  • 602
1

Activator returns an Object

Try something like this:

String className = cmbConversionAlgorithm.Text;
string namespaceName = "CSV_OOP_Convert";

Object myObj =Activator.CreateInstance(Type.GetType(namespaceName + "." + className));

ConvertFile((FileConverter)myObj);
Morcilla de Arroz
  • 2,104
  • 22
  • 29
1

Cast result to FileConverter class

var myObj =Activator.CreateInstance(Type.GetType(namespaceName + "." + className)) as FileConverter;
Szymon D
  • 441
  • 2
  • 13