-1

One hour ago, I spend a lot of time and finished the project but I had problems when saving it properly and totally lost it. However, I did it again but this time it don't work

I am not sure what I did wrong this time... I think I did exactly the same, but this time don't work?

I am getting the following error:

Error 1 An object reference is required for the non-static field, method, or property 'ConsoleApplication1.Program.convertir(string)' C:\Documents and Settings\modifiedForPersonalReasons\Program.cs 12 45 ConsoleApplication1

Here is my code, I was going to put just the error lines but then it would probably miss some information that we could need to spot the problem:

    static void Main(string[] args)
    {
        Console.WriteLine("23.21 es " + convertir("23.21"));
        Console.ReadLine();
    }

    public string convertir(string str)
    {
        string[] arr;
        arr = str.Split('.');

        string rtn = españolizar(arr[0], "peso");

        if (arr.Length > 1)
        {
            rtn += " con " + españolizar(arr[1], "centavo");
        }

        return rtn;
    }

    public string españolizar(string str, string str2)
    {
        string[] list1 = { "cero", "un", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", "once", "doce", "trece", "catorce", "quince" };
        string[] list2 = { "nivelarindexes", "dieci", "veinti", "trei", "cuare", "cincue", "sese", "sete", "oche", "nove" };

        int numero = int.Parse(str);
        string strNumero = Convert.ToString(numero);

        int primerDigito = int.Parse(Convert.ToString(strNumero[0]));
        int segundoDigito = 0;

        if (strNumero.Length > 1)
        {
            segundoDigito = int.Parse(Convert.ToString(strNumero[1]));
        }

        int cases = -1;

        if (numero > -1 && numero < 16)
        {
            cases = 0;
        }
        else if (numero > 15 && numero < 30)
        {
            cases = 1;
        }
        else if (numero > 29 && numero < 100)
        {
            cases = 2;
        }
        else if (numero == 100)
        {
            cases = 3;
        }

        string rtn = "";

        switch (cases)
        {
            case 0:
                rtn = list1[numero] + " " + str2;
                if (primerDigito != 1)
                {
                    rtn += "s";
                }
                break;
            case 1:
                if (numero != 20)
                {
                    rtn = list2[primerDigito] + list1[segundoDigito];
                }
                else
                {
                    rtn = "veinte";
                }
                rtn += " " + str2 + "s";
                break;
            case 2:
                rtn = list2[primerDigito] + "nta";
                if (segundoDigito != 0)
                {
                    rtn += " y " + list1[segundoDigito];
                }
                rtn += " " + str2 + "s";
                break;
            case 3:
                rtn = "cien " + str2 + "s";
                break;
            case -1:
                rtn = "número invalido";
                break;
        }

        rtn = rtn.Replace("iun", "iún").Replace("idos", "idós").Replace("itres", "itrés").Replace("iseis", "iséis");

        return rtn;
    }

I could swear I didn't change anything :C

mithril333221
  • 799
  • 2
  • 9
  • 20

4 Answers4

4

Change public string convertir(string str) to public static string convertir(string str). Do the same for the españolizar function.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
1

You are getting this message because you are calling non-static methods from a static method. You need an instance of this class to call the non-static methods, or make the other methods static.

Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
1

You must change your two methods to static methods as you can't call a static method from a non-static method. See here for an explanation as to why.

To fix this, you must change the following:

public string convertir(string str) 

To

public static string convertir(string str)

And change

public string españolizar(string str, string str2)

To

public static string españolizar(string str, string str2)
Community
  • 1
  • 1
ediblecode
  • 11,701
  • 19
  • 68
  • 116
0

The convertir method needs to be static (same as Main).

Ian Horwill
  • 2,957
  • 2
  • 24
  • 24