Compiler Error CS0117

  • Thread starter Thread starter Thomas Tryde
  • Start date Start date
T

Thomas Tryde

Simplified code.

class A : object {
m() {}
}

class B : objet {
m() {}
}

class Test {
Test() {
object var = RANDOM ? new A() : new B();
var.m(); // Compiler Error CS0117
}
}

How can I get this functionality to work without a compiler
error (it works excellent in VB.Net).


Another example:

object db = RANDOM ? new SqlConnection() : new
OleDbConnection();
....
....
db.Open(); // Compiler Error CS0117
 
Thomas Tryde said:
Simplified code.

class A : object {
m() {}
}

class B : objet {
m() {}
}

class Test {
Test() {
object var = RANDOM ? new A() : new B();
var.m(); // Compiler Error CS0117
}
}

How can I get this functionality to work without a compiler
error

Create a new interface which defines a method, and implement that
interface in both the classes you're interested in. Then do:

IWhatever var = RANDOM ? new A() : new B();
var.SomeMethod();
(it works excellent in VB.Net).

No, it probably works slowly using reflection, and it means that if you
have a typo in the method name, you won't know about it until you end
up running that code. Type safety is a good thing - use it.
 
It worked!
Thanks!
-----Original Message-----


Create a new interface which defines a method, and implement that
interface in both the classes you're interested in. Then do:

IWhatever var = RANDOM ? new A() : new B();
var.SomeMethod();


No, it probably works slowly using reflection, and it means that if you
have a typo in the method name, you won't know about it until you end
up running that code. Type safety is a good thing - use it.

--
Jon Skeet - <[email protected]>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
.
 
Back
Top