I am not able to write "Properties.Settings.Default". It is not coming in the dropdown list. Is there any namespace that we can add?
Add your project namespace to the Properties namespace. The default namespace defined in the project settings.
For instance if your project default namespace is MyApp.
MyApp.Properties.Settings.Default
Make sure you are on the same namespace and try to access the properties like this :
namespace.Properties.Settings.Default
or if you are on different namespace don't forget to use
using namespace
An additional hint on top of the above answers:
Go to solution explorer -> Properties folder -> Settings.settings ->
Settings.Designer.cs file
and see which namespace is being used there. Ensure that you are using this namespace in your code file as others have suggested
Once you setup your environment in the settings under properties of the app. You want to reference it to the project like this:
using App.Properties.Settings
Then in the code you write you can use as example:
userName.Settings.Default
In this case userName is set within the settings.
Set your Setting in your project
go to Project => property => go to setting
Set your element (string,int, ecc...)
Save from program all values
Properties.Settings.Default.User = txtUser.Text; Properties.Settings.Default.Password = txtPass.Text;
Properties.Settings.Default.Save();
if you use Windows 10, this is the directory of setting Values:
C:\Users\AppData\Local<ProjectName.exe_Url_somedata>\1.0.0.0<filename.config>
Related
C# 10 introduced file-scoped namespaces, which I would like to use in Visual Studio's class templates. I've updated the 'Class' template file to the following:
namespace $rootnamespace$;
class $safeitemrootname$
{
//I put this comment here to make sure it's using the right file
}
But when I create a new empty class I get this autogenerated code:
namespace ProjectName
{
internal class Class1
{
//I put this comment here to make sure it's using the right file
}
}
What do I need to do to make the auto-generated code for an empty class look like this?
namespace ProjectName;
internal class Class1
{
}
For reference, I am using Visual Studio 2022 Professional and my project is using C#10 with .NET 6.
The location of the class template file that I am modifying is: C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
You have to set up your project's editorconfig to prefer File-scoped namespaces.
Right click your project. Select "Add" → "New Item"
Select "editorConfig File (.NET)"
Double click the new editorconfig file. In the "Code Style" tab set "Namespace declarations" to "File scoped"
The code template will now work as expected.
Check this thread: https://stackoverflow.com/a/69889803
They use a .editorconfig file where you can specify the namespace declaration style. When creating a new file in VS 2022 it will use that new style
It can be seen and compiled inside the same file it is in inside the same project, the file is called default.aspx.cs.
However when I try to include the namespace in another file of the same project using the using DBConnStrings; statement -> I keep getting the compiler error "The type or namespace name 'DBConnStrings' could not be found".
The Code in the file which compiles called default.aspx.cs is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using DBConnStrings;
namespace DBConnStrings
{
public class GlobalStrings
{
public static string carSalesDBConnString =
ConfigurationManager.ConnectionStrings["ConnectionString2"].ToString();
}
}
public void BindManu()
{
SqlConnection conn = new
SqlConnection(GlobalStrings.carSalesDBConnString); // Connect to Carsales database
conn.Open();
// .....
}
The other files can not see this namespace although they are in the same project.
How do I make them see it?
First of all, I'd suggest to put the method BindManu into the class, because I can't imagine this would work like that.
But to your problem: You have to specify the whole namespace. That means, if the file with the DBConnStrings namespace is in the folder test, you have to use using test.DBConnStrings to import the namespace. You should name the namespace like that as well to avoid confusion (namespace test.DBConnStrings).
But you actually don't have to specify the whole path, just the path within the project. That means, if your class is in C:\Users\Foo\Documents\Visual Studio 2017\Projects\BlaProject\DirectoryA\DirectoryB\MyClass with the project located in C:\Users\Foo\Documents\Visual Studio 2017\Projects\BlaProject, your namespace would be BlaProject.DirectoryA.DirectoryB. If the file your using isn't in your project folder, then you have to add a reference and use the path within the other project as above (but with another project name, obviously). If you want to add references, open the Solution Explorer, right click onto References, select add Reference, and select the reference to the project.
If you don't want to struggle with all that you can let vs do it for you. Simply type in the class you want from the other namespace, it will be marked as an error, click onto the lightbulb and select something like add using reference.
Furthermore, if you want to add a class to your project, don't do it with the explorer - simply right click onto the folder from your project you want to add the class to in your Solution Explorer, select Add, then New Item. In the popup select class and type in your name for the class. That's it!
I have created a solution in VS 2015 given below. I have added a folder Mapper and inside that a class DefaultMapper.cs. I want to access this class(DefaultMapper.cs) from defalut.cs. I could not able to access it.
If I put the same class inside App_code folder then I am able to access it.
I am also using public access modifier everywhere.
Can anybody help me on it. Thanks in advance.
""
You have to add namespace like this (ShortCut CTRL + . (dot) will suggest you to add this while you are trying to create an instance.),
using POC.Mapper;
then you can create instance,
DefaultMapper test = new DefaultMapper();
or
POC.Mapper.DefaultMapper test = new POC.Mapper.DefaultMapper();
You told that you able to call this class if you put it inside app_code. Check namespaces in Default.cs class and you will see that using POC.App_Code is already there. Thats why you can call from there.
Hope helps,
I have a class with a namespace not matching the folder structure of the project.
With // ReSharper disable once CheckNamespace I can disable the hint that the namespace does not correspond to the file-location. But when Refactoring the Projects namespaces, the class is still selected for adjusting the namespace.
Is there a way to disable the adjust namespace?
Update with sample:
In my case I have a view class that has to be in the System.Web.Mvc-Namespace.
The file location is [Root]>src>Views
The default namespace of the Root-Project is Com.xxxxx.Commons.Web
So Resharper always wants to adjust the namespace to either
Com.xxxxx.Commons.Web.Views or to Com.xxxxx.Commons.Web if I disable the namespace-provider-flag für the Views folder.
Unfortunately it's a long existing bug in R# that disable CheckNamespace isn't respected by the "Adjust namespaces" refactoring. As I know, there is no other way to exclude specific files from this refactoring.
You can disable the folder as a Namespace Provider by opening the properties window (right click -> properties) for the folder you wish to exclude and change Namespace Provider to false.
ReSharper's Adjust Namespace refactoring no longer affects namespaces marked with // ReSharper disable CheckNamespace. The long-existing bug in ReSharper (link referenced in #ulrichb's old answer) has been fixed in ReSharper 2016.2.
Limitations (currently, as of ReSharper 2018.1)
If there is no namespace at all, ReSharper will automatically add a namespace even if disable CheckNamespace is specified for that file.
For example, this works:
// ReSharper disable once CheckNamespace
namespace DoNotAdjustThisNamespace
{
public class TestClass1
{
}
}
However, this does not:
// ReSharper disable once CheckNamespace
public class TestClass1
{
}
There is still a misleading UI issue where all files still appear in the Adjust Namespace UI, but the namespace changes will not actually take place. This is a bit misleading..
I have a Windows Form application. I've added a class to my project named MyLibrary.cs and I inherited this class of System.ComponentModel.Component and I've also added a bitmap image with its build action set to EmbeddedResource to my resources folder, but when I write the code below and build the project my customized icon does not change when I want to drag and drop my component to my Form. Would you please help me?
I've checked a lot of links like http://msdn.microsoft.com/en-us/library/system.drawing.toolboxbitmapattribute.aspx and How to find the elusive ToolboxBitmap icon but none of them worked for me!
Here is my code:
MyLibrary.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
internal class resfinder
{
}
namespace MyForm
{
[System.Drawing.ToolboxBitmap(typeof(resfinder),"MyForm.Bitmap1.bmp")]
public class MyLibrary:System.ComponentModel.Component
{
}
}
You should look here it says:
The bitmap must be 16x16
If you specify both a Type and a string, the control searches for an image resource with the name specified by the string parameter in the assembly containing the type specified by the Type parameter
So make sure your bitmap is located in the same assembly as "resfinder" and also put make sure in properties under build actions it is set as an Embedded Resource