-1

I have a class with just one variable

public class C
{
   int i;
}

And in another project file I create an array of classes

C[] classes = new C[100000];

So what i need to do to set some random value to the "i" variable in each class?

StarLight
  • 59
  • 7
  • 2
    Er.. You can't. The field is private, so if the C class itself doesn't set it, nothing* can and it will always have its default value of 0. Consider making it a public property with a much more interesting and useful, self descriptive name, and then using a `new Random()` outside a for loop that repeatedly does `c[someindexvariable].ReallyGoodPropertyName = random.Next(lowestValueYouWant, oneMoreThanHighestValueYouWant)` – Caius Jard Dec 16 '21 at 19:05
  • Why even use a class at all? You have the primitive type `int` and could just use an array of `int`? – Ryan Wilson Dec 16 '21 at 19:06
  • I have an university work where this "i" variable must be in the class according to exercise @RyanWilson – StarLight Dec 16 '21 at 19:11
  • Okay, thank you, i'll try @CaiusJard – StarLight Dec 16 '21 at 19:13
  • If it has to be i just like that you'll have to(==I would recommend to) pass the random number you want to init i with, to a constructor that sets it – Caius Jard Dec 16 '21 at 19:41

4 Answers4

1

First you need to make C.i accessible. One way is to make C.i a public property. While you’re at it, public fields should be pascal cased and all identifiers should have meaningful names.

When naming public members of types, such as fields, properties, events, methods, and local functions, use pascal casing.

public class Foo {
    public int Bar { get; set; }
}

Then you'd use System.Random. Instantiate it once and call Random.Next each time you want a random number.

using System;
var rand = new Random();

// int anyPositiveInt = rand.Next();
// int positiveIntLessThanFifteen = rand.Next(15);
// int intFromOneToFour = rand.Next(1, 5);

Finally, following the example in Creating N objects and adding them to a list, use System.Linq's Enumerable.Range, Enumerable.Select, and Enumerable.ToArray as follows:

Foo[] classes = Enumerable
    .Range(0, 100000)
    .Select(_ => new Foo { Bar = rand.Next() }) 
    .ToArray();
D M
  • 5,769
  • 4
  • 12
  • 27
1

If the requirement is to use a private field then I recant the earlier advice to make it a public property - properties might not have been taught yet

static void Main()
{
    var r = new Random();
    
    var maxValueOfI = 100;
    var minValueOfI = -20;

    var csArr = new C[100000];

    for (var julius = 0; julius < csArr.Length; julius++) {
        var brutus = r.Next(minValueOfI, maxValueOfI+1);
        csArr[julius] = new C(brutus);
    }
}

public class C
{
    private int _i;

    public C(int i){
      _i = i;
    }
}

So, what's going on here?

The main addition is a constructor to C. A constructor is a special method that is called by C# when a new object is constructed. Every class has one even if you can't see it (the compiler provides one if you don't). Constructors are methods that are intended to ensure the class is fully set up and ready to use. Because it's inside the class it has full access to all the data fields of the class:

public C(int i){
  _i = i;
}

This constructor takes an int, and sets the private field _i to the value of the passed in number. It's quite common to use this naming pattern for fields (prefix with underscore) and it helps avoid a name collision with the arguments to the method (in this case called i). If they had both been called i the class one would have to be prefixed with this. and it's (IMHO) more clutter

This line of code calls the constructor:

csArr[julius] = new C(brutus);

We've previously calculated a random number between -30 and 100 (inclusive both ends) and stashed it in a variable called brutus. This number is passed to the constructor, which is called when we say new C. The resulting fully constructed C instance is stored in one of the array slots

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

try this

static void Main()
{
    Random rand = new Random();
    
    var max=100000;

    C[] array = new C[max];

    for (var i=0; i <max; i++)
        array[i] = new C { Num = rand.Next(0, max)}; 
       
      // or using a constructor
        array[i] = new C (rand.Next(0, max)); 
}

public class C
{
    public int Num {get; set;}

   public C (int num)
   {
      Num=num;
   }

 public C (){}

}
Serge
  • 40,935
  • 4
  • 18
  • 45
0

First of all, in your current code i is a private field. Let's add a constructor to set this field:

public class C {
  int i;

  public C(int value) {
    i = value; 
  }
}

Then you can try using Linq:

using System.Linq;

...

Random rand = new Random();

...

C[] classes = Enumerable
  .Range(0, 100000)
  .Select(i => new C(rand.Next(0, 100))) //TODO: Put the right range here
  .ToArray();

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215