Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 410903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:56:02+00:00 2026-05-12T17:56:02+00:00

I need a field that can be assigned to from where ever I want,

  • 0

I need a field that can be assigned to from where ever I want, but it should be possible to assign it only once (so subsequent assignments should be ignored). How can I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:56:03+00:00Added an answer on May 12, 2026 at 5:56 pm

    That would not be a readonly field then. Your only options for initializing real readonly fields are field initializer and constructor.

    You could however implement a kind of readonly functionality using properties. Make your field as properties. Implement a “freeze instance” method that flipped a flag stating that no more updates to the readonly parts are allowed. Have your setters check this flag.

    Keep in mind that you’re giving up a compile time check for a runtime check. The compiler will tell you if you try to assign a value to a readonly field from anywhere but the declaration/constructor. With the code below you’ll get an exception (or you could ignore the update – neither of which are optimal IMO).

    EDIT: to avoid repeating the check you can encapsulate the readonly feature in a class.

    Revised implementation could look something like this:

    class ReadOnlyField<T> {
        public T Value {
            get { return _Value; }
            set { 
                if (Frozen) throw new InvalidOperationException();
                _Value = value;
            }
        }
        private T _Value;
    
        private bool Frozen;
    
        public void Freeze() {
            Frozen = true;
        }
    }
    
    
    class Foo {
        public readonly ReadOnlyField<int> FakeReadOnly = new ReadOnlyField<int>();
    
        // forward to allow freeze of multiple fields
        public void Freeze() {
            FakeReadOnly.Freeze();
        }
    }
    

    Then your code can do something like

            var f = new Foo();
    
            f.FakeReadOnly.Value = 42;
    
            f.Freeze();
    
            f.FakeReadOnly.Value = 1337;
    

    The last statement will throw an exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database table with a field that I need to read from
I need a SQLite query that searches 1 field only using LIKE. Basic example:
I have a field that may be blank, so I need to use Object.try,
I'm working with Sharepoint 2.0 (WSS2) and need to create a lookup field that
I need to ensure that a given field does not have more than one
I need a script that will test an input field's contents to see if
I need a regex that allow user to insert a password in a field.
I've got the entity that contains @version field. Sometimes I need to update it
There is a webpage online that has a vast field of data I need
Given a table in MySQL with rows that have a 'Country' field, I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.