Sunday, August 2, 2009

Is there a way to import a class to a C# application?

In C# you can use the "using" statement to import a namespace. However I have entered two namespaces with the same class and it is giving me an error because the compiler doesnt know which class to use.

Is there a way to import a class to a C# application?
You have two options:





One is to specify the whole name for the class, like this:





Core.Math.Vector3 vector = new Core.Math.Vector3();





Another is to use the using statement in a different way:





using CoreVector3 = Core.Math.Vector3;


using PhysicsVector3 = Physics.Vector3;





Now you can use CoreVector3 or PhysicsVector3 in that file, and it will be using the correct class name behind the scenes. I prefer this method because it saves typing.
Reply:http://msdn2.microsoft.com/en-us/library...





CodeNamespaceImport Class





Represents a namespace import directive that indicates a namespace to use.


_________________





Change the code so that when you refer to the two conflicting class names, you use the fully qualified type references (i.e., including name of namespace).





If you are not using the two class in the same section of code or within the same scope, then consider placing the Using %26lt;namespace%26gt; as a scope delimiter—on an as-needed basis. You would only have to do this for one of the conflicting classes. Whereas, the other such class could be affected by the Using %26lt;namespace%26gt;.


_________________





So, to avoid having to use the fully qualified class name, is it possible to scope the namespace as needed? For example, place Using %26lt;namespace%26gt; right before the first use of a particular class. Allow the scope to continue up to the point where you use the other similarly name class. Then, change the namespace once again. I realize that it is rather convoluted. I would also recommend that you use comments liberally to indicate the beginning and the end of the each scoping, along with the rationale behind it.





________________
Reply:use a different class for one of them


No comments:

Post a Comment