readonly

How to Create a Read-Only Object Property in C#?

僤鯓⒐⒋嵵緔 提交于 2019-12-05 16:25:29
As can be seen below, the user is able to change the readonly product field/property: class Program { static void Main(string[] args) { var product = Product.Create("Orange"); var order = Order.Create(product); order.Product.Name = "Banana"; // Main method shouldn't be able to change any property of product! } } public class Order { public Order(Product product) { this.Product = product; } public readonly Product Product; public static Order Create(Product product) { return new Order (product); } } public class Product { private Product(){} public string Name { get; set; } public static

readonly class design when a non-readonly class is already in place

十年热恋 提交于 2019-12-05 13:20:39
问题 I have a class that upon construction, loads it's info from a database. The info is all modifiable, and then the developer can call Save() on it to make it Save that information back to the database. I am also creating a class that will load from the database, but won't allow any updates to it. (a read only version.) My question is, should I make a separate class and inherit, or should I just update the existing object to take a readonly parameter in the constructor, or should I make a

Text input readonly attribute not recognized in IE7?

自古美人都是妖i 提交于 2019-12-05 11:20:27
I am setting readonly="readonly" (in other words, true) via javascript: document.getElementById("my_id").setAttribute("readonly", "readonly"); This is having the intended effect (making the field no longer editable, but its contents are submitted with the form) in FF, Safari and Chrome, but not for IE7. In IE7 I can still modify the contents of the text input field. I have tried setting ("readonly", "true") as well, which works in all three other browsers that I am testing, but which IE7 also ignores. Does anyone have experience with trying to do this with IE7? I do not want to use the

My project in Visual Studio is Read Only. What did I do?

人盡茶涼 提交于 2019-12-05 09:34:24
问题 I must have done something wrong. I have a C# project in Visual Studio 2008. All of a sudden I see a lock on my classes and when I hover the class names on the top tab I see the class name as : C:\Myprojects\Oder.cs[Read Only] ! Has something weird like that happened to you before? 回答1: A couple of possibilities spring to mind: Have you added the project to source control? Have you marked the project folder (and all it's sub folders) as read only? Of course, someone else may have done either

How to make bootstrap datepicker readonly?

被刻印的时光 ゝ 提交于 2019-12-05 07:32:09
I'm struggling with creating embedded/inline datepicker which will not be clickable - it should only present dates, behave as readonly. What I'm doing is populating calendar with selected dates from model, then I try to make it un-clickable so user doesn't thin he can edit anything. I'm using eternicode-bootstrap-datepicker - to make it multidate. So far I've got this: <link rel="stylesheet" href="<c:url value="/resources/js/lib/eternicode-bootstrap-datepicker/css/datepicker3.css" />" /> <script src="<c:url value="/resources/js/lib/eternicode-bootstrap-datepicker/js/bootstrap-datepicker.js"/>"

Const correctness in C# with rich types

我的未来我决定 提交于 2019-12-05 03:21:35
Coming from a C++ background and trying to learn C#, one of the most frustrating language omissions that I've come across is an equivalent to the const keyword. So, I have been attempting to settle on a pattern that I can use to achieve const correctness in C#. This answer has an interesting suggestion: create a read only interface for all of your types. But as Matt Cruikshank pointed out in the comments, this becomes problematic if your class has collections or other rich types. Particularly if you do not have control over the type, and can't make it implement a read-only interface. Do any

Do not declare read only mutable reference types - why not?

偶尔善良 提交于 2019-12-05 00:15:06
I have been reading this question and a few other answers and whilst I get the difference between changing the reference and changing the state of the current instance I'm not certain why this means that I shouldn't mark it readonly. Is this because marking something as readonly tells the compiler something special about the instance and so it is able to then treat it as thread safe when it actually might not be? Presumably there are situations where I don't want the instance to be able to be changed, but don't mind if the state of the instance is changed (singleton maybe. /me prepares for

C#: Why do mutations on readonly structs not break?

≡放荡痞女 提交于 2019-12-04 23:54:02
In C#, if you have a struct like so: struct Counter { private int _count; public int Value { get { return _count; } } public int Increment() { return ++_count; } } And you have a program like so: static readonly Counter counter = new Counter(); static void Main() { // print the new value from the increment function Console.WriteLine(counter.Increment()); // print off the value stored in the item Console.WriteLine(counter.Value); } The output of the program will be: 1 0 This seems completely wrong. I would either expect the output to be two 1s (as it is if Counter is a class or if struct

How can I make a Django model read-only?

穿精又带淫゛_ 提交于 2019-12-04 16:18:35
问题 Is it possible to make a Django model read only? No creating, updating etc. N.B. this question is different to: Make a Django model read-only? (this question allows creation of new records) Whole model as read-only (only concerns the Django admin interface - I'd like the model to be read only throughout the whole app) 回答1: Override the save and delete methods for the model. How are you planning to add objects to your model? def save(self, *args, **kwargs): return def delete(self, *args, *

Make a foreign key field in a django form read-only, and still enable the form to be submitted

ε祈祈猫儿з 提交于 2019-12-04 13:52:15
How do I make a foreign key field in a form read only but still allow this field to be recognized as valid once the form is submitted? According to W3C, disabled fields are left out once the form is submitted....using the code below, I can set the field as disabled, thus readonly, but my form doesn't go through def __init__(self, *args, **kwargs): super(IssuesForm, self).__init__(*args, **kwargs) self.fields['vehicle'].widget.attrs['readonly'] = True Ideas....? I don't know the Django or Python syntax, however, an input field of type="hidden" might be what you're looking for. If you wanted to