In my current project I have been provided with a solution that contains 3 simple classes and 1 component class. There is some code in component class that i need to access in one of m开发者_开发技巧y simple class. I am trying to create an instance of component class but there is error that Component class does not exist. Please guide either i am going in wrong direction? If so then how can i solve my problem. How can i access code given in component class. I am working in Visual Studio 2010 and .NET 4.0 with C#.net
Thanks
Khizar
Giving the lack of specifics, I'm gonna make a wild guess:
The signature of Component is:
class Component
{
//Class members
}
by default, the class is internal, which means it's only available in the assembly it resides. Change it to:
public class Component
{
//Class members
}
And fix the namespaces (CTRL + ";")
You can't just access the running instance - you will have to pass pointer to the component class instance to the simple class, common way is passing the pointer when creating the class.
For example, in the component class where you create the simple class:
SimpleClass mySimpleClass = new SimpleClass(this);
Now in the simple class, add class member and change the constructor:
ComponentClass m_ComClass;
public SimpleClass(ComponentClass parent)
{
m_ComClass = parent;
}
And finally in the place where you need to access method of the Component class from within the simple class, have:
void SomeAction()
{
m_ComClass.SomeFunc();
}
精彩评论