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 UserControls 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 UserControls look like this:



public partial class a : UserControl
{
public a()
{
InitializeComponent();
}

public void test()
{
//
}
}









share|improve this question















marked as duplicate by Servy c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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




    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















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 UserControls 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 UserControls look like this:



public partial class a : UserControl
{
public a()
{
InitializeComponent();
}

public void test()
{
//
}
}









share|improve this question















marked as duplicate by Servy c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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




    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













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 UserControls 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 UserControls look like this:



public partial class a : UserControl
{
public a()
{
InitializeComponent();
}

public void test()
{
//
}
}









share|improve this question
















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 UserControls 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 UserControls 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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




    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










  • 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






  • 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












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






share|improve this answer




























    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






    share|improve this answer

























      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






      share|improve this answer























        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






        share|improve this answer












        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 20:23









        bug

        645




        645















            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga