By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

Compiler Error CS0117

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Thomas Tryde <th**********@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Jul 19 '05 #2

P: n/a
It worked!
Thanks!
-----Original Message-----
Thomas Tryde <th**********@hotmail.com> wrote:
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 thatinterface 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 youhave a typo in the method name, you won't know about it until you endup running that code. Type safety is a good thing - use it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
.

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.