Master Import (using) file c# - c#

So I have many classes for a unity video game and it is growing VERY quickly, however
I am using quite a few assets to help and as a result I have many using that
are repeated on each class.
Is there a way in c# or unity to have a master file that just contains all of them for example
namespace MasterImports {
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
}
then just adding something like this to each class by default.
using MasterImports;
I just think this makes managment of my skripts alot easier and
means I am unlikely to run into an error where I forget to add a using
then forgetting what one it was i used and having to hunt scripts for it.

This is a deliberate rule of C#. If you do this:
namespace Frobozz
{
namespace Magic
{
class Lamp {}
}
class Foo
{
Magic.Lamp myLamp; // Legal; Magic means Frobozz.Magic when inside Frobozz
}
}
That is legal. But this is not:
namespace Frobozz
{
namespace Magic
{
class Lamp {}
}
}
namespace Flathead
{
using Frobozz;
class Bar
{
Magic.Lamp myLamp; // Illegal; merely using Frobozz does not bring Magic into scope
}
}
Look into namespace aliases:
using n2 = n1.n2;
...
n2.foo something;
What is before the class name should be a complete name space (with/or other class name(s) for nested types). A truncated namespace will not work.

Related

Two namespaces with the same object name

I have two namespaces:
System.Numerics and UnityEngine
Both have the type Vector3.
So now when ever i want to use it i have to declare which namespace before it. Like this:
protected struct CVN
{
public Complex h;
public UnityEngine.Vector2 d;
public UnityEngine.Vector3 n;
}
Is there any way to define that i only want Vector3 from one namespace so i don't have to always type NameSpaceHere.Vector3 every single time ?
Or am i stuck with the tedious nature of stating the namespace every time. Especially since i only need the Complex type from Numerics its quite annoying.
If all you need from System.Numerics is Complex, then:
using UnityEngine;
using Complex = System.Numerics.Complex;
At the top of your file, without using System.Numerics; should do it.
This is an alias.
You can wrap the using directive of the wanted class in the namespace of your current class rather than putting it outside.
Consider this example
namespace System.Numerics
{
class MyClass
{
}
}
namespace UnityEngine
{
class MyClass
{
}
}
using System.Numeric;
namespace ConsoleApplication24
{
using UnityEngine; // inside the namespace
class Program
{
static void Main(string[] args)
{
MyClass xx = new MyClass(); // from UnitEngine instead of System.Numeric
}
}
}

Manage namespace library (C#)

I am trying to organize a library. My issue is that it would quickly gets very large.
This is what i have so far:
namespace MyLibrary {
namespace Math {
namespace Geometry {
public class BezierCurve {
//...
};
}
namespace Combinatorics {
}
}
namespace Collections {
}
//...
}
Its not many lines of code now, but since BezierCurve alone is around 200 lines, creating all of the classes in the above document is not going to be the way to go.
Is it possible to somehow specify the content of BezierCurve in another document and include / refer to it in the namespace Geometry?
Multiple files can declare classes in the same namespace. There is no requirement for a namespace to be fully defined within a single file.
You should have a file per class.
e.g. your BezierCurve would typically be in:
MyLibrary/Math/Geometry/BezierCurve.cs
and be declared within
namespace MyLibrary.Math.Geometry
{
public class BezierCurve
{
}
}
It's recommended to have one type per file (well, with some exceptions). Thus you will have a single namespace without all this nested structure:
namespace MyLibrary.Math.Geometry
{
public class BezierCurve
{
// ...
}
}
If you want to refer BezierCurve in another file, just add a using directive with appropriate namespace:
using MyLibrary.Math.Geometry;
namespace MyLibrary.Math.Combinatorics
{
public class SomeClassFromCombinatoricsWhichUsesBezierCurve
{
// ...
}
}

In C# are the `using` directives of the base class inherited by the child class?

Let's say we have a base class Rectangle and a derived class Square:
namespace Shapes {
using System.Foo;
public class Rectangle {
public Rectangle(int l, int w){}
}
}
namespace Shapes {
public class Square : Rectangle
public Square(int l, int w){}
}
Does the Square class have to explicitly say that it is using System.Foo? I'm getting erratic results. In one project the using directives seem to be inherited and in a web application they aren't.
using statements, in this context, don't compile to code -- they are helpers to make your code read cleaner for others. As a result, they are not "inherited".
So, to answer your question, your Square class needs to reference System.Foo - either with a using statement, or by using a fully qualified class name.
A using statement will only propagate to the next set of closing braces (}) from the level it was declared on within the same the file.
//From File1.cs
using System.Baz;
namespace Example
{
using System.Foo;
//The using statement for Foo and Baz will be in effect here.
partial class Bar
{
//The using statement for Foo and Baz will be in effect here.
}
}
namespace Example
{
//The using statement for Baz will be in effect here but Foo will not.
partial class Bar
{
//The using statement for Baz will be in effect here but Foo will not.
}
}
//From File2.cs
namespace Example
{
//The using statement for Foo and Baz will NOT be in effect here.
partial class Bar
{
//The using statement for Foo and Baz will NOT be in effect here.
}
}
using directives are only shared if the classes are in the same file and they are not nested in the classes themselves like in your example.
For instance:
using System.Foo;
namespace N
{
class A {}
class B {}
}
If this is all in one file, A and B can both use Foo.
I think everyone is missing the point when it comes to using directives. They really have nothing to do with the class at all. using directives in a code file (.cs, .vb, etc...) are not part of the classes defined within the file. They are used by the compiler to resolve namespaces when compiling.
using System.Foo;
namespace Shapes {...
Importing should always be top most and not within a namespace. This will allow the entire structure of the class to rely on that import when needed.

C# web project complaining about a class

I created a new project (web project in C#).
Created a new folder in my project called App_Code.
I then proceeded to add a class with the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Shipper.DataLayer;
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
And in one of my pages default.aspx in the code behind I tried to do:
int i = BL.JustSomeTest();
I am not getting any intellisense when I type in BL.. It says I am missing an assembly or reference. Or it will say The name BL does not exist in the current context.
But do I have to include a reference if the class file is in the same project? I even tried to Build my project and it at first generated a dll file with the name of my solution file, Shipper.dll but as soon as I add this reference it says The name BL does not exist in the current context.
In my default.aspx page I've tried to add a using statement
using Shipper.
But as soon as I do that my namespace BusinessLayer is not shown...
Im confused?
Edit
Here is default.aspx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shipper.BusinessLayer;
namespace Shipper
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetDefaults();
int i = BL.JustSomeTest();
}
}
}
}
Here is my BL.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Shipper.DataLayer;
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
The error reads The type or namespace nameBusinessLayerdoes not exist in the namespace Shipper (are you missing an assembly reference)?
your...
public static int JustSomeTest()
{
return 1;
}
...is out of the 'class' - you cannot have methods defined for the namespace alone.
(at least from your example, it might be just a typo but then you'd need to give us a working example)
your method was outside a class. That's a no-no. Try this instead:
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
Also, if you're expecting BL to pop up, make sure the namespace is referenced (using Shipper.BusinessLayer). Why is this a static class, though? I think you probably don't want that unless you're making extension methods.
Try to delete the namespace declaration:
using System;
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
This forum thread on Wrox might be also useful.
try
using Shipper.BusinessLayer;
Things to try
Verify the namespace is the one you believe it is by viewing the properties of the target project in the solutions explorer.
In Visual Studio use the Object Browser and locate the namespace and verify the class can be seen. If the object browser can't see it, neither can the code.
First thing is make sure you have added a reference to the Shipper project, not the shipper DLL.
Then make sure the Shipper project is re-built, best bet to do a clean and build.
Then check your intellisense again, I suspect you are referencin an oleder version of the Shipper dll.
Try making your class not static but leave your method as static. (only because I never use static classes, but it doesn't appear to make any difference)
Or if you are seriously having problems and can't work it out install a copy of Resharper and it will probably help!

VB.NET namespace abbreviation: How do I make this work in equivalent C# code?

I am a VB.NET programmer by nature and I am having a hard time figuring this out. Any help with the following would be appreciated.
I need to get the C# code (1) below to work. The VB.NET equivalent works just fine, but the C# does not.
Note that both (2) and (3) do work, but this is actually auto-generated code, and I need the VB.NET and C# versions to be as similar as possible.
This does not compile (the fully-qualified name of Engine is ThreeD.QVB.Engine):
using ThreeD.QVB;
namespace QVBScript
{
public class ScriptCode
{
public void Main(ref Engine.QVBObjectsDictionary objects,
Engine.Commands commands)
{
…
However, this does work:
//using ThreeD.QVB; // I'm instead using fully-qualified names in the method
namespace QVBScript
{
public class ScriptCode
{
public void Main(ref ThreeD.QVB.Engine.QVBObjectsDictionary objects,
ThreeD.QVB.Engine.Commands commands)
{
…
This works, too:
using eng = ThreeD.QVB.Engine;
namespace QVBScript
{
public class ScriptCode
{
public void Main(ref eng.QVBObjectsDictionary objects,
eng.Commands commands)
{
…
In VB.NET if you have an import for the first part of a namespace, you can reference just the later half. In C# you cannot do this. You must have a using for the full namespace, or fully qualify your type names. Different languages, different rules.
In your last example you do not need to use the alias.
using ThreeD.QVB.Engine;
namespace QVBScript
{
public class ScriptCode
{
public void Main(ref QVBObjectsDictionary objects, Commands commands)
{
UI.Output Output = (UI.Output)objects["Output"];
Basic rules to remember:
using A.B;
does allow you to refer to types from namespaces A and A.B without fully qualifying them with their namespace (everywhere in the same file).
does not allow you to abbreviate the names of sub-namespaces of A or A.B. by omitting the A. or A.B. part from their names.
namespace A.B { … }
does allow you to refer to types from namespaces A and A.B without fully qualifying them with their namespace (inside the block).
does allow you to abbreviate the names of sub-namespaces of A or A.B by omitting the A. or A.B. part from their names.
Example:
using System.Collections;
namespace A
{
class Top : IDisposable, // importing System.Collections also imports System
IEnumerable, // inside the imported namespace
System.Collections.Generic.IEnumerable<int>
{…} // ^ "using" does not permit namespace abbreviation
}
namespace A.B
{
class Middle : Top, // namespace A available inside namespace A.B
C.IBottom // namespace blocks permit namespace abbreviation
{…}
}
namespace A.B.C
{
interface IBottom {…}
}

Categories

Resources