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

  • Home
  • SEARCH
  • 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 933817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:53:41+00:00 2026-05-15T20:53:41+00:00

How to define a struct whose members can be accessed by any internal classes

  • 0

How to define a struct whose members can be accessed by any internal classes via properties but can only be set by a particular internal class? (If it is possible.)

If it is not possible, is there a work-about to achieve the similar requirement?

Edit 1: below

Reason:

I have a class A with two members _x and _arrB. _x can be read but not modified by internal classes (except class A) through x.

I also want to allow internal classes to read structB elements in _arrB and to read members of those structB. But I only want class A to be able to modify either a structB element in _arrB or a member of those structB. Hence I think I need a struct that can specify which internal classes can modify its member.[1]

class A 
{
    private int _x;
    internal int x
    {
        private set
        {
            _x = value / 2;
        }
        internal get
        {
            return _x * 2;
        }
    }

    private B[] _arrB;
    internal B[] arrB
    { 
        private set
        {
            _arrB = value;
        }
        internal get
        {
            return _arrB;
        }
    }

    public A() 
    {
        _x = //value read from somewhere;            

        //do something to determine size
        _arrB = new B[size];
        //populate _arrB
        ...
    }


}

Note: By internal class I mean a class in the same assembly, not a nested class.

[1] However, _arrB is an array, which is an object, which is a reference type. When an internal class gets a reference of _arrB, elements of structB may be modified without control. I realized that I do not want inner classes to set _arr[0] to null. So any solution have to take this into account.

I will think about it for the rest of the day and post a solution when I have thought of one.

  • 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-15T20:53:42+00:00Added an answer on May 15, 2026 at 8:53 pm

    I wonder why you want to do that. Only ugly hacks are possible AFAIK.

    BEGIN WARNING: do not read if you dislike ugly code!!!

    What David Neale said.

    You could put them in separate assemblies and set them to friendly:

    [assembly:InternalsVisibleToAttribute("assembly name", "key")]
    

    You could design the class like this:

    internal struct ModifiedStruct
    {
        public int Value {get; private set; }
    
        internal class ModifyingClass
        {
            public void ModifyValue()
            {
                ModifiedStruct s = new ModifiedStruct();
                s.Value = 456;
            }
        }
    
    }
    

    END WARNING

    Edit (question changed)
    To prohibit modification of a collection, use the ReadOnlyCollection Class.

    internal ReadOnlyCollection<B> BCollection
    {
        get
        {
            return new ReadOnlyCollection<B>(_arrB);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you define a macro that accesses a normal variable, but in a read-only
If I define a struct in C# using automatic properties like this: public struct
I have a few classes that define sequences whose values must be available both
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
(define a 42) (set! 'a 10) (define a 42) (define (symbol) 'a) (set! (symbol)
I am looking to define a template class whose template parameter will always be
I have a struct for which i want to define a relative order by
I define this structure: struct s_molecule { std::string res_name; std::vector<t_particle> my_particles; std::vector<t_bond> my_bonds; std::vector<t_angle>
I have a lot of code like this: #define WITH_FEATURE_X struct A { #ifdef
I have a struct defined like follows as part of an object. I'm trying

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.