How do I call this method in my UserControl? [duplicate]
up vote
1
down vote
favorite
This question already has an answer here:
C# code to handle different classes with same method names
9 answers
So I have this class:
public class myClass
{
UserControl a;
UserControl b;
UserControl c;
public myClass (UserControl a, UserControl b, UserControl c)
{
this.a = a;
this.b = b;
this.c = c;
}
}
All 3 UserControl
s contain a method called test()
(or whatever you want it to be) They're not the same methods though. I want to be able to call these methods from outside their respective classes, from myClass
. How do I do this? When I simply try a.test()
I get an error saying test
is not a method of UserControl
.
The UserControl
s look like this:
public partial class a : UserControl
{
public a()
{
InitializeComponent();
}
public void test()
{
//
}
}
c# forms
marked as duplicate by Servy
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 20:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 8 more comments
up vote
1
down vote
favorite
This question already has an answer here:
C# code to handle different classes with same method names
9 answers
So I have this class:
public class myClass
{
UserControl a;
UserControl b;
UserControl c;
public myClass (UserControl a, UserControl b, UserControl c)
{
this.a = a;
this.b = b;
this.c = c;
}
}
All 3 UserControl
s contain a method called test()
(or whatever you want it to be) They're not the same methods though. I want to be able to call these methods from outside their respective classes, from myClass
. How do I do this? When I simply try a.test()
I get an error saying test
is not a method of UserControl
.
The UserControl
s look like this:
public partial class a : UserControl
{
public a()
{
InitializeComponent();
}
public void test()
{
//
}
}
c# forms
marked as duplicate by Servy
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 20:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Can you show the class UserControls and where you call test() ?
– Ole EH Dufour
Nov 19 at 20:18
I'm talking about the UserControls in C# windows forms. Updating now
– user3641520
Nov 19 at 20:20
If you have three instances of classUserControl
, those three instances will all have exactly the same methods and properties. The values of the properties with be different, but, if theUserControl
class has a methods namedTest
, then all three instance will have the sameTest
method. If you have three different classes (Uc1, Uc2 and Uc3), and the three classes all inherit from the same base class (or implement the same interface), then they will all have the properties and methods defined in that base class (or interface)
– Flydog57
Nov 19 at 20:27
1
This isn't a duplicate @Servy. You locked me out from being able to post my answer. :(
– Lews Therin
Nov 19 at 20:31
1
@Servy Because the OP didn't understand the problem and asked the wrong question. If you re-read his question, the error OP received has nothing to do with multiple methods with the same name. They can't call the method because they are trying to call it from the base class (but it doesn't exist because the derived classes are where the method is declared). So it's unrelated to the question that it is a "duplicate" of.
– Lews Therin
Nov 19 at 21:03
|
show 8 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This question already has an answer here:
C# code to handle different classes with same method names
9 answers
So I have this class:
public class myClass
{
UserControl a;
UserControl b;
UserControl c;
public myClass (UserControl a, UserControl b, UserControl c)
{
this.a = a;
this.b = b;
this.c = c;
}
}
All 3 UserControl
s contain a method called test()
(or whatever you want it to be) They're not the same methods though. I want to be able to call these methods from outside their respective classes, from myClass
. How do I do this? When I simply try a.test()
I get an error saying test
is not a method of UserControl
.
The UserControl
s look like this:
public partial class a : UserControl
{
public a()
{
InitializeComponent();
}
public void test()
{
//
}
}
c# forms
This question already has an answer here:
C# code to handle different classes with same method names
9 answers
So I have this class:
public class myClass
{
UserControl a;
UserControl b;
UserControl c;
public myClass (UserControl a, UserControl b, UserControl c)
{
this.a = a;
this.b = b;
this.c = c;
}
}
All 3 UserControl
s contain a method called test()
(or whatever you want it to be) They're not the same methods though. I want to be able to call these methods from outside their respective classes, from myClass
. How do I do this? When I simply try a.test()
I get an error saying test
is not a method of UserControl
.
The UserControl
s look like this:
public partial class a : UserControl
{
public a()
{
InitializeComponent();
}
public void test()
{
//
}
}
This question already has an answer here:
C# code to handle different classes with same method names
9 answers
c# forms
c# forms
edited Nov 19 at 20:32
Lews Therin
2,33511436
2,33511436
asked Nov 19 at 20:10
user3641520
184
184
marked as duplicate by Servy
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 20:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Servy
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 20:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Can you show the class UserControls and where you call test() ?
– Ole EH Dufour
Nov 19 at 20:18
I'm talking about the UserControls in C# windows forms. Updating now
– user3641520
Nov 19 at 20:20
If you have three instances of classUserControl
, those three instances will all have exactly the same methods and properties. The values of the properties with be different, but, if theUserControl
class has a methods namedTest
, then all three instance will have the sameTest
method. If you have three different classes (Uc1, Uc2 and Uc3), and the three classes all inherit from the same base class (or implement the same interface), then they will all have the properties and methods defined in that base class (or interface)
– Flydog57
Nov 19 at 20:27
1
This isn't a duplicate @Servy. You locked me out from being able to post my answer. :(
– Lews Therin
Nov 19 at 20:31
1
@Servy Because the OP didn't understand the problem and asked the wrong question. If you re-read his question, the error OP received has nothing to do with multiple methods with the same name. They can't call the method because they are trying to call it from the base class (but it doesn't exist because the derived classes are where the method is declared). So it's unrelated to the question that it is a "duplicate" of.
– Lews Therin
Nov 19 at 21:03
|
show 8 more comments
Can you show the class UserControls and where you call test() ?
– Ole EH Dufour
Nov 19 at 20:18
I'm talking about the UserControls in C# windows forms. Updating now
– user3641520
Nov 19 at 20:20
If you have three instances of classUserControl
, those three instances will all have exactly the same methods and properties. The values of the properties with be different, but, if theUserControl
class has a methods namedTest
, then all three instance will have the sameTest
method. If you have three different classes (Uc1, Uc2 and Uc3), and the three classes all inherit from the same base class (or implement the same interface), then they will all have the properties and methods defined in that base class (or interface)
– Flydog57
Nov 19 at 20:27
1
This isn't a duplicate @Servy. You locked me out from being able to post my answer. :(
– Lews Therin
Nov 19 at 20:31
1
@Servy Because the OP didn't understand the problem and asked the wrong question. If you re-read his question, the error OP received has nothing to do with multiple methods with the same name. They can't call the method because they are trying to call it from the base class (but it doesn't exist because the derived classes are where the method is declared). So it's unrelated to the question that it is a "duplicate" of.
– Lews Therin
Nov 19 at 21:03
Can you show the class UserControls and where you call test() ?
– Ole EH Dufour
Nov 19 at 20:18
Can you show the class UserControls and where you call test() ?
– Ole EH Dufour
Nov 19 at 20:18
I'm talking about the UserControls in C# windows forms. Updating now
– user3641520
Nov 19 at 20:20
I'm talking about the UserControls in C# windows forms. Updating now
– user3641520
Nov 19 at 20:20
If you have three instances of class
UserControl
, those three instances will all have exactly the same methods and properties. The values of the properties with be different, but, if the UserControl
class has a methods named Test
, then all three instance will have the same Test
method. If you have three different classes (Uc1, Uc2 and Uc3), and the three classes all inherit from the same base class (or implement the same interface), then they will all have the properties and methods defined in that base class (or interface)– Flydog57
Nov 19 at 20:27
If you have three instances of class
UserControl
, those three instances will all have exactly the same methods and properties. The values of the properties with be different, but, if the UserControl
class has a methods named Test
, then all three instance will have the same Test
method. If you have three different classes (Uc1, Uc2 and Uc3), and the three classes all inherit from the same base class (or implement the same interface), then they will all have the properties and methods defined in that base class (or interface)– Flydog57
Nov 19 at 20:27
1
1
This isn't a duplicate @Servy. You locked me out from being able to post my answer. :(
– Lews Therin
Nov 19 at 20:31
This isn't a duplicate @Servy. You locked me out from being able to post my answer. :(
– Lews Therin
Nov 19 at 20:31
1
1
@Servy Because the OP didn't understand the problem and asked the wrong question. If you re-read his question, the error OP received has nothing to do with multiple methods with the same name. They can't call the method because they are trying to call it from the base class (but it doesn't exist because the derived classes are where the method is declared). So it's unrelated to the question that it is a "duplicate" of.
– Lews Therin
Nov 19 at 21:03
@Servy Because the OP didn't understand the problem and asked the wrong question. If you re-read his question, the error OP received has nothing to do with multiple methods with the same name. They can't call the method because they are trying to call it from the base class (but it doesn't exist because the derived classes are where the method is declared). So it's unrelated to the question that it is a "duplicate" of.
– Lews Therin
Nov 19 at 21:03
|
show 8 more comments
1 Answer
1
active
oldest
votes
up vote
-1
down vote
You can use reflection to do this
using System.Reflection;
//Get the type of an object where the test method is
Type myType = a.GetType();
//Find the test method
MethodInfo testMethod = myType.GetMethod("test");
//Invoke the test method in the object
testMethod.Invoke(a, new object {});
https://docs.microsoft.com/en-us/dotnet/api/system.reflection.methodbase.invoke?view=netframework-4.7.2
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-1
down vote
You can use reflection to do this
using System.Reflection;
//Get the type of an object where the test method is
Type myType = a.GetType();
//Find the test method
MethodInfo testMethod = myType.GetMethod("test");
//Invoke the test method in the object
testMethod.Invoke(a, new object {});
https://docs.microsoft.com/en-us/dotnet/api/system.reflection.methodbase.invoke?view=netframework-4.7.2
add a comment |
up vote
-1
down vote
You can use reflection to do this
using System.Reflection;
//Get the type of an object where the test method is
Type myType = a.GetType();
//Find the test method
MethodInfo testMethod = myType.GetMethod("test");
//Invoke the test method in the object
testMethod.Invoke(a, new object {});
https://docs.microsoft.com/en-us/dotnet/api/system.reflection.methodbase.invoke?view=netframework-4.7.2
add a comment |
up vote
-1
down vote
up vote
-1
down vote
You can use reflection to do this
using System.Reflection;
//Get the type of an object where the test method is
Type myType = a.GetType();
//Find the test method
MethodInfo testMethod = myType.GetMethod("test");
//Invoke the test method in the object
testMethod.Invoke(a, new object {});
https://docs.microsoft.com/en-us/dotnet/api/system.reflection.methodbase.invoke?view=netframework-4.7.2
You can use reflection to do this
using System.Reflection;
//Get the type of an object where the test method is
Type myType = a.GetType();
//Find the test method
MethodInfo testMethod = myType.GetMethod("test");
//Invoke the test method in the object
testMethod.Invoke(a, new object {});
https://docs.microsoft.com/en-us/dotnet/api/system.reflection.methodbase.invoke?view=netframework-4.7.2
answered Nov 19 at 20:23
bug
645
645
add a comment |
add a comment |
Can you show the class UserControls and where you call test() ?
– Ole EH Dufour
Nov 19 at 20:18
I'm talking about the UserControls in C# windows forms. Updating now
– user3641520
Nov 19 at 20:20
If you have three instances of class
UserControl
, those three instances will all have exactly the same methods and properties. The values of the properties with be different, but, if theUserControl
class has a methods namedTest
, then all three instance will have the sameTest
method. If you have three different classes (Uc1, Uc2 and Uc3), and the three classes all inherit from the same base class (or implement the same interface), then they will all have the properties and methods defined in that base class (or interface)– Flydog57
Nov 19 at 20:27
1
This isn't a duplicate @Servy. You locked me out from being able to post my answer. :(
– Lews Therin
Nov 19 at 20:31
1
@Servy Because the OP didn't understand the problem and asked the wrong question. If you re-read his question, the error OP received has nothing to do with multiple methods with the same name. They can't call the method because they are trying to call it from the base class (but it doesn't exist because the derived classes are where the method is declared). So it's unrelated to the question that it is a "duplicate" of.
– Lews Therin
Nov 19 at 21:03