0

This is a continuation of this question : Best way to represent sets of 3 values

I want to store the temperature of a substance as a function of time and position. So the temperature changes with location and time. regarding location, I have divided the whole surface of material into i*j small zones, so I can assign a temperature to each zone separately. for example, one should be able to read the temperature of zone[3,5] at time=3 seconds. my goal is, having a collection, consisting of time,i, and j. So I can access the temperature at time = 3.5 seconds in zone [i=4, j=7]. a 3D array doesn't help, because I need to be able to use time as a key like below:

temperature[3.5][4,7] 

here 3.5 is the time, 4 is i and 7 is j. How would I do that? And how should I access the temperature data?

4
  • Why not using a struct, too? Commented Mar 29, 2017 at 11:51
  • 2d array inside the temperature class? Or, "Dictionary<DateTime, Dictionary<int, Dictionary<int, Position>>>", however, the api of a Dictionary will not allow "temperature[3.5][4][7]" if there is a possibility that the key would not exist Commented Mar 29, 2017 at 11:51
  • You can definitely achieve this by defining some structs and overloading the indexer operator. Commented Mar 29, 2017 at 11:57
  • You can't achieve that with simple data collection. The reason behind this is that all those collections store discrete sets of data, while you're trying to store temperature as a continuous function of time. You'll have to either implement some custom storage class with collection-like interface or rethink the way you are working with that function. Is that function really a result of continuous temperature monitoring or you get it as an approximation of some discrete set of measurements? Commented Mar 29, 2017 at 12:06

2 Answers 2

2

If you want to implement exactly same interface, then you should use indexer properties. Let's start from the object which returns temperature by position. I'll call it Surface. You can use name Area or whatever you find more appropriate. This object should hold temperature values for each position on your surface or it should go and get those values from storage:

public class Surface
{
    // you can have time property here as well

    public double this[int i, int j]
    {
        get { return GetTemperatureValueFromDataSource(i,j); }
    }
}

Now you need to return these objects for given time from another class:

public class Temperature
{
    public Surface this[double time]
    {
        get { return GetSurfaceObjectForGivenTime(time); }
    }
}

Then interface will be as you want to:

var temperature = new Temperature();
double value = temperature[3.5][4,7];

Note that you should think how to handle situations when you don't have surface temperature data for given time, or if you are trying to get the temperature of surface point outside of available range. E.g. you can return null if there is no data for given time, but you should use null-conditional operator to avoid NullReferenceException:

 double? value = temperature[3.5]?[4,7];

Or you can return some NullObject which will have some dummy behavior. Same goes to referencing surface point outside of available range. You can use nullable return type for temperature value. Or you can throw ArgumentOutOfRangeException.

Sign up to request clarification or add additional context in comments.

Comments

0

I think it will be best to use a Dictionary<float,Dictionary<int,Dictionary<int,float>>>

It will let you index with a first float (time), and it will return a Dictionary of all zones with temperatures recorded for that given time.

Then you can index again with the int i to get all zones with that index, and index again with the int j, and then you finally get all temperatures for that time and that zone i,j

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.