-1

So I am new to C# and done some researching on how to do this but I still don't get it.

I have 2 files, file foo and file bar. File foo is my main file and I want to use a function from inside bar inside foo. The function's purpose is not important because I am just playing around for now. As of now it is an alternative method to print text into the console. I am getting this error message when I try to execute the command csc foo.cs:

foo.cs(9,13): error CS0103: The name 'message' does not exist in the current context

foo.cs

using System;

namespace main 
{
    class program 
    {
        static void Main(string[] args) 
        {
            message.print("Hello World!"); //line 9
            Console.ReadLine();
        }
    }
}

bar.cs

using System;

namespace main 
{
    public class message
    {
        public void print(string Message) 
        {
            Console.WriteLine(Message);
        }
    }
}

any help would be much appreciated

ALSO: note that both files are in the same directory and both classes are in the same namespace.

Because screenshot was requested

aa aa
  • 11
  • 1
  • _message_ is a class, without creating an _instance_ of this class you cannot use any of its exposed public methods. Unless you have a _static_ method inside that message class. This is basic stuff. I think you need to deepen your knowledge of the language otherwise you will find stumbling blocks at each line you write – Steve Jul 31 '22 at 17:09
  • @Steve Well I did say I was new did I not? And I even tried creating an instance it seems to just raise more errors. – aa aa Jul 31 '22 at 17:16
  • a _static_ method doesn't require an instance of a class. – Steve Jul 31 '22 at 17:28

4 Answers4

1
using System;

namespace main 
{
    class program 
    {
        static void Main(string[] args) 
        {
            message m=new message();
            m.print("Hello World!"); //line 9
            Console.ReadLine();
        }
    }
}
Damian
  • 56
  • 3
1

You should create class instance, and then call the method of that variable.

Not that class names should start with capital first letter.

Try this:

using System;
                    
public class Program
{
    public static void Main()
    {   
        Message msg = new Message();
        msg.print("Hello World!"); //line 9
        Console.ReadLine();
    }
    
    public class Message
    {
        public void print(string Message) 
        {
            Console.WriteLine(Message);
        }
    }
}

Here is a working solution. snippet

aca
  • 1,071
  • 5
  • 15
  • I tried this and it actually raised two different errors: main.cs(9,13): error CS0246: The type or namespace name 'Message' could not be found (are you missing a using directive or an assembly reference?) main.cs(9,31): error CS0246: The type or namespace name 'Message' could not be found (are you missing a using directive or an assembly reference?) (note that I changed the classes to be capitalized) – aa aa Jul 31 '22 at 17:10
  • @aaaa Take a look in the snippet that I've provided, please. There is nowhere `Message` inside my code, I'm not sure where that's coming from. Try using `message` with lowercase, as you declared your class like that. – aca Jul 31 '22 at 17:12
  • I've changed the lower case to be capitalized to formate it better. The results are the same either way. – aa aa Jul 31 '22 at 17:15
  • The working example has both classes in the same file though. I am trying to use the class from seperate files – aa aa Jul 31 '22 at 17:17
  • @aaaa The logic is the same, I've used the same class since it was an online compiler. Only thing that could be possible, based on what I know, is that your classes are in different folders or namespaces. Can you provide us with screenshot of your solution explorer? – aca Jul 31 '22 at 17:20
  • @aaaa Okay, ill provide a screenshot in an edit. – aa aa Jul 31 '22 at 17:22
  • @aaaa What is `ts.cs` in your explorer? Rename it to `Message.cs` – aca Jul 31 '22 at 17:26
  • @aaaa ts.cs was just a random name I gave the file because I was just going to delete it after I was finished playing around with it. I didn't think it mattered. – aa aa Jul 31 '22 at 17:28
  • @aaaa I understand, but that was the issue. That's why it said that `Message` could not be found, since the name of the class was different in code, and in the explorer. Does it work now? – aca Jul 31 '22 at 17:31
  • @aaaa nope. The compiler still generates the same exact error message. Weird. – aa aa Jul 31 '22 at 17:33
  • @aaaa That happens. Try deleting that class, and create a new one, with the proper name. – aca Jul 31 '22 at 17:34
  • @aaaa Nope, same errors. Should I also note that I am not using unity to run my program? I am using VScode and running it in my cmd. – aa aa Jul 31 '22 at 17:38
1

Alternatively to the other answers, print could be static. Also note that the C# convention is that file, namespace, class and method names are in upper camel case, while parameters are in lower camel case. Another convention is to name the file the same as the class.

Program.cs

using System;

namespace Main 
{
    class Program 
    {
        static void Main(string[] args) 
        {
            Message.Print("Hello World!");
            Console.ReadLine();
        }
    }
}

Message.cs

using System;

namespace Main 
{
    public class Message
    {
        public static void Print(string message) 
        {
            Console.WriteLine(message);
        }
    }
}

Edit: Earlier I wrote that Main must be public, because .NET Fiddle required it. However, this is apparently not generally the case.

Marco Eckstein
  • 4,448
  • 4
  • 37
  • 48
0

OK. I found an answer. There were many problems but at the end I had to change up the command I was using.

At first I was using csc foo.cs then I changed it to csc foo.cs bar.cs

As of yet I am unsure of exactly why but I will provide updates when I figure it out.

https://www.c-sharpcorner.com/UploadFile/puranindia/CommandLineCompilerOptions11082009234932PM/CommandLineCompilerOptions.aspx

There is a documentation for options for the csc command.

aa aa
  • 11
  • 1