Opened 8 years ago
Closed 7 years ago
#2723 closed defect (done)
Providing variable values as double[] to Dataset fails at runtime
Reported by: | abeham | Owned by: | gkronber |
---|---|---|---|
Priority: | medium | Milestone: | HeuristicLab 3.3.15 |
Component: | Problems.DataAnalysis | Version: | 3.3.14 |
Keywords: | Cc: |
Description
In the Dataset class an IList is accepted as variable values, however later on this IList is cast as a List<T>.
public Dataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues) : base() { } // line 199: List<T> values = list as List<T>; if (values == null) throw new ArgumentException("The variable " + variableName + " is not a " + typeof(T) + " variable.");
Now, a double[] is an IList, but not a List<double>. The compiler is happy, but the program crashes at runtime. In my opinion, it should be checked if it's a T[] in case List<T> fails or otherwise prevent accepting double[] in the constructor.
Change History (11)
comment:1 Changed 8 years ago by bburlacu
comment:2 Changed 8 years ago by gkronber
- Owner set to bburlacu
- Status changed from new to assigned
I think r14857 is not related to the ticket description.
comment:3 Changed 8 years ago by bburlacu
- Owner changed from bburlacu to gkronber
- Status changed from assigned to reviewing
comment:4 Changed 8 years ago by abeham
I agree with gkronber, this change does not adress the ticket.
comment:5 Changed 8 years ago by bburlacu
r15013: Found a different solution to this issue, by using IList<T> as the return type for the GetValues<T> method.
comment:6 Changed 8 years ago by bburlacu
r15015: Fix exception in Dataset.cs when calling the ToModifiable() method.
comment:7 Changed 7 years ago by gkronber
comment:8 Changed 7 years ago by gkronber
Reviewed r14857, r15013, r15015 and tested this with the following C# script.
var names = new string[] {"a", "b", "c"}; var @as = Enumerable.Repeat(1.0, 10).ToArray(); var bs = Enumerable.Repeat("abc", 10).ToArray(); var cs = Enumerable.Repeat(DateTime.Now, 10).ToArray(); var values = new System.Collections.IList[] {@as, bs, cs}; var ds = new Dataset(names, values); vars["ds"] = ds; Console.WriteLine(ds.GetDoubleValue("a", 0)); Console.WriteLine(ds.GetStringValue("b", 0)); Console.WriteLine(ds.GetDateTimeValue("c", 0)); var columnA = ds.GetReadOnlyDoubleValues("a"); Console.WriteLine(columnA[0]); var columnB = ds.GetReadOnlyStringValues("b"); Console.WriteLine(columnB[0]); var columnC = ds.GetReadOnlyDateTimeValues("c"); Console.WriteLine(columnC[0]); var aEnum = ds.GetDoubleValues("a"); Console.WriteLine(aEnum.First()); var bEnum = ds.GetStringValues("b"); Console.WriteLine(bEnum.First()); var cEnum = ds.GetDateTimeValues("c"); Console.WriteLine(cEnum.First());
comment:9 Changed 7 years ago by gkronber
comment:10 Changed 7 years ago by gkronber
- Status changed from reviewing to readytorelease
comment:11 Changed 7 years ago by gkronber
- Resolution set to done
- Status changed from readytorelease to closed
r14857: Add DatasetUtil static class with useful methods for dealing with Datasets. Add input validation to Dataset constructor.