locked
I can't call my class's method :( RRS feed

  • Question

  • Hi, I'm new to programming and C# and I've run into a problem I can't solve.

    I'm writing a console application, a simple one to practice. This program was first written in visual C# and was simple, now in the console, I can't call my classes method (it has only one).

    From within main(), i'm trying to call a method of my class. The compiler complained because main is static, and I can only call static method from a static method apparently. So I added the static keyword to my method and that resolve the "static" issue, but the error became something else: "Member Cipher.Convert(string) cannot be accessed with an instance reference, qualify it with a typename instead."

    Here is my Cipher class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace GemCalcConsole
    {
        class Cipher
        {
            // one variable for each letter of the alphabet
            private readonly byte mbA;
            private readonly byte mbB;
            private readonly byte mbC;
            private readonly byte mbD;
            private readonly byte mbE;
            private readonly byte mbF;
            private readonly byte mbG;
            private readonly byte mbH;
            private readonly byte mbI;
            private readonly byte mbJ;
            private readonly byte mbK;
            private readonly byte mbL;
            private readonly byte mbM;
            private readonly byte mbN;
            private readonly byte mbO;
            private readonly byte mbP;
            private readonly byte mbQ;
            private readonly byte mbR;
            private readonly byte mbS;
            private readonly byte mbT;
            private readonly byte mbU;
            private readonly byte mbV;
            private readonly byte mbW;
            private readonly byte mbX;
            private readonly byte mbY;
            private readonly byte mbZ;
    
            public Cipher(byte a, byte b, byte c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k, byte l, byte m, byte n, byte o, byte p, byte q, byte r, byte s, byte t, byte u, byte v, byte w, byte x, byte y, byte z)
            {
                //store cipher values for each letter in a variable
                mbA = a;
                mbB = b;
                mbC = c;
                mbD = d;
                mbE = e;
                mbF = f;
                mbG = g;
                mbH = h;
                mbI = i;
                mbJ = j;
                mbK = k;
                mbL = l;
                mbM = m;
                mbN = n;
                mbO = o;
                mbP = p;
                mbQ = q;
                mbR = r;
                mbS = s;
                mbT = t;
                mbU = u;
                mbV = v;
                mbW = w;
                mbX = x;
                mbY = y;
                mbZ = z;
            }
    
            public static int Convert(string strText)
            {
                // do nothing if the string is empty
                if (strText == "")
                    return 0;
    
                //value is the end result of the cipher conversion
                int value = 0;
    
                char[] chars = strText.ToCharArray();
    
                //calculate the correct value for the current letter then add it to value until the end of the string is reached
                for (int index = 0; index <= chars.Length - 1; index++)
                {
                    switch (chars[index])
                    {
                        case 'a':
                            value += mbA;
                            break;
                        case 'b':
                            value += mbB;
                            break;
                        case 'c':
                            value += mbC;
                            break;
                        case 'd':
                            value += mbD;
                            break;
                        case 'e':
                            value += mbE;
                            break;
                        case 'f':
                            value += mbF;
                            break;
                        case 'g':
                            value += mbG;
                            break;
                        case 'h':
                            value += mbH;
                            break;
                        case 'i':
                            value += mbI;
                            break;
                        case 'j':
                            value += mbJ;
                            break;
                        case 'k':
                            value += mbK;
                            break;
                        case 'l':
                            value += mbL;
                            break;
                        case 'm':
                            value += mbM;
                            break;
                        case 'n':
                            value += mbN;
                            break;
                        case 'o':
                            value += mbO;
                            break;
                        case 'p':
                            value += mbP;
                            break;
                        case 'q':
                            value += mbQ;
                            break;
                        case 'r':
                            value += mbR;
                            break;
                        case 's':
                            value += mbS;
                            break;
                        case 't':
                            value += mbT;
                            break;
                        case 'u':
                            value += mbU;
                            break;
                        case 'v':
                            value += mbV;
                            break;
                        case 'w':
                            value += mbW;
                            break;
                        case 'x':
                            value += mbX;
                            break;
                        case 'y':
                            value += mbY;
                            break;
                        case 'z':
                            value += mbZ;
                            break;
                    }
                }
    
                // return the cipher value
                return value;
            }
        }
    }
    

    Here is my main code file:

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GemCalcConsole { class Program { // create the cipher Cipher MyCipher1 = new Cipher(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26);

    Cipher MyCipher2 = new Cipher(2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27);

    static void Main(string[] args) { //infinite loop until app is manually closed while (true) { string strString = Console.ReadLine(); strString = strString.Trim(); strString = strString.ToLower();

    Console.WriteLine("Word or Phrase: {0}", strString); Console.WriteLine("Cipher1 Value: {0}\t\tCipher2 Value: {1}", MyCipher1.Convert(strString), MyCipher2.Convert(strString);

    } } } }

    The error is on the final line, specifically the calls to MyCipher.Convert() with the error mentioned above: "Member Cipher.Convert(string) cannot be accessed with an instance reference, qualify it with a typename instead."

    Solutions to the problem I've found online include removing the static keyword, but that's not possible for main. Could someone explain what is going wrong please?

    Thursday, April 12, 2018 11:57 AM

Answers

  • May This Help You

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

    namespace GemCalcConsole
    {
        class Program
        {
            // create the cipher
            
            static void Main(string[] args)
            {
                Cipher MyCipher1 = new Cipher(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);

                Cipher MyCipher2 = new Cipher(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27);
                //infinite loop until app is manually closed
                while (true)
                {
                    string strString = Console.ReadLine();

                    strString = strString.Trim();
                    strString = strString.ToLower();


                    Console.WriteLine("Word or Phrase: {0}", strString);
                    Console.WriteLine("Cipher1 Value: {0}\t\tCipher2 Value: {1}", MyCipher1.Convert(strString), MyCipher2.Convert(strString));

                }
            }
        }
    }

    Alternatively

    class Program
        {
            // create the cipher
            static Cipher MyCipher1 = new Cipher(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
    
            static Cipher MyCipher2 = new Cipher(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27);
            static void Main(string[] args)
            {
                           .....
                }
            }
        }
    Also Remove static from Convert; static method Convert can't access non static bytes mbA,etc.

    public static int Convert(string strText){....}

    i.e. make it as

    public int Convert(string strText)
            {......}





    • Edited by Vikram Manjare Thursday, April 12, 2018 12:30 PM
    • Marked as answer by Integrater Thursday, April 12, 2018 12:57 PM
    Thursday, April 12, 2018 12:04 PM