How to access a class which is in a folder c# (wpf)? - c#

I'm building a WPF application. I'm almost finished with one part of it, and now I want to clean up the code a bit. English is not my native language (so sorry if this gets marked as a duplicate) and I couldn't find anything about this, so I hope someone can help me out.
If I have public voids in a class called 'class.cs' and this class is in a folder called 'classes', how do I access the class so I can use its functions?

At the top of the file where you want to use the class include a using statement.
using [namespace];
Then you will be able to instantiate the class
Class testClass = new Class();
testClass.Method();
If you aren't sure what the namespace for your Class class is, then you can write the line to instantiate the class and then use the intellisense (assuming you are using a development ide like Visual Studio) to automatically include the reference. Alternatively, you can look at the top of your class.cs file to see what namespace has been declared.

check what namespace it's in , then what the class name is and call it as
"Namespace.class"
in case that you can't see it still , check if the class is public and the class.cs properties have the build property set to compile

I'm not sure from where You are trying to access this class. But I think the easiest way of doing it probably will be just typing your class name in place where You are trying to use it and press 'ctrl+.' which should show You context menu with options to add using statement for this class.
If You are trying to access this class from other project in solution then above way will only work with VS2017(because it will also add missing project reference) otherwise You need to add reference for this project and then add missing using statement for your class(in your case 'using ProjectName.classes').

The folder you are talking about is called a namespace. You first need to import the namespace and then you can use your class.
*Normally the IDE will prompt you.

Related

Is it possible to create new C# scripts with proper namespace?

I'm using the C# Mono version for Godot and would like to know if it is possible to configure Godot to make use of namespaces. When creating a new script at
./Folder/MyClass.cs
I would expect the namespace to be
MyProject.Folder
So the created file should be similiar to
using Godot;
namespace MyProject.Folder
{
public class MyClass : Node2D { }
}
Is that possible?
I don't think it is possible to configure Godot to do this. To be clear, it is possible to create custom templates for scripts, see Creating script templates. However, as far as I can tell Godot does not offer a way to use the folder path (or its parts) in a template.
I also don't think its possible to do this full automatically.
But if you are using vs code you can just click on the class definition and press ctrl + point or press the lamp icon to show some quick options and than you can select the change namespace to MyProject.Folder option.

How to refactor a file namespace?

I have a N-Layer project and when im defining the ContextBounderies i use to change it during the needs of the development. For example i have this class:
namespace Domain.Entities.Item
public class ItemForSale
{
}
This class is now inside the Entites/Item folder in the solution. But during the development I realize that this class will make more sense inside the Sell folder, so the namespace should be namespace Domain.Entities.Sell. Well, the problem is that i have lots of references of this class in a number of layers projects, so its always a pain to refactor it by hand.
Is there a way to configure it, or some free extension (resharper do that but its not for free) wich do this job?
Im using Visual Studio 2015
Thanks!
Yes, this is just a feature of VS2015. You can right click on the namespace you want to change, and then select the "Rename" options under the drop-down. It will update all of the references inside of the solution. So, if you have multiple projects inside of your solution, they will all get updated with the new name.
(Also, there is a keyboard shortcut as well. You can click the namespace you want to change, and then press F2 on your keyboard to bring up the Rename menu.)
If the problem is not frequent then you can use Replace in Files option to replace all old namespace occurences to new ones. Just use Crtl+Shift+H, fill appropriate fields and select Look in option to look in Entire solution.

Moved a class to a different namespace, is it possible to add an implicit using to my files?

I moved a class from one namespace to another, and now I have over 2000 errors to go through.
All the errors are related to the class that I moved, is it possible to implicitly or globally add a namespace to all my files somehow?
Or is the only way to manually go and fix each error?
I have ReSharper, but I am weary of making a global change and not even sure if Resharper go fix this ONE issue in all the files?
With ReSharper you can right-click the class name, select Refactor from the popup menu and select Move... select Move Type To Another Namespace. ReSharper will change the namespace and correct all the files referencing the new namespace, an option if you can rollback your move.
Are you implementing the using statement on your classes that are calling this class?
If so, just use the search and replace for the entire solution from the old namespace to the new name. It will do it with. If they are in seperate projects in the same solution, you will have to add. a reference to that project.

C# resx file not working after changing namespace

I am working on an existing windows form application. I only need to make few modification, during those changes i came across a scenario where i need to rename an existing namespace aswell. Now after renaming that namespace my project is not running. Although it compiles fine but breaks at the following line of code
ImageList il = new ImageList();
ImageList = il;
il.Images.Add((Image)new Bitmap(GetType(), "Images.ImageFileName.png"));//when this line is executed a dialog box appears and says that "there is no source code available for current location"
Now since i am new to .net world so i study this problem and figure that it arises due to change in namspace. I also trying renaming the namespace in the Resources.Designer.cs
but it also didn't solve my problem
Thanks All.
I find the solution by myself. The purpose of posting solution here is that may be some one else benefit from it.
Infact its a sort of carelessness from my side because i forget to change the value of Property "Default NameSpace" this property contains the old namespace that's why my resource file alwasy point to old namespace
Right click on Resources.resx, select Properties, find Custom Tool Namespace, set the namespace that you need.
Also, why don't you reference your resources in the typed manner like Resources.ResrouceName.
Here is a quick tutorial how you can setup your resources so you don't have to cast and think about namespaces: http://msdn.microsoft.com/en-us/library/7k989cfy%28v=vs.80%29.aspx
try getting rid of that second line, ImageList = il;. what it's doing is replacing a class with an instance of a class. not good.
I have also came across this issue. I had to rename the namespace of my application. After that the designer was ignoring all resources, although they were appearing after building a solution and running the application.
I found that even though I had renamed the namespace, it wasn't replaced in Application Properties under "Default Namespace" textbox. I have changed the old namespace which was still appearing there with the new namepspace and it worked properly afterwards.
That specific overload of the bitmap constructor combines the namespace of the given type with the string name of the resource and looks for a match in the assembly manifest. So in this case it would be the namespace of your class + ".Images.ImageFileName.png". I'm guessing that does not exist. Change the namespace of the Resources.resx by right clicking on it and selecting properties, do a Rebuild and see if it works now.

Namespace & Class Conflict

I'm facing a problem with a conflict between the DateTime class and a namespace for some unknown reason was also called DateTime.
Assembly CompanyDateTime has namespace Company.DateTime
My Application is in the namespace: Company
the problem is that everytime I need to use DateTime class, I have to explicitely say System.DateTime is their any way of getting around this?
Is is possible to say SomeRandomStuff = Company.DateTime and have DateTime always be System.DateTime
Note:
I need to reference this Assembly in my application eventhough I do not use it because some Assembly that I need actually uses this class.
I can use an entry on the app.config file to identify a dependent assembly but I cannot do that since company policy is against it and all referenced assembly needs to be in the output folder.
Deployment goes through a build server
Possible Resolution?
Is is possible for CompanyDateTime to get automatically deployed to the output folder without adding it in the reference?
Question : the problem is that everytime I need to use DateTime class, I have to explicitely say System.DateTime is their any way of getting around this
Answer : Already answered above - use an Alias eg
using CompanyDateTime = Company.DateTime;
using StandardDateTime = System.DateTime;
Question : Is is possible for CompanyDateTime to get automatically deployed to the output folder without adding it in the reference?
Answer : Put this dll in the application root folder and create a postbuild event that copies this to the output folder. You can use the regular DOS COPY command here.
Refer link for postbuild event details
Yes, use a using directive at the top of your code files that reference it like this.
using SomeRandomStuff = Company.DateTime;
Edit: you may also need another one to resolve the ambiguity:
using DateTime = System.DateTime;
Give the assembly an alias (the default is global, set yours to something else). You can set this in Visual Studio's property window when you select a reference, or using a compile switch if you're manually compiling. The compiler will only resolve things in the global alias, unless you specify an external alias at the top of your code file (extern alias myalias).
Note, this is different from the namespace alias others have mentioned. With this you should simply be able to use DateTime to refer to the System.DateTime, rather than using another name. If you need to refer to the other one later, you'd need to specify myalias::Company.DateTime...
Use an alias.
If you want to use System.DateTime without having to use System, then try:
using SysDate = System.DateTime;
Then, just reference it like you would a class:
SysDate mySystemDotDateTime = new SysDate();

Categories

Resources