readonly

How to get around lack of covariance with IReadOnlyDictionary?

落花浮王杯 提交于 2019-11-27 09:01:54
I'm trying to expose a read-only dictionary that holds objects with a read-only interface. Internally, the dictionary is write-able, and so are the objects within (see below example code). My problem is that IReadOnlyDictionary doesn't support covariant conversions because of the reason outlined in the question here . This means I can't just expose my internal dictionary as a read only one. So my question is, is there an efficient way to convert my internal dictionary to an IReadOnlyDictionary, or some other way to handle this? The options I can think of are: Hold two internal dictionaries and

What is the correct readonly attribute syntax for input text elements?

主宰稳场 提交于 2019-11-27 08:48:16
As Most, I am familiar with the readonly attribute for text input , But while reading code from other websites (a nasty habit of mine ) I saw more than one implementation for this attribute: <input type="text" value="myvalue" class="class anotherclass" readonly > and <input type="text" value="myvalue" class="class anotherclass" readonly="readonly" > and I have even seen <input type="text" value="myvalue" class="class anotherclass" readonly="true" > .. And I believe I saw even more, but can not recall the exact syntax now.. So, which one is the correct one that I should use? Vucko From w3 :

How should I use properties when dealing with read-only List<T> members

筅森魡賤 提交于 2019-11-27 07:54:54
When I want to make a value type read-only outside of my class I do this: public class myClassInt { private int m_i; public int i { get { return m_i; } } public myClassInt(int i) { m_i = i; } } What can I do to make a List<T> type readonly (so they can't add/remove elements to/from it) outside of my class? Now I just declare it public: public class myClassList { public List<int> li; public myClassList() { li = new List<int>(); li.Add(1); li.Add(2); li.Add(3); } } JP Alioto You can expose it AsReadOnly . That is, return a read-only IList<T> wrapper. For example ... public ReadOnlyCollection<int

Readonly ComboBox in WinForms

点点圈 提交于 2019-11-27 07:34:36
问题 I'm writing a GUI in C#, Visual Studio 2008, using the Designer and WinForms. I've got a ComboBox control, and I'd like it to only allow to select from the provided options and not to accept a user-entered string. It doesn't appear to have a ReadOnly property, and disabling it hinders the readability of the control (as well as disallowing user-selection). 回答1: Set DropDownStyle to "DropDownList" 回答2: Set the ComboBox.DropDownStyle property to ComboBoxStyle.DropDownList. 回答3: Another simple

Remove readonly attribute from directory

你离开我真会死。 提交于 2019-11-27 07:24:12
How can I programatically remove the readonly attribute from a directory in C#? var di = new DirectoryInfo("SomeFolder"); di.Attributes &= ~FileAttributes.ReadOnly; Here's a good link to examples of modifying file attributes using c# http://www.csharp-examples.net/file-attributes/ based on their example, you can remove the Read Only attribute like this (I haven't tested this): File.SetAttributes(filePath, File.GetAttributes(filePath) & ~FileAttributes.ReadOnly); Using the -= assignment operator is dangerous for two reasons: 1) It works ONLY IF the ReadOnly attribute is set, thus a test is

Python read-only property

蹲街弑〆低调 提交于 2019-11-27 06:56:53
I don't know when attribute should be private and if I should use property. I read recently that setters and getters are not pythonic and I should use property decorator. It's ok. But what if I have attribute, that mustn't be set from outside of class but can be read (read-only attribute). Should this attribute be private, and by private I mean with underscore, like that self._x ? If yes then how can I read it without using getter? Only method I know right now is to write @property def x(self): return self._x That way I can read attribute by obj.x but I can't set it obj.x = 1 so it's fine. But

How to make Entity Framework Data Context Readonly

六眼飞鱼酱① 提交于 2019-11-27 06:04:24
I need to expose an Entity Framework Data Context to 3rd party plugins. The purpose is to allow these plugins to fetch data only and not to let them issue inserts, updates or deletes or any other database modification commands. Hence how can I make a data context or entity readonly. In addition to connecting with a read-only user, there are a few other things you can do to your DbContext. public class MyReadOnlyContext : DbContext { // Use ReadOnlyConnectionString from App/Web.config public MyContext() : base("Name=ReadOnlyConnectionString") { } // Don't expose Add(), Remove(), etc. public

C# and immutability and readonly fields… a lie?

一世执手 提交于 2019-11-27 05:35:29
问题 I have found that People claim that using all readonly fields in a class does not necessarily make that class's instance immutable because there are "ways" to change the readonly field values even after initialization (construction). How? What ways? So my question is when can we really have a "real" immutable object in C#, that I can safely use in threading? Also do anonymous types create immutable objects? And some say LINQ uses immutable objecst internally. How exactly? 回答1: You've asked

How to keep the Text of a Read only TextBox after PostBack()?

[亡魂溺海] 提交于 2019-11-27 05:33:15
问题 I have an ASP.NET TextBox and I want it to be ReadOnly . (The user modify it using another control) But when there is a PostBack() , The text get reset to an empty string. I understand that if you set the ReadOnly property to True of a TextBox it's content does not get saved through PostBack() . Is there a way to keep the content after PostBack() and make the TextBox not editable by the user? I tried to set the Enabled property to False ,But still the content doesn't save after PostBack() .

How can I enable jquery validation on readonly fields?

风格不统一 提交于 2019-11-27 04:48:31
Guys from http://jqueryvalidation.org/ just released version 1.13.1. Checking on their website i see this on the changelog: CORE: * Ignore readonly as well as disabled fields. (9f4ba10) This is the link: https://github.com/jzaefferer/jquery-validation/commit/9f4ba10ea79b4cf59225468d6ec29911f0e53a0a I use some bootstrap templates and one of them now uses that version. That brings me a problem, i use readonly attribute to prevent users typing dates manually, so their only option is to choose a date from the datepicker. With this change, the validation plugin ignores the readonly inputs marked as