Custom data type (Structures) vs arrays

风流意气都作罢 提交于 2019-12-11 08:21:50

问题


I have a combo box that, when selected, displays text in 3 text boxes.

The combo box items are the 50 states, and the text boxes display information on state statutes. Right now I'm using a multidimensional array to hold the data for each state (each item in the array holds the state, item 1, item 2, item 3).

My question is: Would it be better or more compact to create a State data type that holds the 3 items, and actually if this is even an effective approach? I don't like the array, it's just all I know how to do right now.

For the most part, the 3 data types are used repeatedly.

For example, one of them is the time zone the state uses, so there are only 6 possible options.

I don't have the code in front of me or I would post it, just something I was thinking about.


回答1:


The question you have to ask yourself about a multidimensional array is, "Can another programmer look at it and understand what is going on?"

Is is more compact? Maybe mayby not. It all depends on the code. In truth, unless you are dealing with very low memory requirements, it probably doesn't matter. Think of it this way, let's say that each timezone takes up four bytes (size of an integer). Having entry for each timezone means you've used what 50 x 4 = 200 bytes. Not enough to worry about either way.

I would change it, because in 6 months you probably are going to have difficulty understanding what it does. Readability and maintainability is king in almost all situations.

so maybe an example is:

class State
{
    public State (string stateId, int timeZoneOffset)
    {
       StateId = stateId;
       TimeZoneOffSet = timeZoneOffset;
    }

    public String StateId {get;set;}
    public int TimeZoneOffest {get;set;}
}

public class StatesAndTerritories
{
    List<State> _states = new List<State>
    public StatesAndTerritories ()
    {
      //_state.Add state information here

      _state.Add(new State("AZ", -6); ......

    }

    public IEnumerable<State> GetStates (){
     return _state;
    }

   public IEnumerable<State> GetStatesInZimeZone(int timezone)
    {}

    etc..
}



回答2:


Go for the custom type, it's cleaner (you can access properties by name), less error prone (it is harder to confuse names than indices) and maintainable (the properties are not "sorted", there is no unneeded length that needs to fit the property count, and if you want properties of different types you do have type safety).




回答3:


The source code would be helpful for me to understand what exactly you are doing.

If pure performance is a goal then I would suggest a single dimension array with a struct holding the information for each state.

This would speed up access to the elements and make it easier to keep track of which variable has which value (named variables instead of array indexes). It is important to remember that a struct is a value type, so if you pass it to another function then the function will get a copy of the the struct and not the original. This is not a problem if you work directly with the array, but will be a problem if you use List or need to pass the selected state info to another method.



来源:https://stackoverflow.com/questions/8393456/custom-data-type-structures-vs-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!