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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:12:00+00:00 2026-05-29T08:12:00+00:00

The background… Lets say I have a class called cars. We are just going

  • 0

The background…

Lets say I have a class called cars. We are just going to store the car name and an ID.
Lets also say I have an admin page based on an admin class that I set the total number of cars I want to create in an int called totalCars

The question:
How do I dynamically create cars as fields that can be accessed from anywhere in the code, while at the same time creating a total number of cars based on the number in totalCars?

example code:

      Cars car1 = new Cars();
      int totalCars;
      //Somehow I want to create cars objects/fields based on the 
      //number in the totalCars int
      protected void Page_Load(object sender, EventArgs e)
      {
          car1.Name = "Chevy";
          car1.ID = 1;

      }

      protected void Button1_Click(object sender, EventArgs e)
      {
         TextBox1.Text = car1.Name.ToString();
         //this is just a sample action.
      }
  • 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-29T08:12:00+00:00Added an answer on May 29, 2026 at 8:12 am

    This should de the trick:

    int CarCount = 100;
    Car[] Cars = Enumerable
                .Range(0, CarCount)
                .Select(i => new Car { Id = i, Name = "Chevy " + i })
                .ToArray();
    

    Regards GJ

    Edit

    If you just want to know how you would do such a thing (which you shouldn’t), try this:

    using System.IO;
    
    namespace ConsoleApplication3 {
    
        partial class Program {
    
            static void Main(string[] args) {
                Generate();
            }
    
            static void Generate() {
    
                StreamWriter sw = new StreamWriter(@"Program_Generated.cs");
                sw.WriteLine("using ConsoleApplication3;");
                sw.WriteLine("partial class Program {");
    
                string template = "\tCar car# = new Car() { Id = #, Name = \"Car #\" };";
                for (int i = 1; i <= 100; i++) {
                    sw.WriteLine(template.Replace("#", i.ToString()));
                }
    
                sw.WriteLine("}");
                sw.Flush();
                sw.Close();
            }
        }    
    
        class Car {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    }
    

    Note the keyword partial class, this mean that you can have a class that spans multiple source files. Now you can code one by hand, and generate the other.

    If you run this code it will generate this code:

    using ConsoleApplication3;
    partial class Program {
        Car car1 = new Car() { Id = 1, Name = "Car 1" };
        Car car2 = new Car() { Id = 2, Name = "Car 2" };
        ...
        Car car99 = new Car() { Id = 99, Name = "Car 99" };
        Car car100 = new Car() { Id = 100, Name = "Car 100" };
    }
    

    You can the add this code file to your solution (right click project.. add existing..) and compile it. Now you can use these variables car1 .. car100.

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

Sidebar

Related Questions

Background: I have some existing apps in the App Store and I have just
Background: I have a website where people can store transactions. As part of this
Background I have a dimension table that has a single record for each day.
Background, Part 1 I have a form that collects both frequency and duration from
Background: I have this with rollup query defined in MySQL: SELECT case TRIM(company) when
Background I have an existing extension designed to accompany a browser-based game (The extension
Background I am writing code in VS 2010, .NET 4, C#. Also, in case
Background: I have a css and a js that is used only by the
Background: Sometimes when editing in vim it is possible to have extra characters in
Background I have a ror application which is continuously recording and showing on a

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.