using directive and namespaces

  • Thread starter Thread starter neverstill
  • Start date Start date
N

neverstill

Hello-

I am a little confused about something. I have create a .cs file that looks
like this:
using System;
namespace MyApp.Types
{
using INT = System.Int32;
}

In my Main() function, even though I have added the using sirective : using
Types; I get compiler errors with this line of code:
INT nrBones = 0;


So this doesn't make sense to me. How do you have custom types all over
your application if you can't do it like this? What am I missing?

Thanks for any help,
Steve
 
So this doesn't make sense to me.

It does to me. :-) The using alias syntax only affects the source file
it's in, not your entire project.

How do you have custom types all over
your application if you can't do it like this? What am I missing?

Copy the using statement to all source files you need it. Or define
your own struct called INT with implicit conversion opererators
to/from int. Or just avoid creating type aliases.



Mattias
 
I figured that was the case, but I couldn't believe it.
I need to use a ton of C structs that use types like DWORD, _WORD, INT and I
that that I could just add a bunch of using statements to map things around.
Nope! ;)

Thanks for the quick response, I will just cram it all into one file,
ugly... but this is a ont-time-use tool, so who cares ;)
 
neverstill said:
Hello-

I am a little confused about something. I have create a .cs file
that looks like this:

using System;
namespace MyApp.Types
{
using INT = System.Int32;
}

In my Main() function, even though I have added the using
directive : using Types; I get compiler errors with this line
of code: INT nrBones = 0;

So this doesn't make sense to me. How do you have custom
types all over your application if you can't do it like this?
What am I missing?

You haven't created a custom type, merely an alias [i.e. alternate or
shortened name] for an existing type. I'm assuming, also, that this is what
you actually wish to do - post again with additional information if it
isn't.

The reason the 'aliasing' didn't work was because you created it [I'm
assuming this, since you didn't show all your code] in one scope but
attempted to use it in another.

The following code snippets should helps clear this up. As they show, you
just need to ensure the alias created by the using directive is done so in
the correct scope / namespace.

I hope this helps.

Anthony Borla

// FILE: Example1.cs
using System;

using INT = System.Int32;

public class X
{
INT returnINT() { INT x = 5; return x; }

public static void Main(String[] args)
{
INT x = new X().returnINT();
Console.WriteLine(x);
}
}

// FILE: Example2.cs
using System;

namespace MyApp
{
using INT = System.Int32;

public class X
{
INT returnINT() { INT x = 5; return x; }

public static void Main(String[] args)
{
INT x = new X().returnINT();
Console.WriteLine(x);
}
}
}
 
Back
Top