Calling function from another .cs file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Miller

    #1

    Calling function from another .cs file

    I have a form1.cs file where I am trying to call a sorting
    function from a SortingAlgorith ms.cs file. They are part
    of the same solution, but even when I make the
    SelectionSort algorithm public static, it does not appear
    in the intellisense, not does it let me call it. I tried
    even instantiating an object of type Sorts, the name of my
    class, but that did not help. Also, I added the namespace
    for the Sorts class to the top of my form1.cs class.

    How do I call the SelectionSort from class Sorts.

    Following is some code snippets:

    In form1.cs class:

    using DSM.SortingAlgo rithms; //at top of form1.cs

    In private void Form1_Load(para ms here)
    {
    //define A[10] here

    Sorts s = new Sorts();

    s.SelectionSort (A) // does not work
    .SelectionSort( A) // does not work
    }

    In SortingAlgorith m.cs module


    namespace DSM.SortingAlgo rithms
    {
    Sorts
    {
    public static void SelectionSort(i nt [] A)
    {
    //do stuff here
    }

    //other algorithms
    };
    }


  • Tim Matteson

    #2
    Re: Calling function from another .cs file

    Try DSM.SortingAlgo rithms.Sorts.Se lectionSort(A) or simply
    Sorts.Selection Sort(A) if using the DSM.SortingAlgo rithms namespace. You
    don't access static methods by instantiating the object.

    "David Miller" <david_miller@c infin.com> wrote in message
    news:06f501c389 bb$c1908730$a00 1280a@phx.gbl.. .[color=blue]
    > I have a form1.cs file where I am trying to call a sorting
    > function from a SortingAlgorith ms.cs file. They are part
    > of the same solution, but even when I make the
    > SelectionSort algorithm public static, it does not appear
    > in the intellisense, not does it let me call it. I tried
    > even instantiating an object of type Sorts, the name of my
    > class, but that did not help. Also, I added the namespace
    > for the Sorts class to the top of my form1.cs class.
    >
    > How do I call the SelectionSort from class Sorts.
    >
    > Following is some code snippets:
    >
    > In form1.cs class:
    >
    > using DSM.SortingAlgo rithms; //at top of form1.cs
    >
    > In private void Form1_Load(para ms here)
    > {
    > //define A[10] here
    >
    > Sorts s = new Sorts();
    >
    > s.SelectionSort (A) // does not work
    > .SelectionSort( A) // does not work
    > }
    >
    > In SortingAlgorith m.cs module
    >
    >
    > namespace DSM.SortingAlgo rithms
    > {
    > Sorts
    > {
    > public static void SelectionSort(i nt [] A)
    > {
    > //do stuff here
    > }
    >
    > //other algorithms
    > };
    > }
    >
    >[/color]


    Comment

    • David Miller

      #3
      Re: Calling function from another .cs file

      that worked.

      Is there any way to shorten that?

      David
      [color=blue]
      >-----Original Message-----
      >Try DSM.SortingAlgo rithms.Sorts.Se lectionSort(A) or simply
      >Sorts.Selectio nSort(A) if using the DSM.SortingAlgo rithms[/color]
      namespace. You[color=blue]
      >don't access static methods by instantiating the object.
      >
      >"David Miller" <david_miller@c infin.com> wrote in message
      >news:06f501c38 9bb$c1908730$a0 01280a@phx.gbl. ..[color=green]
      >> I have a form1.cs file where I am trying to call a[/color][/color]
      sorting[color=blue][color=green]
      >> function from a SortingAlgorith ms.cs file. They are[/color][/color]
      part[color=blue][color=green]
      >> of the same solution, but even when I make the
      >> SelectionSort algorithm public static, it does not[/color][/color]
      appear[color=blue][color=green]
      >> in the intellisense, not does it let me call it. I[/color][/color]
      tried[color=blue][color=green]
      >> even instantiating an object of type Sorts, the name of[/color][/color]
      my[color=blue][color=green]
      >> class, but that did not help. Also, I added the[/color][/color]
      namespace[color=blue][color=green]
      >> for the Sorts class to the top of my form1.cs class.
      >>
      >> How do I call the SelectionSort from class Sorts.
      >>
      >> Following is some code snippets:
      >>
      >> In form1.cs class:
      >>
      >> using DSM.SortingAlgo rithms; //at top of form1.cs
      >>
      >> In private void Form1_Load(para ms here)
      >> {
      >> //define A[10] here
      >>
      >> Sorts s = new Sorts();
      >>
      >> s.SelectionSort (A) // does not work
      >> .SelectionSort( A) // does not work
      >> }
      >>
      >> In SortingAlgorith m.cs module
      >>
      >>
      >> namespace DSM.SortingAlgo rithms
      >> {
      >> Sorts
      >> {
      >> public static void SelectionSort(i nt [] A)
      >> {
      >> //do stuff here
      >> }
      >>
      >> //other algorithms
      >> };
      >> }
      >>
      >>[/color]
      >
      >
      >.
      >[/color]

      Comment

      • Tim Matteson

        #4
        Re: Calling function from another .cs file

        Not sure what you mean, with the static method and using the
        DSM.SortingAlgo rithms namespace, your at one line of code:

        Sorts.Selection Sort(A);

        Tim


        "David Miller" <david_miller@c infin.com> wrote in message
        news:002f01c389 c1$6db07480$a30 1280a@phx.gbl.. .[color=blue]
        > that worked.
        >
        > Is there any way to shorten that?
        >
        > David
        >[color=green]
        > >-----Original Message-----
        > >Try DSM.SortingAlgo rithms.Sorts.Se lectionSort(A) or simply
        > >Sorts.Selectio nSort(A) if using the DSM.SortingAlgo rithms[/color]
        > namespace. You[color=green]
        > >don't access static methods by instantiating the object.
        > >
        > >"David Miller" <david_miller@c infin.com> wrote in message
        > >news:06f501c38 9bb$c1908730$a0 01280a@phx.gbl. ..[color=darkred]
        > >> I have a form1.cs file where I am trying to call a[/color][/color]
        > sorting[color=green][color=darkred]
        > >> function from a SortingAlgorith ms.cs file. They are[/color][/color]
        > part[color=green][color=darkred]
        > >> of the same solution, but even when I make the
        > >> SelectionSort algorithm public static, it does not[/color][/color]
        > appear[color=green][color=darkred]
        > >> in the intellisense, not does it let me call it. I[/color][/color]
        > tried[color=green][color=darkred]
        > >> even instantiating an object of type Sorts, the name of[/color][/color]
        > my[color=green][color=darkred]
        > >> class, but that did not help. Also, I added the[/color][/color]
        > namespace[color=green][color=darkred]
        > >> for the Sorts class to the top of my form1.cs class.
        > >>
        > >> How do I call the SelectionSort from class Sorts.
        > >>
        > >> Following is some code snippets:
        > >>
        > >> In form1.cs class:
        > >>
        > >> using DSM.SortingAlgo rithms; //at top of form1.cs
        > >>
        > >> In private void Form1_Load(para ms here)
        > >> {
        > >> //define A[10] here
        > >>
        > >> Sorts s = new Sorts();
        > >>
        > >> s.SelectionSort (A) // does not work
        > >> .SelectionSort( A) // does not work
        > >> }
        > >>
        > >> In SortingAlgorith m.cs module
        > >>
        > >>
        > >> namespace DSM.SortingAlgo rithms
        > >> {
        > >> Sorts
        > >> {
        > >> public static void SelectionSort(i nt [] A)
        > >> {
        > >> //do stuff here
        > >> }
        > >>
        > >> //other algorithms
        > >> };
        > >> }
        > >>
        > >>[/color]
        > >
        > >
        > >.
        > >[/color][/color]


        Comment

        • David Miller

          #5
          Re: Calling function from another .cs file

          Sorry, I didn't see the second option you listed,
          Sorts.Selection Sort(A). That is actually what I was
          assuming it could do, I just didn't realize that after
          specifying the namespace I would also have to specify the
          class. Now that I have 2 classes in that namespace it is
          immediately obvious why I must specify the class.

          thanks for your help

          David
          [color=blue]
          >-----Original Message-----
          >Not sure what you mean, with the static method and using[/color]
          the[color=blue]
          >DSM.SortingAlg orithms namespace, your at one line of code:
          >
          >Sorts.Selectio nSort(A);
          >
          >Tim
          >
          >
          >"David Miller" <david_miller@c infin.com> wrote in message
          >news:002f01c38 9c1$6db07480$a3 01280a@phx.gbl. ..[color=green]
          >> that worked.
          >>
          >> Is there any way to shorten that?
          >>
          >> David
          >>[color=darkred]
          >> >-----Original Message-----
          >> >Try DSM.SortingAlgo rithms.Sorts.Se lectionSort(A) or[/color][/color][/color]
          simply[color=blue][color=green][color=darkred]
          >> >Sorts.Selectio nSort(A) if using the[/color][/color][/color]
          DSM.SortingAlgo rithms[color=blue][color=green]
          >> namespace. You[color=darkred]
          >> >don't access static methods by instantiating the[/color][/color][/color]
          object.[color=blue][color=green][color=darkred]
          >> >
          >> >"David Miller" <david_miller@c infin.com> wrote in[/color][/color][/color]
          message[color=blue][color=green][color=darkred]
          >> >news:06f501c38 9bb$c1908730$a0 01280a@phx.gbl. ..
          >> >> I have a form1.cs file where I am trying to call a[/color]
          >> sorting[color=darkred]
          >> >> function from a SortingAlgorith ms.cs file. They are[/color]
          >> part[color=darkred]
          >> >> of the same solution, but even when I make the
          >> >> SelectionSort algorithm public static, it does not[/color]
          >> appear[color=darkred]
          >> >> in the intellisense, not does it let me call it. I[/color]
          >> tried[color=darkred]
          >> >> even instantiating an object of type Sorts, the name[/color][/color][/color]
          of[color=blue][color=green]
          >> my[color=darkred]
          >> >> class, but that did not help. Also, I added the[/color]
          >> namespace[color=darkred]
          >> >> for the Sorts class to the top of my form1.cs class.
          >> >>
          >> >> How do I call the SelectionSort from class Sorts.
          >> >>
          >> >> Following is some code snippets:
          >> >>
          >> >> In form1.cs class:
          >> >>
          >> >> using DSM.SortingAlgo rithms; //at top of form1.cs
          >> >>
          >> >> In private void Form1_Load(para ms here)
          >> >> {
          >> >> //define A[10] here
          >> >>
          >> >> Sorts s = new Sorts();
          >> >>
          >> >> s.SelectionSort (A) // does not work
          >> >> .SelectionSort( A) // does not work
          >> >> }
          >> >>
          >> >> In SortingAlgorith m.cs module
          >> >>
          >> >>
          >> >> namespace DSM.SortingAlgo rithms
          >> >> {
          >> >> Sorts
          >> >> {
          >> >> public static void SelectionSort(i nt [] A)
          >> >> {
          >> >> //do stuff here
          >> >> }
          >> >>
          >> >> //other algorithms
          >> >> };
          >> >> }
          >> >>
          >> >>
          >> >
          >> >
          >> >.
          >> >[/color][/color]
          >
          >
          >.
          >[/color]

          Comment

          Working...