自定义数据类型(结构)VS阵列 [英] Custom data type (Structures) vs arrays

查看:164
本文介绍了自定义数据类型(结构)VS阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组合框,选中时,在3文本框中显示文本。

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

该组合框项目是50个州,以及文本框显示在状态法规信息。现在,我使用的是多维数组来保存数据,每个状态(数组中的每个项目持有状态,第1项,第2项,第3项)。

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).

我的问题是:它会更好或更紧凑,以创建包含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.

有关的大多数情况下,3个数据类型被重复使用。

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

例如,其中一个是国家使用的时区,所以只有6个可能的选择。

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

我没有code在我面前还是我张贴,只是我在想什么。

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

推荐答案

你要问自己关于多维数组的问题是,另一个程序员看一下,能理解这是怎么回事?

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

时更紧凑?也许mayby​​没有。这一切都依赖于code。事实上,除非你正在处理非常低的内存需求,这可能并不重要。想想这样的方式,让我们说每一个时区占用四个字节(整数大小)。有每个时区项意味着你使用什么50×4 = 200字节。没有足够的理由担心两种方式。

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.

我要改变它,因为6个月,你可能将不得不很难理解它做什么。可读性和可维护性是国王在几乎所有情况。

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.

所以也许一个例子是:

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..
}

这篇关于自定义数据类型(结构)VS阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆