invoking method of outer class from inner class.

  • Thread starter Thread starter trialproduct2004
  • Start date Start date
T

trialproduct2004

hi all
i am having application which is using two classes.
in one class i am creating instance of another class and calling method
of that class.
what i want is from inner class's method i want to invoke method of
outer class.
can i use delegates to do this.
any help will be appreciated.
thanks in advance.
 
i don't think C# allow inner class to invoke method of outer class
can't you store the data you want in inner class and create a method in
outerclass to return the data (which is from inner class) ?

hope this helps
Ivan Wong
 
Hello trialproduct2004!

OuterClass.cs
=============

using System;

namespace Kalsoft.OuterInner.Test
{
public class OuterClass
{
public int OuterPublicMethod()
{
return 1;
}

private int OuterPrivateMethod()
{
return 0;
}

public class InnerClass
{
public void InnerPublicMethod()
{
OuterClass outer = new OuterClass();
Console.WriteLine("Outer Private Method Called:
"+outer.OuterPrivateMethod());
Console.WriteLine("Outer Public Method Called:
"+outer.OuterPublicMethod());
}
}
}
}


MainClass.cs
============

using System;

namespace Kalsoft.Main.Test
{
public class MainClass
{
[STAThread()]
public static void Main(String[] args)
{
Kalsoft.OuterInner.Test.OuterClass.InnerClass inner =
new Kalsoft.OuterInner.Test.OuterClass.InnerClass();
inner.InnerPublicMethod();
}
}
}


Console Output
==============

Outer Private Method Called: 0
Outer Public Method Called: 1

I hope this might be of some help.

Let me know in case of any inconsistancy.

Regards,

Moiz Uddin Shaikh
Software Engineer
Kalsoft (Pvt) Ltd
 
This is done often

InnerClass needs a member of type OuterClass called owner.
the InnerClass constructor should take a paramater of type OuterClass
and assign it to owner.

The InnerClass would call owner.somefunction();


Hope this helps.

ps I dont think this works across appdomains
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top