Good day, help figure out how to perform a loop when filling an array. Here is a class with variables:

internal class Transhipment { public float Weight { get; set; } public float WeightThin { get; set; } public float WeightThick { get; set; } public float KalN { get; set; } public float KalV { get; set; } public float ResourceShaft { get; set; } public float[] FloatReserv { get; set; } public int[] IntReserv { get; set; } } 

Next, I fill in the data from the file and put everything in the List:

  var transhipment = new Transhipment() { Weight = read.ReadSingle(), WeightThin = read.ReadSingle(), WeightThick = read.ReadSingle(), KalN = read.ReadSingle(), KalV = read.ReadSingle(), ResourceShaft = read.ReadSingle(), FloatReserv = new[] {read.ReadSingle()}, IntReserv = new[] {read.ReadInt32()} }; result.Add(transhipment); 

But I need to execute FloatReserv and IntReserv in a loop, if I write inside for I get an error, if I get out of the brackets and write:

 transhipment.FloatReserv[1] = read.ReadSingle(); 

I get the Null exception. Help to understand please. thank

  • Can you give the contents of the file? It is advisable, with comments, what is what? - klutch1991
  • Use instead of arrays List<int> and List<float> . But such a record IntReserv = new[] {read.ReadInt32()} creates an array of one element in size, and therefore an error occurs when trying to do this: transhipment.FloatReserv[1] = read.ReadSingle(); . And in addition: whenever you start creating a new array, immediately remember that you are writing in C # and it has typed collections, for example List <T>. - Bulson
  • @Bulson OK, but I need to fill in 4 times with data for 1 pass, I don’t quite understand how to do this? - Ethernets
  • There are many options, here's one of them: create SetFloatReserv(IEnumerable<float> data) methods SetFloatReserv(IEnumerable<float> data) in the class, and for the second property, similar to fill in with data - Bulson
  • one
    @Ethernets: Then FloatReserv = new[] {read.ReadSingle(), read.ReadSingle(), read.ReadSingle() } ? - VladD

2 answers 2

A simple way - if the number of numbers is known and small, you can write this:

 FloatReserv = new[] { read.ReadSingle(), read.ReadSingle(), read.ReadSingle() }, 

(for three).

If the number of elements becomes known only at runtime, then the easiest way is to write an auxiliary method:

 static float[] ReadMany(int howMuch, BinaryReader read) { var result = new float[howMuch]; for (int i = 0; i < howMuch; i++) result[i] = read.ReadSingle(); return result; } 

and correspondingly

 FloatReserv = ReadMany(3, read), 

You can still, of course, use a LINQ expression.

 FloatReserv = Enumerable.Range(0, 3).Select(n => read.ReadSingle()).ToArray(), 

but for my taste it is not very readable.

  • Thank you very much. - Ethernets
  • @Ethernets: Please! - VladD

Here you need to pass a reference to the read in the constructor of the class Transhipment. Inside the constructor, you can declare a for loop. PS: if you show the file format, I will describe in more detail.