Calling a method within the same class

Tim D

I am working on a simple program to generate an array of values and perform some basic computations on them. I am having problems calling my "Mean" and "Median" methods from the Main method. I've attached the compiler error below. Thank you.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication4
{
    class arraymod
    {
        static void Main(string[] args)
        {
            int[] values;
            values = new int[10];
            Random rand = new Random();
            for (int i = 0; i < 10; i++)
            {
                values[i] = rand.Next(1,20);
            }

            Array.Sort(values);
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(values[i]);
            }
            Console.WriteLine("Min: {0}", values.Min());
            Console.WriteLine("Max: {0}", values.Max());
            Console.WriteLine("Sum: {0}", values.Sum());
            Console.WriteLine("Mean: {0}", values.Mean()); //program fails here
            Console.WriteLine("Median: {0}", values.Median());
        }
        public int Mean(int[] arr)
        {
            int valuesMean = arr.Sum()/arr.Count();
            return valuesMean;
        }
        public int Median(int[] arr)
        {
            int valuesMedian = (arr.Max()+ arr.Min())/2;
            return valuesMedian;
        }
    }
}

arraymod.cs(29,42):

error CS1061: 'System.Array' does not contain a definition for 'Mean' and no extension method 'Mean' accepting a first argument of
type 'System.Array' could be found (are you missing a using directive or an assembly reference?)

Shoring

It's not a extension method. You need to make methods static and call them:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication4
{
class arraymod
{
    static void Main( string[] args )
    {
        int[] values;
        values = new int[ 10 ];
        Random rand = new Random();
        for( int i = 0 ; i < 10 ; i++ )
        {
            values[ i ] = rand.Next( 1 , 20 );
        }

        Array.Sort( values );
        for( int i = 0 ; i < 10 ; i++ )
        {
            Console.WriteLine( values[ i ] );
        }
        Console.WriteLine( "Min: {0}" , values.Min() );
        Console.WriteLine( "Max: {0}" , values.Max() );
        Console.WriteLine( "Sum: {0}" , values.Sum() );
        Console.WriteLine( "Mean: {0}" , Mean( values ) ); //call
        Console.WriteLine( "Median: {0}" , Median( values ) );//call
    }
    //make method "Mean" static
    public static int Mean( int[] arr )
    {
        int valuesMean = arr.Sum() / arr.Count();
        return valuesMean;
    }
    //make method "Median" static
    public static int Median( int[] arr )
    {
        int valuesMedian = ( arr.Max() + arr.Min() ) / 2;
        return valuesMedian;
    }
}
}

Or use extension method:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication4
{
static class arraymod
{
    static void Main( string[] args )
    {
        int[] values;
        values = new int[ 10 ];
        Random rand = new Random();
        for( int i = 0 ; i < 10 ; i++ )
        {
            values[ i ] = rand.Next( 1 , 20 );
        }

        Array.Sort( values );
        for( int i = 0 ; i < 10 ; i++ )
        {
            Console.WriteLine( values[ i ] );
        }
        Console.WriteLine( "Min: {0}" , values.Min() );
        Console.WriteLine( "Max: {0}" , values.Max() );
        Console.WriteLine( "Sum: {0}" , values.Sum() );
        Console.WriteLine( "Mean: {0}" , values.Mean() ); //call
        Console.WriteLine( "Median: {0}" , values.Median() );//call
    }
    //use extension method
    public static int Mean( this int[] arr )
    {
        int valuesMean = arr.Sum() / arr.Count();
        return valuesMean;
    }
    //use extension method
    public static int Median( this int[] arr )
    {
        int valuesMedian = ( arr.Max() + arr.Min() ) / 2;
        return valuesMedian;
    }
}
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling method of same name from different class

From Java

OOP: Calling a public method within the same class

From Javascript

Calling a method from another method in the same class

From Java

Calling one method from another within same class in Python

From Java

Calling a method inside another method in same class

From Java

java calling a class from another class within same package

From Java

Calling a method on an Object from within a Class vs from within a method

From Java

Calling super Method within Anonymous Class

From Java

Calling super method from within super class

From Dev

Python -- calling a function in same file as a class from within an instance of that class?

From Dev

Calling a private method within a class in Perl 6

From Dev

Calling method, classmethod, staticmethod in the same Python class

From Dev

PHP Calling a function within a static method of a class

From Dev

Calling function within method python class

From Dev

Calling method of another parent of the same class

From Dev

calling an object method with a setTimeout function from within the same object in Javascript

From Dev

Problems calling a method within the same class in f#

From Dev

Calling a constructor from method within the same class

From Dev

Calling a method within the same method?

From Dev

calling a method from same class in xcode

From Dev

Calling another method within method with the same name in Java

From Dev

Calling a singleton method within the singleton class in Ruby?

From Dev

Trouble understanding the calling of global variables with in a class to a method within the class

From Dev

Calling a method from base class to class with same name as the method

From Dev

Calling main method within the class python

From Dev

JS - Calling method in class from other method in same class

From Dev

Calling an object of a method of a class within another method of the same class

From Dev

call variable in method within the same class

From Dev

@Transactional method calling a method in the same class

Related Related

  1. 1

    Calling method of same name from different class

  2. 2

    OOP: Calling a public method within the same class

  3. 3

    Calling a method from another method in the same class

  4. 4

    Calling one method from another within same class in Python

  5. 5

    Calling a method inside another method in same class

  6. 6

    java calling a class from another class within same package

  7. 7

    Calling a method on an Object from within a Class vs from within a method

  8. 8

    Calling super Method within Anonymous Class

  9. 9

    Calling super method from within super class

  10. 10

    Python -- calling a function in same file as a class from within an instance of that class?

  11. 11

    Calling a private method within a class in Perl 6

  12. 12

    Calling method, classmethod, staticmethod in the same Python class

  13. 13

    PHP Calling a function within a static method of a class

  14. 14

    Calling function within method python class

  15. 15

    Calling method of another parent of the same class

  16. 16

    calling an object method with a setTimeout function from within the same object in Javascript

  17. 17

    Problems calling a method within the same class in f#

  18. 18

    Calling a constructor from method within the same class

  19. 19

    Calling a method within the same method?

  20. 20

    calling a method from same class in xcode

  21. 21

    Calling another method within method with the same name in Java

  22. 22

    Calling a singleton method within the singleton class in Ruby?

  23. 23

    Trouble understanding the calling of global variables with in a class to a method within the class

  24. 24

    Calling a method from base class to class with same name as the method

  25. 25

    Calling main method within the class python

  26. 26

    JS - Calling method in class from other method in same class

  27. 27

    Calling an object of a method of a class within another method of the same class

  28. 28

    call variable in method within the same class

  29. 29

    @Transactional method calling a method in the same class

HotTag

Archive