By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,122 Members | 2,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,122 IT Pros & Developers. It's quick & easy.

Alias names

P: n/a
Hi,

i try to define alias and then use it in another file like this:

// ----------
// file: a.cs
using System;

using MyAlias = MyNamespace.MyClass;

namespace MyNamespace {
public class MyClass {
public MyClass() { . . . }
}
}

// ----------
// file: b.cs

namespace MyNamespace {
public class OtherClass {
public MyAlias m_theClass;
public OtherClass() { . . . }
}
}

Compiler says: "The type or namespace name 'MyAlias' could not be found..."
Am i missing something? I can use the MyAlias in a.cs just fine.

thx

Kimmo Laine
Nov 16 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
The alias will, unfortunately, be effective only in the physichal file where
it was defined.
Since we have nothing like
#include "a.cs"
there is no way around this.
You will have to add the using statement to any file where you want to use
that alias.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Kimmo Laine" <reply.to@newsgroup> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Hi,

i try to define alias and then use it in another file like this:

// ----------
// file: a.cs
using System;

using MyAlias = MyNamespace.MyClass;

namespace MyNamespace {
public class MyClass {
public MyClass() { . . . }
}
}

// ----------
// file: b.cs

namespace MyNamespace {
public class OtherClass {
public MyAlias m_theClass;
public OtherClass() { . . . }
}
}

Compiler says: "The type or namespace name 'MyAlias' could not be
found..."
Am i missing something? I can use the MyAlias in a.cs just fine.

thx

Kimmo Laine

Nov 16 '05 #2

P: n/a
Hi Dennis,

how does the System.Int32 alias "int" work around this limitation?

Kimmo
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:_4********************@news2.e.nsc.no...
The alias will, unfortunately, be effective only in the physichal file
where it was defined.
Since we have nothing like
#include "a.cs"
there is no way around this.
You will have to add the using statement to any file where you want to use
that alias.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Kimmo Laine" <reply.to@newsgroup> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Hi,

i try to define alias and then use it in another file like this:

// ----------
// file: a.cs
using System;

using MyAlias = MyNamespace.MyClass;

namespace MyNamespace {
public class MyClass {
public MyClass() { . . . }
}
}

// ----------
// file: b.cs

namespace MyNamespace {
public class OtherClass {
public MyAlias m_theClass;
public OtherClass() { . . . }
}
}

Compiler says: "The type or namespace name 'MyAlias' could not be
found..."
Am i missing something? I can use the MyAlias in a.cs just fine.

thx

Kimmo Laine


Nov 16 '05 #3

P: n/a
Kimmo,

The primitive types defined in C# are known by the compiler
and is therefore treated in a special way.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Kimmo Laine" <reply.to@newsgroup> wrote in message
news:uZ**************@TK2MSFTNGP14.phx.gbl...
Hi Dennis,

how does the System.Int32 alias "int" work around this limitation?

Kimmo
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:_4********************@news2.e.nsc.no...
The alias will, unfortunately, be effective only in the physichal file
where it was defined.
Since we have nothing like
#include "a.cs"
there is no way around this.
You will have to add the using statement to any file where you want to
use that alias.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Kimmo Laine" <reply.to@newsgroup> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Hi,

i try to define alias and then use it in another file like this:

// ----------
// file: a.cs
using System;

using MyAlias = MyNamespace.MyClass;

namespace MyNamespace {
public class MyClass {
public MyClass() { . . . }
}
}

// ----------
// file: b.cs

namespace MyNamespace {
public class OtherClass {
public MyAlias m_theClass;
public OtherClass() { . . . }
}
}

Compiler says: "The type or namespace name 'MyAlias' could not be
found..."
Am i missing something? I can use the MyAlias in a.cs just fine.

thx

Kimmo Laine



Nov 16 '05 #4

P: n/a
Hi,

AFAIK "using alias" works with "namespace" only.
So you should not use it for "class".

Marcin
Hi,

i try to define alias and then use it in another file like this:

// ----------
// file: a.cs
using System;

using MyAlias = MyNamespace.MyClass;

namespace MyNamespace {
public class MyClass {
public MyClass() { . . . }
}
}

// ----------
// file: b.cs

namespace MyNamespace {
public class OtherClass {
public MyAlias m_theClass;
public OtherClass() { . . . }
}
}

Compiler says: "The type or namespace name 'MyAlias' could not be found..."
Am i missing something? I can use the MyAlias in a.cs just fine.

thx

Kimmo Laine

Nov 16 '05 #5

P: n/a
Although using statements certainly is more frequently used to import
namespaces,
they may also be used to create aliases for types.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Marcin Grzêbski" <mg*******@taxussi.no.com.spam.pl> wrote in message
news:co**********@atlantis.news.tpi.pl...
Hi,

AFAIK "using alias" works with "namespace" only.
So you should not use it for "class".

Marcin
Hi,

i try to define alias and then use it in another file like this:

// ----------
// file: a.cs
using System;

using MyAlias = MyNamespace.MyClass;

namespace MyNamespace {
public class MyClass {
public MyClass() { . . . }
}
}

// ----------
// file: b.cs

namespace MyNamespace {
public class OtherClass {
public MyAlias m_theClass;
public OtherClass() { . . . }
}
}

Compiler says: "The type or namespace name 'MyAlias' could not be
found..."
Am i missing something? I can use the MyAlias in a.cs just fine.

thx

Kimmo Laine


Nov 16 '05 #6

P: n/a
Marcin Grzebski <mg*******@taxussi.no.com.spam.pl> wrote:
AFAIK "using alias" works with "namespace" only.
So you should not use it for "class".


Nope, that's not true. For instance:

using FooBar = System.Console;

public class Test
{
static void Main()
{
FooBar.WriteLine ("Hello");
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7

P: n/a
Hi Jon and Dennis,

Upsss....
My ignorance was a step before .NET docs :-(

THANKS!
Marcin
Nov 16 '05 #8

P: n/a
Hi,

Some time ago i tried something like this:

using System.Console;

public class Test
{
static void Main()
{
Console.WriteLine ("Hello");
}
}

....but it does not work, without alias (now i can see):

using Console=System.Console;

JAVA have support for class (and packages) "import", and i miss
this feature in C#.

Marcin
Nov 16 '05 #9

P: n/a
VB.NET has this "feature" as well.

I wouldn't implement such a thing in my code. For instance:

using System.Console;
..
..
..
WriteLine("Hello green world!");
..
..

Now how would instantly know, when reviewing the code, that i mean
Console.WriteLine and not Debug.WriteLine??

Maybe for console and debug this is fine, but for our domain objects?
*shrug*

--
Patrik Löwendahl [C# MVP]
cshrp.net - 'Elegant code by witty programmers'
cornerstone.se 'IT Training for professionals'

Marcin Grzębski wrote:
Hi,

Some time ago i tried something like this:

using System.Console;

public class Test
{
static void Main()
{
Console.WriteLine ("Hello");
}
}

...but it does not work, without alias (now i can see):

using Console=System.Console;

JAVA have support for class (and packages) "import", and i miss
this feature in C#.

Marcin

Nov 16 '05 #10

P: n/a
Hi Patrik,
I wouldn't implement such a thing in my code. For instance:

using System.Console;
.
.
.
WriteLine("Hello green world!");

Now how would instantly know, when reviewing the code, that i mean
Console.WriteLine and not Debug.WriteLine??

Maybe for console and debug this is fine, but for our domain objects?
*shrug*
In JAVA class "import" (C# using) means that this class is directly
available, like in the code below:
using System.Console; // its forbiden i C#, of course

public class Test
{
static void Main()
{
Console.WriteLine ("Hello");
}
}
So, you cannot access directly to its "static" methods (fields,...)
WriteLine("Hello green world!");


but you can use it name without its namespaces (packages).
Console.WriteLine ("Hello");


Only difference in JAVA's "import" is that all classes from within
package are declared by "*" e.g.:

import java.util.*;

means that you can access all classes from package "java.util"
but:

import java.util.ArrayList;

means that only class "ArrayList" is available to use in your code.

So, its a most common way (in Java coding) to imports all needed
classes, besides imports its packages.

As i wrote before i can use alias same with class name in C#,
but it is the option only, e.g.:

using ArrayList=System.Collection.ArrayList;

Regards
Marcin
Nov 16 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.