Raymond.Cao
Raymond.Cao

Reputation: 1

How can I use a self-defined namespace?

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

Upvotes: 0

Views: 125

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 157048

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
    {
    }
}

Upvotes: 1

Related Questions