c# - Dynamic-length array using Singleton -
okay, way outside comfort zone here , struggling new concepts hope can make myself clear.
understand it, global variables bad in c#
(and dangerous in general) don't want debate. after research led believe singletons
can help. please feel free offer alternatives here if wrong situation describe below.
trying create dynamic multi-dimensional array
contain numerical data. matrix varying in size , must created during runtime (i pulling data logging device through gui).
see being solution create class
has variable can can get
, set
dynamic size.
public class mysingleton { public static int datasize { get; set; } public double[] dataset = new double[datasize] { get; set; } }
something effect wrong , not work. have been trying research how initialize array @ runtime cannot figure out, feel don't know terms search. help?
what want use explicit (rather implicit) backing fields can add logic getter , setter. this:
public class mysingleton { private static int _datasize; // might want set sensible default public static int datasize { { return _datasize; } set { _datasize = value; _dataset = null; // changing size implicitly clear array - write code resize if wanted } } private static double[] _dataset; public static double[] dataset { { if (_dataset == null) { _dataset = new double[_datasize]; } return _dataset; } // can include setter if want let consumer set dataset directly - in case should update _datasize field. } }
Comments
Post a Comment