Simple class problem

  • Thread starter Thread starter Gerben van Loon
  • Start date Start date
G

Gerben van Loon

Hello,

I'm quite new to Csharp and like to know how I can call a function from an
upper class.
Following example might explain my problem a bit better:

public class MyClass
{
public void DoSomeThing()
{
//does something
}
public class MySecondClass
{
public void NiceFunction()
{
//How can I call the DoSomeThing() function from here?
}
}
}

Hope somebody can help, thanks

Gerben van Loon.
 
Gerben van Loon said:
I'm quite new to Csharp and like to know how I can call a function from an
upper class.
Following example might explain my problem a bit better:

public class MyClass
{
public void DoSomeThing()
{
//does something
}
public class MySecondClass
{
public void NiceFunction()
{
//How can I call the DoSomeThing() function from here?
}
}
}

Hope somebody can help, thanks

You need to have an instance of MyClass in order to call DoSomeThing.
If you were assuming that the above is like an inner class in Java,
it's more like a static nested class in Java - there's no implicit
instance of MyClass available to an instance of MySecondClass.
 
Hi Gerben,
You can call methods of class only if the method is static. See the example
below and pay attention on the *static* modifier.

public class MyClass
{
public static void DoSomeThing()
{
//does something
}
public class MySecondClass
{
public void NiceFunction()
{
MyClass.DoSomeThing();
}
}
}

A long as MySecondClass is *inner* for MyClass you can access any member
regardless of the access modifier.

If you want to call an isntance method, though, you are gonna need a
reference to the instance of the outer class.
I changed the access modifier of the DoSomeThing method, just to show you
that you can use outer class members regardless of the access modifier.
Event thow the inner class declaration is nested into the outer class they
are still to separate classes

public class MyClass
{
private void DoSomeThing()
{
//does something
}

public class MySecondClass
{
MyClass mOwner;

public MySecondClass(MyClass owner)
{
mOwner = owner;
}
public void NiceFunction()
{
mOwner..DoSomeThing();
}
}
}

HTH
B\rgds
100
 
Gerben van Loon said:
Thanks for your fast reactions guys. I cannot make the method static,
because the method I want to call is a refresh method from a form. Like
displayed below:

public class Form1 : System.Windows.Forms.Form
{
public class SomeList : CollectionBase
{
public void Add()
{
//Add something to the list

//When something is added I like to refresh the form
automaticly
}
}
}

So how can I let the form refresh automaticly when something is added to the
class?

Make sure the list has a reference to the form in question.
 
Hi Gerben,

public class Form1 : System.Windows.Forms.Form
{
public class SomeList : CollectionBase
{
public void Add()
{
//Add something to the list

//When something is added I like to refresh the form
automaticly
}
}
}
So how can I let the form refresh automaticly when something is added to the
class?
Of course there are bunch of decision. For me the better one is as follows:
public class Form1 : System.Windows.Forms.Form
{
//The presumption is that the list class is internal and it is not
going to be used with any other class.
//Because it is binded to the form
protected class SomeList : CollectionBase
{
Form1 mOwner;
//Public constructor
public SomeList (Form1 mOwner)
{
//you should check for owner != null and throw exception if
it is not
mOwner = owner;
}

protected override int OnInsertComplete(int index, object value)
{
mOwner.Refresh(); //Or any other method of the Form1
inttance
}
}

Somewhere in the code the list is created as:
SomeList list = new SomeList(this);
}

HTH
B\rgds
100
 
Back
Top