I have to store some strings in the Settings file of my project. However, I am not able to access the Settings file to store the strings.
I definitely have accessed Properties file in an earlier project without facing any problems to store values.
Properties.Settings.Default.test = ...
MyApp.Properties.Settings.Default.test = ...
But here in the current project, I am constantly being driven mad by getting the Properties does not exist in the current context
Is there any setting or something that has changed because of which Properties does not appear in Intellisense?
I had this same problem occur when I had been copying and pasting code for plug-ins. It turns out that I had accidentally left the namespace of the other project in place when I copied. By changing the namespace back to the appropriate project, I was able to use the Properties.
at compile-time, Visual Studio generates a c# class which allows access to user and application settings. the generated class is defined in the default namespace specified in the project settings.
you may have to prefix any access to the Properties namespace with the default namespace defined in the project settings;
or you may change the default namespace: go to project settings, application tab, and edit the default namespace of the project.
Make sure your project has a settings file by going to the project properties and clicking on the Settings tab. If there is no settings file, it will tell you so and provide you with the option to create one.
I had this error when I accidentally had the wrong namespace as the Default namespace in the project properties.
To choose the default namespace, right click the project in the Solution ExplorerProperties->Application tab->Default namespace and check that it matches the namespace of your .cs files:
[...]
using System.Text;
using System.Threading;
namespace Correct.Namespace //<-- should match Default namespace field
{
static class Program
{
[...]
In my case, i have referenced a resource that has an Internal/Public access modifier. I have changed its access modifier to Public and the problem was gone.
First, try to recreate your project from scratch, if it's your testing environment. This step will recreate default namespace properly. You may skip this step if you are confident with your namespace configurations.
Second, if you still get Properties unrecognize as in my example where im trying to get data from sqlCon.
string connectionString = Properties.Settings.Default.sqlCon;
go to Project -> EventsandDelegates Properties as shown in the picture below. Make sure to create default settings file.
As soon as you create default settings under your Application settings, in my case it's SQL connection variable. Properties error will go away.
I just had the same problem. But the problem occured when I added a new setting to an already defined settings file (created a long time ago). Somehow adding a setting to the settings file made Visual Studio change the setting's namespace to the project default namespace (which had changed since the settings where modified last time).
So, because the settings access modifier was set to Internal, and the class trying to access the settings still where in the old previous namespace (still in the same project though), the settings became invisible from that class.
Quickest Solution i can offer is to double click on your resources.resx file if it exists, than change the Access Modifier Property at the top of the window from Internal to Public, Rebuild, which should compile successfully, then switch it back to internal and rebuild again. Should be fine after that.
In my case I was referencing one project (A) from another (B). Both in the same solution.
A has a public class (AClass) with a public static method. (AMethod)
I wanted to Test the results of this AClass.AMethod(...)
Visual Studio "colored" AClass in the call AClass.AMethod(...)
and I could right-click AClass and "Go to Definition",
which took me either directly to the source if I added referenced to the "Solution" project
Or to the DLL "outline" if I added reference to the DLL created by building Project "A"
But when I tried to build project "B" I got the error "AClass does not exist in current context"
It turns out Project B was targeting 4.5 and Project A was targeting 4.5.2
This answer helped: https://stackoverflow.com/a/15556365/1175496
Make sure you can see your setting in Settings.Designer.cs file. If not, try deleting and reentering the setting. For some reason, sometimes, the code auto generated by the tool does not include your setting. In that case, the setting wont be available in intellisense.
Funny, but in my case, I had defined the Properties in one project in the solution and was trying to access from another.
Hope this might help someone.
My issue with the "does not contain a definition for 'Properties'", was caused by adding a class to the namespace (JsonBreaker), with the same name as the namespace (ie JsonBreaker.JsonBreaker). Its notable that it was breaking compilation, as it seems some other folks are having a different experience.
namespace JsonBreaker {
public class JsonBreaker {
private readonly Options _opt = null;
...
I just changed the classname (and .cs filename) to Breaker, and my JsonBreaker.Properties.Settings.Default["InteractiveMode"] call started compiling again.
namespace JsonBreaker {
public class Breaker {
private readonly Options _opt = null;
...
I had this problem when running my web project in Visual Studio 2019. After unsuccessfully trying some of the solutions here, I closed VS2019 and re-opened the project from VS2017. Now the Properties namespace is being recognised.
My case:
I added a new database in the Solution Explorer
I tried:
string connectionString = Properties.Settings.Default.SlovickaSQLConnectionString;
My error:
I get:
Properties does not exist in the current context
Here is why:
I forget to add a new dataset in Data Sources panel.
This dataset has to be connected to the previously added database.
And now it works..
Related
In a dll solution, I changed the main public class (namebase) to have a better name EasyXML.Settings class has now becomme EasyXML.XMLconfig.
namespace EasyXML
{
public class XMLconfig // i changed this name and its constructor.
{
When compiling i did a clean solution first and then i compiled both debug and release versions for this .dll
Another project already used the older version, but needed to update to the new version. Thus I removed references to the old dll, and removed old dll, added the new version of the dll, and created a reference to it again.
However somehow that other project doesn't recognize the updated class name XMLconfig. (not on clean builds either), it behaves like if it is cached or so.
I'm not sure, do i need to do additional actions on the dll solution, or on the other project?, to refresh those names.
There must be a simple explanation for this:
make sure there aren't any errors in the second project (otherwise VS most likely fails to update the class names from the dll)
make sure by adding the reference of the dll to the second project you are referencing the right file (use solution tab instead of browse)
make sure your dll project was actually compiled. (There are several cases where the project didn't compile but the solution build was successful). Check the modified date of the dll project output and make sure it isn't an old one.
you can go to project->properties->build->output path and project->properties->build events and make sure the output for the current build configuration points to a right location
make sure both projects use the exact same version of .net framework
other than that:
make sure the class name (XMLConfig) is not exact match of a namespace (EasyXML) otherwise the class might get inaccessible to the VS user
make sure there aren't any partial classes in dll project with the same name.
make sure there aren't any other classes somewhere else with the same namespace and class name
I've just upgraded from VS2013 to VS2015, and a ton of CS0436 warnings have appeared, all seemingly relating to the same issue.
I am slowly migrating web applications from VB to C#, so perhaps this is something really simple. I'm new to C# so please use layman-type answers...
My solution is structured as such:
Project 1 - Reusable methods (database access, etc)
\CommonDataAccessFunctionality.vb
Namespace MyCompany
Public Class CommonDataAccessFunctionality
Public Sub New(ByVal storedProcedureToRun As String)
' db stuff here '
End Sub
End Class
End Namespace
Project 2 - Web Applicable (C#) with dependency on Project 1
App_Code\DataAccess.cs
using System.Data;
using System.Data.SqlClient;
namespace QrCodes.App_Data
{
public abstract class QrDataCommon : MyCompany.CommonDataAccessFunctionality
{
public QrDataCommon(string storedProcedureToRun)
: base(storedProcedureToRun)
{
}
}
public class QrDataGrabber : QrDataCommon
{
public QrDataGrabber(string storedProcedureToRun)
: base(storedProcedureToRun)
{
}
}
}
The error is shown on this line:
public class QrDataGrabber : QrDataCommon
Warning CS0436
The type 'QrDataCommon' in 'D:\Web\wwwroot\MyApp\MyApp-InProgress-Person
WebApi\QrCodes\App_Code\DataAccess.cs' conflicts with the imported
type 'QrDataCommon' in 'QrCodes, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null'. Using the type defined in
'D:\Web\wwwroot\MyApp\MyApp-InProgress-Person
WebApi\QrCodes\App_Code\DataAccess.cs'.
I've read many posts on here about a project referencing itself, however, there are no references in the project dialog (that are listed), only a single dependency in Project 2 to Project 1. Also, Project 2 has no controls, etc, that reference anything else.
Can anyone please give me some guidance? I'm not sure if its helpful, but in the object browser when searching for 'QrDataCommon' I see this:
Update based on help so far
If I opt to view all files within Solution Explorer, within the \bin\ directory there is a file called QrCodes.dll that gets created when the project/solution is built. I also see the same in the hidden Debug folder.
If I rename the QrDataCommon class to something totally unique, say QrDataCommonTest123, and clean/rebuild, the error immediately updates to use the new class name.
Therefore, might this be something to do with the application build target location or something?
Warning CS0436
The type 'QrDataCommon' in '...DataAccess.cs' conflicts with the imported type 'QrDataCommon' in '...'. Using the type defined in '...DataAccess.cs'.
Most people likely run into this because of projects referencing themselves (as you pointed out); however, in your case it's because your VB project has a type with exactly the same namespace and name - a result of doing a direct 1:1 port from VB to C#.
Due to the name and namespace being identical C# has a choice to make: which one should it use? It is informing you that it is made the most logical choice and has chosen the one in your C# project - which is probably what you wanted it to do anyway.
These are your options:
Good option: Once you have completed porting a type to C# delete it from the VB project and recompile the VB project.
Good option: If you are not distributing your code as a standalone re-usable DLL (it looks like you are not), change the root namespace of your C# code.
Ignore the error until you have completed the port.
Worst option: Use a global namespace alias.
You have 2 DLLs using the same class name QrDataCommon. Rename one of them or don't reference QrCodes.dll.
I have solution with 3 projects.
SharingFilesTest have a file MainPage.xaml and the other two projects have this MainPage.xaml added as a link.
So, the issue is when I try to use any App.xaml.cs GLOBAL variable in MainPage.xaml.cs file it says "The name 'App' doesnot exist." only when I tried to Build Pro1 and Pro2. SharingFilesTest works fine
However, I have defined the same variable in all App.xaml.cs in the solution. I mean Pro1, Pro2 and SharingFilesTest all three have the same variable.
I haved tried referencing Pro1 and Pro2 in SharingFilesTest and then add Using statement for both Pro1 and Pro2. But still doesnt work as Pro1 doenst know Pro2 and vice versa.
It's usually better to share a view among multiple assemblies by moving that code into a separate class library that all your other assemblies can then reference. Linking the code file itself, as you've done in this case, is definitely not the preferred method.
That said... if you're determined to just link the exact code file, you'll need to move your global variable into a class which exists in the same namespace within each project. Your projects should therefore not reference each others' assemblies.
For example, you might add a file called Globals.cs which uses the same namespace in each project:
namespace SharedNS
{
public class Globals
{
public const int MyGlobalVar = 0;
}
}
The App.xaml.cs files, which should probably stay in the project-specific namespaces (e.g. Pro1, Pro2, SharingFilesTest) can reference the Globals object if needed.
Background: I am a novice in C#, and use Visual Studios 2010 Express.
I have a class (let's call it myclass) which I want to use in multiple projects. I used to add classes with Project->Add Existing Item... Which creates a copy of myclass.cs.
Now I just found out that when I build the original myclass.cs it creates a myclass.dll and places it in the release folder of my project.
But when I try to use this DLL, I get the following error:
The type or namespace name 'myclass' could not be found(are you
missing a using directive or an assembly refference?
Which is weird to me, because I already have referenced it (it is also in the Reference folder of my Solution Explorer). And I already have added this to my code:
using myclass;
So what am I doing wrong?
Update: When I tried my old method (add existing item -> myclass.cs) the error message goes away. So it's not a matter of spelling things correctly.
Add the dll first:
Click on references in your project-explorer in visual studio and add your dll then you can use it as you expected it.
Add the reference in your project and check that the target Framework version of that assembly fits the project.
Check the namespaces inside the assembly and then use them like:
using YourAssemblyNamespace.class
Okay so I found the answer myself. It turns out that when you use the using function, it automatically searches for all public classes in the namespace you want to use.
If it can't find a public class, it refuses to recognize the DLL.
Furthermore, not specifying a class makes it internal.
So:
class myclass // internal!
private class myclass // private!
public class myclass // only this makes it visible for others!
Everything was okay after changing class myclass into public class myclass.
I have a LINQ-to-SQL Generator class in a project I'm working on, and to conform to naming conventions, the default namespace has to be a little different than the name of the project itself.
Essentially, it has to be GroupName.ProjectName, instead of just ProjectName.
I can manually rename stuff and that's fine, except for the LINQ-to-SQL class we have in the project.
Renaming the namespace manually just causes the change to be wiped out next time the tool is regenerated. Any help on getting this refactored properly?
Nevermind. I am an idiot and thought this would be handled at the solution level and not the project level. For what it's worth, right click on your project file, click properties, and edit the "default namespace" field.