954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C# Working between Classes

I was just a bit confused about something sorry. If you have two classes, Ribbon1 and ThisAddIN below. How can I call Example()?
- All are in the same namespace..

public partial class Ribbon1
    {

        private void button1_Click_1(object sender, RibbonControlEventArgs e)
        {
            //Call Example() from ThisAddIn Class
            Example();
        }
    }


public partial class ThisAddIn
    {
        public void Example()
        {
        //Do x,y,z etc
        }
    }


Obviously I've tried running something like:

ThisAddIn test = new ThisAddIn();
test.Example();


I think i'm missing something basic as I don't really work with multiple classes. Mybe this would work?! I just want to know the most appropriate way to do this really..

public partial class Ribbon1 : ThisAddIn
    {

        private void button1_Click_1(object sender, RibbonControlEventArgs e)
        {
            //Call Example() from ThisAddIn Class
            ThisAddIn.Example();
        }
    }


public partial class ThisAddIn
    {
        public void Example()
        {
        //Do x,y,z etc
        }
    }


Thank you

stevetaylor15
Newbie Poster
22 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Unless the method is static, you need an object to call it on. How you go about accessing the method (static, instance, inheritance, etc...) really depends on your design. How do these two classes relate and interact in your application?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

You were almost there, tou just needed a line of code (using the first way):

public partial class Ribbon1
    {
 
        private void button1_Click_1(object sender, RibbonControlEventArgs e)
        {
            ThisAddIn test = new ThisAddIn();
            test.Example();

    
        }
    }
 
 
public partial class ThisAddIn
    {
        public void Example()
        {
            //Do x,y,z etc
        }
    }


Regards

williamrojas78
Junior Poster
111 posts since Jun 2005
Reputation Points: 23
Solved Threads: 10
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: