Simple class problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gerben van Loon

    Simple class problem

    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.


  • Jon Skeet

    #2
    Re: Simple class problem

    Gerben van Loon <nospamplz_gerb envl@home.nl> wrote:[color=blue]
    > 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[/color]

    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.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • 100

      #3
      Re: Simple class problem

      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.DoSomeT hing();
      }
      }
      }

      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(M yClass owner)
      {
      mOwner = owner;
      }
      public void NiceFunction()
      {
      mOwner..DoSomeT hing();
      }
      }
      }

      HTH
      B\rgds
      100
      "Gerben van Loon" <nospamplz_gerb envl@home.nl> wrote in message
      news:bfrhta$i07 $1@news1.tilbu1 .nb.home.nl...[color=blue]
      > 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.
      >
      >[/color]


      Comment

      • Jon Skeet

        #4
        Re: Simple class problem

        Gerben van Loon <nospamplz_gerb envl@home.nl> wrote:[color=blue]
        > 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?[/color]

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

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • 100

          #5
          Re: Simple class problem


          Hi Gerben,

          [color=blue]
          > 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[/color]
          the[color=blue]
          > class?[/color]
          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 OnInsertComplet e(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


          Comment

          Working...