0

In the Mono basis, there is an example:

using System;
using System.Windows.Forms;

public class HelloWorld : Form
{
    static public void Main ()
    {
        Application.Run (new HelloWorld ());
    }

    public HelloWorld ()
    {
        Text = "Hello Mono World";
    }
}

I want to know how to add self-defined namespace in C# code? For example, adding a new namespace

using myComponent;

in order to attach a new string,

myString.FirstString

will cause an error when compiling:

using System;
using System.Windows.Forms;
using myComponents;

public class HelloWorld : Form
{
    static public void Main ()
    {
        Application.Run (new HelloWorld ());
    }

    public HelloWorld ()
    {
        Text = "Hello Mono World " + myString.FirstString;
    }
}

helloworld.cs(3,7): error CS0246: The type or namespace name 'myComponent' could not be found (are you missing a using directive or an assembly reference?)

Below is the self-defined "myComponent", how can I integrate the Main() and "myComponent" together ?

namespace myComponent
{
    public static class myString
    {
        public static string FirstString
        {
            get
            {
                return "connection";
            }
        }
    } //Class end
}//Namespace end

What should I do? Even I use the same namespace(myComponet) in the two files, I still get the error.

Thanks !!


more info provided 1. The two cs files are in the same directory

2. The execute progress as below

C:\Users\xxxxx\CSharpWorkSpace\Pop20b>csc helloworld.cs -r:System.Windows.Form s.dll Microsoft (R) Visual C# Compiler version 2.6.0.62309 (d3f6b8e7) Copyright (C) Microsoft Corporation. All rights reserved.

warning CS1668: Invalid search path 'C:\Program Files (x86)\sql11\LIB' specified in 'LIB environment variable' -- 'directory does not exist' helloworld.cs(3,7): error CS0246: The type or namespace name 'myComponent' could not be found (are you missing a using directive or an assembly reference?)

C:\Users\xxxxx\CSharpWorkSpace\Pop20b>mono --version Mono JIT compiler version 5.10.0 (Visual Studio built mono) Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-proj ect.com

  • Do you even have a namespace called `myComponents`? – Sebastian Hofmann Mar 16 '18 at 08:48
  • yeah, actually, it is a VS2015 project, running well in WIndows7. Now, customers want to use this application in Linux platform, so, I am trying the Mono. But I failed. The project has many self-defined namespace. – Raymond.Cao Mar 16 '18 at 08:52
  • @Sebastian, I update the question, could you have a look ? – Raymond.Cao Mar 16 '18 at 10:06
  • I would *start* by following .NET naming conventions, even in sample code like this. Anything that doesn't follow the convention is immediately a distraction. – Jon Skeet Mar 16 '18 at 10:09
  • Well, that answers my question :) – Sebastian Hofmann Mar 16 '18 at 10:09
  • Please show how you're trying to build in Mono. My guess is that *that's* the problem. If you could provide a [mcve] complete with how you're trying to build (and the precise version of Mono) it would be a lot easier to help you. – Jon Skeet Mar 16 '18 at 10:10
  • From your edit, I wonder if you think that csc.exe is related to mono... It is not. – Cleptus Mar 16 '18 at 13:54
  • Related https://stackoverflow.com/questions/553143/compiling-executing-a-c-sharp-source-file-in-command-prompt – Cleptus Mar 19 '18 at 09:05

1 Answers1

1

Can you only use a namespace, if it has been defined. You can do that by creating a class, struct, enum or other object in that namespace, or include an assembly that has that namespace.

If you put this in your code, your using will work:

namespace myComponents
{
    public class SomeComponent
    {
    }
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Or if that namespace is defined in an assembly referenced in the project – Cleptus Mar 16 '18 at 08:52
  • @bradbury9 Indeed, included in my answer. – Patrick Hofman Mar 16 '18 at 08:55
  • @bradbury9, I've tried to use the same namespace "myComponent", both in MainForm, and in myComponent, I still meet the same error. I've updated the description, would you have a look ? – Raymond.Cao Mar 16 '18 at 10:08
  • @PatrickHofman, I've tried using the same namespace, it works. But I still want to keep two files, I don't want to merge them into one single file, because it is a large project. What can I do ? Thanks. – Raymond.Cao Mar 16 '18 at 10:10
  • @Raymond.Cao I would put enphasis on the `or include an assembly that has that namespace` from Patrick's answer. If SomeComponent namespace is in a different .cs file, you should tweak your `csc.exe` line to include all used .cs files. – Cleptus Mar 16 '18 at 13:50
  • @bradbury9, thanks, I've compiled this simple example by using "csc *.cs" command, all the .cs files are in the same directory in this simple case, I understand it is not Mono's. I want to know, how I could compile a large project by manually using "csc" ? there is lots of directories and files, should I type them all in "csc ..." ? Or is there any file I could use ? And, what do you mean by `include an assembly that has that namespace` ? Is it like "" or something ? Thank you again !! – Raymond.Cao Mar 18 '18 at 03:29
  • Added a link for a related or duplicated answer. If the namespaces you use are in another dll (assembly in .net syntax) you must reference it (in the accepted answer of the linked question it says hot to reference external assemblies). – Cleptus Mar 19 '18 at 09:24
  • @Raymond.Cao for complex escenarios you should consider other alternatives. Visual Sudio internally uses msbuild. Check this doc https://msdn.microsoft.com/en-us/library/dd576348.aspx – Cleptus Mar 19 '18 at 14:11
  • Thank you @bradbury9. I've run the C# in linux now. Although there is still other issues, I need further investigation. Thanks again ! – Raymond.Cao Mar 21 '18 at 06:42