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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:05:55+00:00 2026-05-12T07:05:55+00:00

my problem is as follows: Im building a console application which asks the user

  • 0

my problem is as follows:

Im building a console application which asks the user for the numbers of objects it should create and 4 variables that have to be assigned for every object.
The new objects name should contain a counting number starting from 1.

How would you solve this?
Im thinking about a class but im unsure about how to create the objects in runtime from userinput. Is a loop the best way to go?

What kind of class, struct, list, array …. would you recommend. The variables in the object are always the same type but i need to name them properly so I can effectivly write methods to perform operations on them in a later phase of the program.

Im just learning the language and I would be very thankful for a advice on how to approach my problem.

  • 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-12T07:05:56+00:00Added an answer on May 12, 2026 at 7:05 am

    If I understand your problem correctly:

    class MyClass
    {
        public int ObjectNumber { get; set; }
        public string SomeVariable { get; set; }
        public string AnotherVariable { get; set; }
    }
    
    // You should use keyboard input value for this 
    int objectsToCreate = 10;
    
    // Create an array to hold all your objects
    MyClass[] myObjects = new MyClass[objectsToCreate];
    
    for (int i = 0; i < objectsToCreate; i++)
    {
        // Instantiate a new object, set it's number and
        // some other properties
        myObjects[i] = new MyClass()
        {
            ObjectNumber = i + 1,
            SomeVariable = "SomeValue",
            AnotherVariable = "AnotherValue"
        };
    }
    

    This doesn’t quite do what you described. Add in keyboard input and stuff 🙂 Most of this code needs to be in some kind of Main method to actually run, etc.

    In this case, I’ve chosen a class to hold your 4 variables. I have only implemented 3 though, and I’ve implemented them as properties, rather than fields. I’m not sure this is necessary for your assignment, but it is generally a good habit to not have publically accessible fields, and I don’t want to be the one to teach you bad habits. See auto-implemented properties.

    You mentioned a struct, which would be an option as well, depending on what you want to store in it. Generally though, a class would be a safer bet.

    A loop would indeed be the way to go to initialize your objects. In this case, a for loop is most practical. It starts counting at 0, because we’re putting the objects in an array, and array indexes in C# always start at 0. This means you have to use i + 1 to assign to the object number, or the objects would be numbered 0 – 9, just like their indexes in the array.

    I’m initializing the objects using object initializer syntax, which is new in C# 3.0.
    The old fashioned way would be to assign them one by one:

    myObjects[i] = new MyClass();
    myObjects[i].ObjectNumber = i + 1;
    myObjects[i].SomeVariable = "SomeValue";
    

    Alternatively, you could define a constructor for MyClass that takes 3 parameters.

    One last thing: some people here posted answers which use a generic List (List<MyClass>) instead of an array. This will work fine, but in my example I chose to use the most basic form you could use. A List does not have a fixed size, unlike an array (notice how I initialized the array). Lists are great if you want to add more items later, or if you have no idea beforehand how many items you will need to store. However, in this case, we have the keyboard input, so we know exactly how many items we’ll have. Thus: array. It will implicitly tell whoever is reading your code, that you do not intend to add more items later.

    I hope this answered some questions, and raised some new ones. See just how deep the rabbit hole goes 😛

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

Sidebar

Ask A Question

Stats

  • Questions 389k
  • Answers 389k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer public static List<string> GetImportedFileList(){ List<string> ImportedFiles = new List<string>(); using… May 15, 2026 at 12:46 am
  • Editorial Team
    Editorial Team added an answer They are close in execution, but they come from different… May 15, 2026 at 12:46 am
  • Editorial Team
    Editorial Team added an answer theVar = { "100": [0, 1, 2], "101": [3, 4,… May 15, 2026 at 12:46 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.