Inaccessible, Internal, Resource Files? - c#

I am building an ASP.NET MVC 4 website with C#. I am localizing all of my static strings in a Resource File (.resx). I use the Visual Studio interface for creating and populating the resource file. In my index.cshtml view, I try to use one of the strings from the resource file, like this:
#using Namespace.To.RESX.File
...
<h1>#EN_US.GLOBAL_NAME</h1>
<h2>“#EN_US.GLOBAL_SLOGAN”</h2>
...
I can compile the application, but I get a runtime error when I load the page, saying:
Compiler Error Message: CS0122: 'Namespace.To.RESX.File.EN_US' is inaccessible due to its protection level
Looking in the resx designer file, I can see the class and all of its members are marked as internal. I completely understand what the C# access control modifiers do, how they work, etc... but I don't understand why Visual Studio would restrict access to the resource members like this.
Why would Visual Studio do this? Is there an (easy) way to change these access control modifiers, or am I missing the point of resource files completely?

Yep... didn't see this. Open up the resource file in design mode and change the access modifier (at least, on VS 2013):

For the Mac / visual studio 2017 guys , Open .Designer file and make your classes public :
Folder:
Change internal to public and save:
Update: It may Default back to internal. everytime you make changes which you want to make this change listed here
Right click on Resource file ( resource.resx ) => Properties.
Custom Tool => Change to PublicResXFileCodeGenerator

I don't know if it's a bug in VS 2019 or what, but going by Oliver Spryn's answer, I could expand that Access Modifier dropdown box which was "Internal" by default, but clicking "Public" just closed the dropdown onMouseDown without selecting an option. So, if you click to dropdown the list, but then use your keyboard down arrow to select "Public" and hit enter, it will change the value.

Related

Adding external Folder from another Project

I am probably facing a basic problem. I would like to create TimePickers as in this project link: TimePickerProject
This project was given as reference in one of the Stack overflow answers Stack OverflowAnswer
You can download and directly run that project really easy. But couldn't add it to my own project as an independent TimerPicker Object. TimePicker class is inside Opulos folder. As you can see Opulos folder is copy pasted to my own project.
I
And I can see that Timepicker inside my toolbox.
But when I try to drag it to my panel I am facing with this error.
To explain this process more clearly:
Just paste the Opulos folder into the root directory of your program. (I am here. Net framework 4.8 winform)
Edit Opulos/Core/UI/TimePicker.cs
Add the below codes:
public TimePicker() :this(3, true, true, true, true) {
}
Then rebuild the program, you could find it in the toolbox.
Output:
Thanks to jimi's explanation:
Pass default values or configure it to generate a different pre-defined behavior when the class is created via ToolBox.
Keep in mind that those classes are not built with the ToolBox in mind (no reference to ToolBox behavior is specified anywhere), it looks like all are meant to be instantiated in code.
I.e., you may have the same problem somewhere else.
You should know what to do (though decorating the public Control classes with appropriate [ToolboxItem], [ToolboxBitmap] etc. stuff should be considered)
If you are trying to drag another folder into your solution folder, then you can use the default windows file explorer. If you want to import a library from another project, simply right click Dependencies->add project reference, browse, and select the library. In general, you should avoid copying the code itself, and instead add a reference to the output.
Hope I could help, if I misunderstood your question I apologize!

c# Winforms share resources between projects

I have a solution that contains 2 winforms app and a classlibrary referenced by the 2 apps... I added some images in resource
but when I go on the winform app and try to add the image into a button appears this:
Is there a way to do this?
thanks
You have to make resource items public instead internal.
To do this, in .resx file in solution explorer select Property Window (Alt+Enter) and in File Properties -> Custom Tool should be ResXFileCodeGenerator
Change this option to PublicResXFileCodeGenerator (if option don't show in dropdown field, don't worry aboit it, write/type PublicResXFileCodeGenerator in field)
After that resources member class will be generated with public modifier and you can access to it from any project that reference assembly.
I hope it can help you!

Assigning an image to the composite custom control c# VS .NET 2013

The problem concerns Windows Forms.
I have no idea how to do it. I've tried following:
1) Add a Bitmap (CarControl.bmp) do the solution, then set BuildAction to Embedded Resource and add the ToolBoxBitmapAttribute like:
[ToolboxBitmap(typeof(CarControl),"CarControl")]
2) Going into Properties, then in Resources section I've added some images. Still doesn't work.
Can anybody help?
Naming is very important, otherwise it does not work.
[ToolboxBitmap(typeof(CarControl), "Resources.CarControl.png")]
Another important note:
If you open the solution of your CarControl you will always see the default gear icons for the controls of your actual build. But if you make a build and you add the controls of this (released) library to the Toolbox either by drag-and-dropping the dll or by Right click/Choose Items..., then the icon will appear.
If you did everything well, you will able to find the embedded resource in the compiled .dll when you open it with Reflector or other disassembler tool. The following example is from System.dll:
You should find a <DefaultNamespace of your project>.<Your resources folder>.CarControl.png here.

Creating default set-ups for commonly used code in Visual Studio 2010?

This is a pretty minor annoyance, but I noticed that every time I create a class in Visual Studio there are a few things that I would like to be automatically-generated for me. For instance, all of my classes start with a log4net declaration. It's only one line of code, but it's annoying having to find a class where I declared it in to copy/paste from.
In addition, there are multiple common 'usings' which I use, but that are not created by default.
Is it possible to setup VS to do this?
You just need to edit the default Visual Studio Class template. A walkthrough of the process can be found here.
From the File menu, select "Export Template".
You can create your own item/project templates. Here is a primer from MSDN:
http://msdn.microsoft.com/en-us/library/6db0hwky.aspx
Basically you'd create your own custom class template.
From MSDN:
Creating Project and Item Templates
How to: Manually Create Item Templates:
Create a project and project item.
Modify the project item until it is ready to be saved as a template.
As appropriate, modify the code file to indicate where parameter replacement should occur. For more information about parameter replacement, see How to: Substitute Parameters in a Template.
Create an XML file and save it by using a .vstemplate file name extension, in the same directory as your new item template.
Author the .vstemplate XML file to provide item template metadata. For more information, see the example in the following section.
Save the .vstemplate file and close it.
In Windows Explorer, select the files you want to include in your template, right-click the selection, click Send To, and then click Compressed (zipped) Folder. The files that you selected are compressed into a .zip file.
Copy the .zip file and paste it in the user item template location. In Windows Vista, the default directory is ..\Users\\Documents\Visual Studio 2010\Templates\ItemTemplates. For more information, see How to: Locate and Organize Project and Item Templates.
Although not exactly what you are asking for, one option is to declare code snippets. This will allow you to leave the standard class alone (not change the template) and add in the pieces that you require without having to find a class with it. However, if all your classes for every project you create requires the same basic set of code then Templates are the way to go.
C# Code Snippets
How to use them can be found How to Use Snippets

Any way to make Intellisense work, when opening a cs file that's not part of the project?

What we ideally need is, to know how Microsoft handles XAML generated code (Those *.g.cs files). If you goto a XAML code behind, intellisense will work even if the *.g.cs file is not part of the project!!
Context:
In a custom VS package, we need to have some logic to open a CS file (with out making it a part of the project) in the VS IDE, to allow the user to edit it.
We are hooking up the document to the Running document table and receiving the events like Saving, Close and all, using IVSRunningDocumentTable. Works good.
Now the problem is, when the document is opened, Intellisense can't work, for the simple reason that the opened document is not part of the project (sadly, we can't do that, we can't make it code behind).
Intellisense is driven by a memory cache of identifiers and types. These types are cached based on the project you are in and the references that project has. If the code file you are editing is not part of a project, Visual Studio would have to load every possible assembly and create intellisense data for each type in the entire .NET framework because it would have no way of knowing whether or not your code file required it.
I guess Visual Studio could load intellisense based on the content of the file but that is not the way it currently works.
Visual Assist X by Whole Tomato is an addin to VS I've been using for many years. It will give you Intellisense and more when you open it.

Categories