Search code examples
c#classmethodscompiler-errors

Why C# Error CS0103 when calling a method from another namespace when the namespace was rightly specified? [solved]


I created an age conditionnal "Hello World!". You type in your name, then I ask your age and if you're under 8, I ask you to leave the computer because you should just go play outside.

Here's the file:

using System;
using System.Security.Cryptography; //honestly don't know why this is here
using FileManagement; //i'm gonna talk about this later

namespace P2C2._1;

public class ConditionnalHello
{
    public static void SayHello(string receiver)
    {
        Console.WriteLine("Hello " + receiver + " !");
    }

    public static void AgeCondition(string receiver)
    {
        Console.WriteLine("How old are you?");
        int age = Convert.ToInt32(Console.ReadLine());

        if (age <= 8)
        {
            Console.WriteLine("A nice day awaits outside the computer :)");
        }
        if (age > 8)
        {
            SayHello(receiver);
        }
    }

    public static void Main(string[] args)
    {
        // main function, say hello if you are old enough
        Console.Write("Enter the receiver's name : ");
        string receiver = Console.ReadLine();
        AgeCondition(receiver);

        // (temp lines)
        // appending the README from OpenClassRoom to explain the transformation of P2C2.1
        // 1. content creation
        string content = "# Originally an OpenClassRoom file, from their course 'debuting w\ C#" +
            "***" +
            "I transformed it to add an age condition to cast away the children under 8" +
            "Next, I need to add a function for when the age entered isn't a number.";

        // 2. appending the file
        string fileName = "README.md";
        WriteFile(content, fileName); //error CS0103 right here
    }
}

Originally, this was a code from an OpenClassRoom course. That's why I don't know the using System.Security.Cryptography and also why I want to append the original README. I meant to use the function WriteFile just for once.

I notice the code was too busy so I created a new file for the WriteFile method, FileManagement.cs, and here's its script:

using System;

// adding text to the og README file

namespace FileManagement;

public class FileManagement
{
    public static void WriteFile(string content, string fileName)
    {
        try
        {
            string filePath = @"C:\Users\(lots of folders)\P2C2.1\README.md";
            File.AppendAllText(filePath, Environment.NewLine + content);
            Console.WriteLine("Writing done in the file" + fileName);

            string fullPath = Path.GetFullPath(fileName);
            Console.WriteLine($"You can find the file in: {fullPath}");

        }
        catch (UnauthorizedAccessException e) //there might be too much catch here
        {
            Console.WriteLine("Non-authorized access: " + e.Message);
        }
        catch (IOException e)
        {
            Console.WriteLine("E/S error: " + e.Message);
        }
        catch (Exception e)
        {
            Console.WriteLine($"Error while writing in the file: {e.Message}");
        } 
    }
}

Why can't I just initiate WriteFile in my Main function when I put on "static" before the WriteFile method?

I tried to:

  • put static to the class;
  • put brackets to actually form the namespace in the FileManagement.cs;
  • copy/paste the namespace FileManagement and method WriteFile to each occurence to prevent tipo ;
  • read the Microsoft documentation here that said it could happened if the method is defined in try/catch but I think I cannot do otherwise in my case;
  • read a real similar issue here on Stack Overflow, C# error CS0103 when using methods in different classes, but very much complex code so I didn't understand anything.

Solution

  • To call a static method, you write:

    ClassName.MethodName (arguments);
    

    This can be simplified to just:

    MethodName (arguments);
    

    but only if you are in the same class. This is why you don't write ConditionnalHello.SayHello and write just SayHello while in ConditionalHello class.

    Your class name FileManagement and namespace FileManagement are called the same, which is problematic because FileManagement now has two meanings. I would recommend never call the namespace and inner class the same. Try namespace P2C2._1 (same as the other file in your project) with class FileManagement. Then you would write:

    FileManagement.WriteFile (content, fileName);