Alex Rangerisng
Alex Rangerisng

Reputation: 17

Is it possible to create loop which create instance of classes?

I want to create console app which read userinput value and create multiple instances of one class and then run void say() in all class instances. Code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int botsnumber = 0;
            Console.WriteLine("Type number: ");
            botsnumber = Int32.Parse(Console.ReadLine());
            for (int i = 0; i < botsnumber; i++)
            {
                //Here generate bots: Bot b1 = new Bot(); Bot b2 = new Bot(); 
               // Bot b = new Bot(); 
            }
            b.say();// b1.say(); b2.say(); b3.say(); ...
        }
    }

    class Bot
    {
        public static void say()
        {
            Console.WriteLine("Hello!");
        }
    }
}

Upvotes: 0

Views: 80

Answers (4)

Marc Gravell
Marc Gravell

Reputation: 1062600

I think you want to call say() inside the loop, and make it a non-static method:

for (int i = 0; i < botsnumber; i++)
{
     Bot b = new Bot();
     b.Say();
}

class Bot
{
    public void Say()
    {
        Console.WriteLine("Hello!");
    }
}

Although note that in many ways there's no point in creating a stateless object instance that you immediately discard, so ... in real usage I would either expect some state, or I'd expect it to be a static method.

Alternatively, you could create all the bots first, accumulate them in a list, then loop over that - again, whether this is sensible depends on context, but:

var bots = new List<Bot>(botsnumber);
for (int i = 0; i < botsnumber; i++)
{
     bots.Add(new Bot());
}
// ...
foreach(var bot in bots)
{
    bot.Say();
}

Upvotes: 1

ikkentim
ikkentim

Reputation: 1648

You could either immediately call say, or call them all after creating all the bots

for (int i = 0; i < botsnumber; i++)
{
    //Here generate bots: Bot b1 = new Bot(); Bot b2 = new Bot(); 
    Bot b = new Bot(); 
    b.say();
}

// or

var bots = new List<Bot>();
for (int i = 0; i < botsnumber; i++)
{
    //Here generate bots: Bot b1 = new Bot(); Bot b2 = new Bot(); 
    bots.Add(new Bot());
}
foreach (var bot in bots)
{
    bot.say();
}

Upvotes: 0

TheGeneral
TheGeneral

Reputation: 81493

Like this i guess

public static List<Bot> bots = new List<Bot>();

...

for (int i = 0; i < botsnumber; i++)
{
    bots.Add(new Bot);
}

...

bots[0].Say();
bots[1].Say();

Upvotes: 1

Antoine V
Antoine V

Reputation: 7204

Put the instances into a list, fill it, after that, loop the list and call say

botsnumber = Int32.Parse(Console.ReadLine());
var list = new List<Bot>();
for (int i = 0; i < botsnumber; i++)
{
   Bot b = new Bot(); 
   list.Add(b);
}
list.ForEach(b => b.say());

And you don't need to put static in your method say

class Bot
{
    public void say()
    {
        Console.WriteLine("Hello!");
    }
}

Upvotes: 1

Related Questions