readonly

What is the difference between const and readonly?

What is the difference between const and readonly and do you use one over the other? ...

Readonly ComboBox in WinForms

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 disallow...

Html.TextBox conditional attribute with ASP.NET MVC Preview 5

I have a strongly-typed MVC View Control which is responsible for the UI where users can create and edit Client items. I'd like them to be able to define the ClientId on creation, but not edit, and this to be reflected in the UI. To this end, I have the following line: <%= Html.TextBox("Client.ClientId", ViewData.Model.ClientId, new {...

What is the best way to implement a property that is readonly to the public, but writable to inheritors?

If I have a property that I want to let inheritors write to, but keep readonly externally, what is the preferred way to implement this? I usually go with something like this: private object m_myProp; public object MyProp { get { return m_myProp; } } protected void SetMyProp(object value) { m_myProp = value; } Is there a better...

What are the benefits to marking a field as `readonly` in C#?

What are the benefits of having a member variable declared as read only? Is it just protecting against someone changing during the lifecycle of the class or are there any compiler speed improvements due to this keyword ...

WPF UserControl expose ActualWidth

How do I expose the ActaulWidth property of one of the components of my user control to users? I have found plenty of examples of how to expose a normal property by creating a new dependency property and binding, but none on how to expose a read-only property like ActualWidth. cheers. ...

In a django form, How to make a field readonly (or disabled) so that it cannot be edited?

In a django form, How to make a field readonly (or disabled) so that it cannot be edited? When the form is being used for a new record entry, all fields are enabled, but when the record is in update mode some fields need to be readonly. For example, in the Item model, for a new record entry, all fields are editable, but while updating ...

How to implement a readonly (immutable) object interface in C#

What I need is to make sure that in most scenarios objects are used via "readonly interface", which is a subset of the full interface. If I were in C++, I would just return a const object, for instance. If I could use interfaces, I would just implement a readonly interface and use it everywhere, however, I need operator overloading, wh...

Why readonly and volatile modifiers are mutually exclusive?

I have a reference-type variable that is readonly, because the reference never change, only its properties. When I tried to add the volatile modifier to it the compiled warned me that it wouldn't let both modifiers apply to the same variable. But I think I need it to be volatile because I don't want to have caching problems when reading ...

Is there a difference between private const and private readonly variables in C#?

Is there a difference between having a private const variable or a private static readonly variable in C# (other than having to assign the const a compile-time expression)? Since they are both private, there is no linking with other libraries. So would it make any difference? Can it make a performance difference for example? Interned st...

How do I clear the read-only flag from a file in Perl?

Hi, I need to clear the read-only flag of a file in my Perl program that runs on Windows. I know system("attrib -r $filename") would work, but I was wondering if there is no built-in option in Perl to do it. chmod 777, $filename doesn't seem to work. Thanks, splintor ...

Is there a way to make a region of code "read only" in visual studio?

Every so often, I'm done modifying a piece of code and I would like to "lock" or make a region of code "read only". That way, the only way I can modify the code is if I unlock it first. Is there any way to do this? ...

Changing a Read Only attribute on a file

I can't seem to change the read only flag on a file. I've tried this on Vista and XP with the same result, so I don't think it's a UAC related issue. Nothing I've done seems to work though. See the sample below. Can someone tell me what I'm doing wrong? public bool UpdateResFile(string fileName, string language, string objectName, stri...

Why isn't String.Empty a constant?

In .Net why is String.Empty read only instead of a constant? I'm just wondering if anyone knows what the reasoning was behind that decision. ...

C# - Is this declared string treated as a const?

Help me settle an argument here. Is this: SqlCommand cmd = new SqlCommand( "sql cmd", conn); treated exactly the same as this: const string s = "sql cmd"; SqlCommand cmd = new SqlCommand( s, conn); Ie. does it make a difference if I state specifically that the string s is a const. And, if it is not treated in the same way, why n...

Overriding ReadOnly Property in a subclass to make it Read/Write (VB.NET)

In C# I can have a base class with a property containing just a getter. Subclasses can then override the property and give it both a getter and a setter. This doesn't seem possible in VB.NET with properties since the property statement itself must describe whether it is ReadOnly or not. In my example below, it doesn't let me make the ...

When, if ever, should we use const?

Const is baked into the client code. Readonly isn't. But const is faster. May be only slightly though. The question is, is there ever any scenario where you should prefer const over readonly? Or to rephrase, are we not practically always better off using a readonly instead of a const (keeping in mind the above-said baking thing)? ...

"Read only" Property Accessor in C#

I have the following class: class SampleClass { private ArrayList mMyList; SampleClass() { // Initialize mMyList } public ArrayList MyList { get { return mMyList;} } } I want users to be able to get mMyList which is why i exposed the "get" via a property however i don't want changes they make to th...

How do I make a 'ReadOnly' property in LINQ to SQL?

I have a property ("IsLatest") that I have set 'Read Only' to 'True' Here's the XML: <Column Name="IsLatest" Type="System.Boolean" DbType="Bit NOT NULL" IsReadOnly="true" CanBeNull="false" /> Why does the code-generator generate a public 'get' AND 'SET' accessor? Also, is there a way to have it not generate a SET for a read-only pro...

Delphi ClientDataset Read-only

I am currently testing with: A SQLConnection which is pointed towards an IB database. A SQLDataset that has a SQLConnection field set to the one above. A DatasetProvider that has the SQLDataset in (2) as its Dataset field value. A ClientDataset, with the ProviderName field pointing to the provider in (3). I use the following meth...