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 8563435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:57:34+00:00 2026-06-11T16:57:34+00:00

If a class contains just string array variables, is there an easy way to

  • 0

If a class contains just string array variables, is there an easy way to initialize them all without having to type the same code over and over?

For example, if I have something like

[Serializable]
public class ReadDiagnosticEntirePointValuesResponse
{
    public string[] pointidentifier;
    public string[] presentvalue;
    public string[] priorityarray;
    public string[] alarmstate;
    public string[] outofservice;
    public string[] correctvalue;
    public string[] affect;
    public string[] covenable;
    public string[] covincrement;
    public string[] covtarget;
    public string[] covlifetime;
    public string[] historyenable;
    public string[] historyincrement;
    public string[] elapsedactivetime;
    public string[] feedbackvalue;
    public string[] rawvalue;
    ...//A lot more 
}

and I want to assign values to to them, I want to avoid doing:

        ReadDiagnosticEntirePointValuesResponse response = new ReadDiagnosticEntirePointValuesResponse();


        response.affect = new string[count];
        response.alarmstate = new string[count];
        response.correctvalue = new string[count];
        response.covenable = new string[count];
        response.covincrement = new string[count];
        response.covlifetime = new string[count];
        response.covtarget = new string[count];
        response.elapsedactivetime = new string[count];
        response.feedbackvalue = new string[count];
        response.historyenable = new string[count];
        response.historyincrement = new string[count];
        response.outofservice = new string[count];
        response.pointidentifier = new string[count];
        response.presentvalue = new string[count];
        response.priorityarray = new string[count];
        response.rawvalue = new string[count];
        ...

Sure, I could write those initialization in constructor but that still doesn’t save me from having to manually initialize them all.

What’s a good way to avoid 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-06-11T16:57:35+00:00Added an answer on June 11, 2026 at 4:57 pm

    That is a pretty horrible way to manage your data, however, something like the following would work….

    foreach(var field in GetType().GetFields()) {
        if(!field.IsStatic) field.SetValue(this, new string[count]);
    }
    

    However! I strongly suggest you rethink this design. A better mechanism would be:

    class DiagnosticPoint // TODO: rename as appropriate
    {  // TODO: check these all need to be strings
        public string Affect {get;set;}
        public string AlarmState {get;set;}
        ...
        public string RawValue {get;set;}
    }
    

    and have an array of that as a field:

    public class ReadDiagnosticEntirePointValuesResponse
    {
        DiagnosticPoint[] points;
        ...
    

    then simply init the array:

    points = new DiagnosticPoint[count];
    

    and init each:

    for(int i = 0 ; i < count ; i++) points[i] = new DiagnosticPoint();
    

    and access via:

    var alarm = points[index].AlarmState;
    

    (etc)

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

Sidebar

Related Questions

My class contains LOT_SIZE constant which can not be changed. But I can initialize
My Java class contains the following methods: public static void main(String[] argv) //start the
Let's say I have a ClassA which contains some members of type String and
I have a custom class in Obj-C called RouteManager which contains an array of
I'm using the initializer list method for my class constructor. I initialize my variables
I have a Person class. A person class contains a collection of Friends (also
I have a class in Qt that inherits QDockWidget. And that class contains another
I have a Singleton which has an NSMutableArray containing a Class, the Class contains
I currently have 2 concrete methods in 2 abstract classes. One class contains the
I have a simple JSF web app as follows. The Java class contains: private

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.