-1

I have a single module for a console application with an extension method in a separate Extensions namespace and class, referred to in the DataProcessor class with using Extensions;, effectively as follows with extraneous code removed:

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

namespace DataProcessor
{
    using Extensions;
    class Program
    {
        public static int Main(string[] Args)
        {
              for (int CtrA = 0; CtrA <= 100; CtrA++)
              {
                    System.Diagnostics.Debug.Print((CtrA + 1).Ordinal);     // Error occurs here
              }
        }
    }
}

namespace Extensions
{
    public static class Extensions
    {
        public static string Ordinal(int Number)
        {
            string Str = Number.ToString();
            Number = Number % 100;
            if ((Number >= 11) && (Number <= 13))
            {
                Str += "th";
            }
            else
            {
                switch (Number % 10)
                {
                    case 1:
                        Str += "st";
                        break;
                    case 2:
                        Str += "nd";
                        break;
                    case 3:
                        Str += "rd";
                        break;
                    default:
                        Str += "th";
                        break;
                }
            }
            return Str;
        }
    }

I am getting a compile-time error on the line System.Diagnostics.Debug.Print((CtrA + 1).Ordinal);, as well as everywhere else I use .Ordinal as an int method, stating:

'int' does not contain a definition for 'Ordinal' and no extension method 'Ordinal' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)

Can anyone tell me what I'm doing wrong?

Monty Wild
  • 3,981
  • 1
  • 21
  • 36
  • you forgot to put `this` keyword in `Ordinal` signiture. – Old Fox Jul 31 '15 at 05:38
  • 2
    Probably should be duplicate of [Does C# support extension properties](http://stackoverflow.com/questions/619033/does-c-sharp-have-extension-properties)... Not yet closing as it is unclear whether OP does not know syntax of extensions (should be another dup then) or actually need extension property. – Alexei Levenkov Jul 31 '15 at 05:52

4 Answers4

5

You method is not an extension method. It misses this before first argument:

public static string Ordinal(this int Number)

Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier.

from Extension Methods (C# Programming Guide)

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
3

You need to change the function like this:

public static string Ordinal(this int Number)

Notice the this keyword. This is required when the function you have is an extension.

Prashant
  • 966
  • 9
  • 26
2

You have to add this to prior any parameters.

public static string Ordinal(this int Number)

Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.

CharithJ
  • 46,289
  • 20
  • 116
  • 131
0

You forgot this.

namespace Extensions
{
    public static class Extensions
    {
        public static string Ordinal(this int Number)
        {
            string Str = Number.ToString();
            Number = Number % 100;
            if ((Number >= 11) && (Number <= 13))
            {
                Str += "th";
            }
            else
            {
                switch (Number % 10)
                {
                    case 1:
                        Str += "st";
                        break;
                    case 2:
                        Str += "nd";
                        break;
                    case 3:
                        Str += "rd";
                        break;
                    default:
                        Str += "th";
                        break;
                }
            }
            return Str;
        }
    }
jtabuloc
  • 2,479
  • 2
  • 17
  • 33