Compiler Error CS0117

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
 
J

Jon Skeet

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.
 
T

Thomas Tryde

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
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top