Standardized US States Array and Countries Array

后端 未结 9 1728
再見小時候
再見小時候 2020-12-09 16:33

Does anyone know of a nice, usable class that everyone on here could benefit from to use in populating the ComboBox controls on a form for the Country and State

相关标签:
9条回答
  • 2020-12-09 17:34

    I took jp2code's answer and just made a tuple with it, rather than creating a new class and all of that... try this:

    public static class StateArray
    {
        public static List<(string Abbreviation, string Name)> States { get; }
    
        static StateArray()
        {
            States = new List<(string, string)>(50);
            States.Add(("AL", "Alabama"));
            States.Add(("AK", "Alaska"));
            States.Add(("AZ", "Arizona"));
            States.Add(("AR", "Arkansas"));
            States.Add(("CA", "California"));
            States.Add(("CO", "Colorado"));
            States.Add(("CT", "Connecticut"));
            States.Add(("DE", "Delaware"));
            States.Add(("DC", "District Of Columbia"));
            States.Add(("FL", "Florida"));
            States.Add(("GA", "Georgia"));
            States.Add(("HI", "Hawaii"));
            States.Add(("ID", "Idaho"));
            States.Add(("IL", "Illinois"));
            States.Add(("IN", "Indiana"));
            States.Add(("IA", "Iowa"));
            States.Add(("KS", "Kansas"));
            States.Add(("KY", "Kentucky"));
            States.Add(("LA", "Louisiana"));
            States.Add(("ME", "Maine"));
            States.Add(("MD", "Maryland"));
            States.Add(("MA", "Massachusetts"));
            States.Add(("MI", "Michigan"));
            States.Add(("MN", "Minnesota"));
            States.Add(("MS", "Mississippi"));
            States.Add(("MO", "Missouri"));
            States.Add(("MT", "Montana"));
            States.Add(("NE", "Nebraska"));
            States.Add(("NV", "Nevada"));
            States.Add(("NH", "New Hampshire"));
            States.Add(("NJ", "New Jersey"));
            States.Add(("NM", "New Mexico"));
            States.Add(("NY", "New York"));
            States.Add(("NC", "North Carolina"));
            States.Add(("ND", "North Dakota"));
            States.Add(("OH", "Ohio"));
            States.Add(("OK", "Oklahoma"));
            States.Add(("OR", "Oregon"));
            States.Add(("PA", "Pennsylvania"));
            States.Add(("RI", "Rhode Island"));
            States.Add(("SC", "South Carolina"));
            States.Add(("SD", "South Dakota"));
            States.Add(("TN", "Tennessee"));
            States.Add(("TX", "Texas"));
            States.Add(("UT", "Utah"));
            States.Add(("VT", "Vermont"));
            States.Add(("VA", "Virginia"));
            States.Add(("WA", "Washington"));
            States.Add(("WV", "West Virginia"));
            States.Add(("WI", "Wisconsin"));
            States.Add(("WY", "Wyoming"));
        }
    
        public static string[] Abbreviations()
        {
            List<string> abbrevList = new List<string>(States.Count);
            foreach (var state in States)
            {
                abbrevList.Add(state.Abbreviation);
            }
            return abbrevList.ToArray();
        }
    
        public static string[] Names()
        {
            List<string> nameList = new List<string>(States.Count);
            foreach (var state in States)
            {
                nameList.Add(state.Name);
            }
            return nameList.ToArray();
        }
    }
    
    0 讨论(0)
  • 2020-12-09 17:35
    public partial class State
    {
        public string Name { get; set; }
        public string Abbreviations { get; set; }
    }
    
        /// <summary>
        /// Return a static list of all U.S. states
        /// </summary>
        /// <returns></returns>
        public IEnumerable<State> ListOfStates()
        {
            var states = CreateStateList();
            return states.ToList();
        }
    
        private static IList<State> CreateStateList()
        {
            List<State> states = new List<State>();
    
            states.Add(new State() { Abbreviations = "AL", Name = "Alabama" });
            states.Add(new State() { Abbreviations = "AK", Name = "Alaska" });
            states.Add(new State() { Abbreviations = "AR", Name = "Arkansas" });
            states.Add(new State() { Abbreviations = "AZ", Name = "Arizona" });
            states.Add(new State() { Abbreviations = "CA", Name = "California" });
            states.Add(new State() { Abbreviations = "CO", Name = "Colorado" });
            states.Add(new State() { Abbreviations = "CT", Name = "Connecticut" });
            states.Add(new State() { Abbreviations = "DC", Name = "District of Columbia" });
            states.Add(new State() { Abbreviations = "DE", Name = "Delaware" });
            states.Add(new State() { Abbreviations = "FL", Name = "Florida" });
            states.Add(new State() { Abbreviations = "GA", Name = "Georgia" });
            states.Add(new State() { Abbreviations = "HI", Name = "Hawaii" });
            states.Add(new State() { Abbreviations = "ID", Name = "Idaho" });
            states.Add(new State() { Abbreviations = "IL", Name = "Illinois" });
            states.Add(new State() { Abbreviations = "IN", Name = "Indiana" });
            states.Add(new State() { Abbreviations = "IA", Name = "Iowa" });
            states.Add(new State() { Abbreviations = "KS", Name = "Kansas" });
            states.Add(new State() { Abbreviations = "KY", Name = "Kentucky" });
            states.Add(new State() { Abbreviations = "LA", Name = "Louisiana" });
            states.Add(new State() { Abbreviations = "ME", Name = "Maine" });
            states.Add(new State() { Abbreviations = "MD", Name = "Maryland" });
            states.Add(new State() { Abbreviations = "MA", Name = "Massachusetts" });
            states.Add(new State() { Abbreviations = "MI", Name = "Michigan" });
            states.Add(new State() { Abbreviations = "MN", Name = "Minnesota" });
            states.Add(new State() { Abbreviations = "MS", Name = "Mississippi" });
            states.Add(new State() { Abbreviations = "MO", Name = "Missouri" });
            states.Add(new State() { Abbreviations = "MT", Name = "Montana" });
            states.Add(new State() { Abbreviations = "NE", Name = "Nebraska" });
            states.Add(new State() { Abbreviations = "NH", Name = "New Hampshire" });
            states.Add(new State() { Abbreviations = "NJ", Name = "New Jersey" });
            states.Add(new State() { Abbreviations = "NM", Name = "New Mexico" });
            states.Add(new State() { Abbreviations = "NY", Name = "New York" });
            states.Add(new State() { Abbreviations = "NC", Name = "North Carolina" });
            states.Add(new State() { Abbreviations = "NV", Name = "Nevada" });
            states.Add(new State() { Abbreviations = "ND", Name = "North Dakota" });
            states.Add(new State() { Abbreviations = "OH", Name = "Ohio" });
            states.Add(new State() { Abbreviations = "OK", Name = "Oklahoma" });
            states.Add(new State() { Abbreviations = "OR", Name = "Oregon" });
            states.Add(new State() { Abbreviations = "PA", Name = "Pennsylvania" });
            states.Add(new State() { Abbreviations = "RI", Name = "Rhode Island" });
            states.Add(new State() { Abbreviations = "SC", Name = "South Carolina" });
            states.Add(new State() { Abbreviations = "SD", Name = "South Dakota" });
            states.Add(new State() { Abbreviations = "TN", Name = "Tennessee" });
            states.Add(new State() { Abbreviations = "TX", Name = "Texas" });
            states.Add(new State() { Abbreviations = "UT", Name = "Utah" });
            states.Add(new State() { Abbreviations = "VT", Name = "Vermont" });
            states.Add(new State() { Abbreviations = "VA", Name = "Virginia" });
            states.Add(new State() { Abbreviations = "WA", Name = "Washington" });
            states.Add(new State() { Abbreviations = "WV", Name = "West Virginia" });
            states.Add(new State() { Abbreviations = "WI", Name = "Wisconsin" });
            states.Add(new State() { Abbreviations = "WY", Name = "Wyoming" });
            return states.ToList();
        }
    

    VB.NET

    Public Shared Function CreateStateList() As IList(Of State)
            Dim states As New List(Of State)()
            states.Add(New State() With {.Abbreviation = "AL", .Name = "Alabama"})
            states.Add(New State() With {.Abbreviation = "AK", .Name = "Alaska"})
            states.Add(New State() With {.Abbreviation = "AR", .Name = "Arkansas"})
            states.Add(New State() With {.Abbreviation = "AZ", .Name = "Arizona"})
            states.Add(New State() With {.Abbreviation = "CA", .Name = "California"})
            states.Add(New State() With {.Abbreviation = "CO", .Name = "Colorado"})
            states.Add(New State() With {.Abbreviation = "CT", .Name = "Connecticut"})
            states.Add(New State() With {.Abbreviation = "DC", .Name = "District of Columbia"})
            states.Add(New State() With {.Abbreviation = "DE", .Name = "Delaware"})
            states.Add(New State() With {.Abbreviation = "FL", .Name = "Florida"})
            states.Add(New State() With {.Abbreviation = "GA", .Name = "Georgia"})
            states.Add(New State() With {.Abbreviation = "HI", .Name = "Hawaii"})
            states.Add(New State() With {.Abbreviation = "ID", .Name = "Idaho"})
            states.Add(New State() With {.Abbreviation = "IL", .Name = "Illinois"})
            states.Add(New State() With {.Abbreviation = "IN", .Name = "Indiana"})
            states.Add(New State() With {.Abbreviation = "IA", .Name = "Iowa"})
            states.Add(New State() With {.Abbreviation = "KS", .Name = "Kansas"})
            states.Add(New State() With {.Abbreviation = "KY", .Name = "Kentucky"})
            states.Add(New State() With {.Abbreviation = "LA", .Name = "Louisiana"})
            states.Add(New State() With {.Abbreviation = "ME", .Name = "Maine"})
            states.Add(New State() With {.Abbreviation = "MD", .Name = "Maryland"})
            states.Add(New State() With {.Abbreviation = "MA", .Name = "Massachusetts"})
            states.Add(New State() With {.Abbreviation = "MI", .Name = "Michigan"})
            states.Add(New State() With {.Abbreviation = "MN", .Name = "Minnesota"})
            states.Add(New State() With {.Abbreviation = "MS", .Name = "Mississippi"})
            states.Add(New State() With {.Abbreviation = "MO", .Name = "Missouri"})
            states.Add(New State() With {.Abbreviation = "MT", .Name = "Montana"})
            states.Add(New State() With {.Abbreviation = "NE", .Name = "Nebraska"})
            states.Add(New State() With {.Abbreviation = "NH", .Name = "New Hampshire"})
            states.Add(New State() With {.Abbreviation = "NJ", .Name = "New Jersey"})
            states.Add(New State() With {.Abbreviation = "NM", .Name = "New Mexico"})
            states.Add(New State() With {.Abbreviation = "NY", .Name = "New York"})
            states.Add(New State() With {.Abbreviation = "NC", .Name = "North Carolina"})
            states.Add(New State() With {.Abbreviation = "NV", .Name = "Nevada"})
            states.Add(New State() With {.Abbreviation = "ND", .Name = "North Dakota"})
            states.Add(New State() With {.Abbreviation = "OH", .Name = "Ohio"})
            states.Add(New State() With {.Abbreviation = "OK", .Name = "Oklahoma"})
            states.Add(New State() With {.Abbreviation = "OR", .Name = "Oregon"})
            states.Add(New State() With {.Abbreviation = "PA", .Name = "Pennsylvania"})
            states.Add(New State() With {.Abbreviation = "RI", .Name = "Rhode Island"})
            states.Add(New State() With {.Abbreviation = "SC", .Name = "South Carolina"})
            states.Add(New State() With {.Abbreviation = "SD", .Name = "South Dakota"})
            states.Add(New State() With {.Abbreviation = "TN", .Name = "Tennessee"})
            states.Add(New State() With {.Abbreviation = "TX", .Name = "Texas"})
            states.Add(New State() With {.Abbreviation = "UT", .Name = "Utah"})
            states.Add(New State() With {.Abbreviation = "VT", .Name = "Vermont"})
            states.Add(New State() With {.Abbreviation = "VA", .Name = "Virginia"})
            states.Add(New State() With {.Abbreviation = "WA", .Name = "Washington"})
            states.Add(New State() With {.Abbreviation = "WV", .Name = "West Virginia"})
            states.Add(New State() With {.Abbreviation = "WI", .Name = "Wisconsin"})
            states.Add(New State() With {.Abbreviation = "WY", .Name = "Wyoming"})
            Return states.ToList()
        End Function
    
    0 讨论(0)
  • 2020-12-09 17:36

    Here is the code done in a HashSet of SelectListItem

     public HashSet<SelectListItem> GetAllStates()
        {
            var states = new HashSet<SelectListItem>()
            {
                new SelectListItem() { ItemValue = "AL", Name = "Alabama" },
                new SelectListItem() { ItemValue = "AK", Name = "Alaska" },
                new SelectListItem() { ItemValue = "AZ", Name = "Arizona" }, 
                new SelectListItem() { ItemValue = "AR", Name = "Arkansas" },
                new SelectListItem() { ItemValue = "CA", Name = "California" },
                new SelectListItem() { ItemValue = "CO", Name = "Colorado" },
                new SelectListItem() { ItemValue = "CT", Name = "Connecticut" },
                new SelectListItem() { ItemValue = "DE", Name = "Delaware" },
                new SelectListItem() { ItemValue = "DC", Name = "District Of Columbia" },
                new SelectListItem() { ItemValue = "FL", Name = "Florida" },
                new SelectListItem() { ItemValue = "GA", Name = "Georgia" },
                new SelectListItem() { ItemValue = "HI", Name = "Hawaii" },
                new SelectListItem() { ItemValue = "ID", Name = "Idaho" },
                new SelectListItem() { ItemValue = "IL", Name = "Illinois" },
                new SelectListItem() { ItemValue = "IN", Name = "Indiana" },
                new SelectListItem() { ItemValue = "IA", Name = "Iowa" },
                new SelectListItem() { ItemValue = "KS", Name = "Kansas" },
                new SelectListItem() { ItemValue = "KY", Name = "Kentucky" },
                new SelectListItem() { ItemValue = "LA", Name = "Louisiana" },
                new SelectListItem() { ItemValue = "ME", Name = "Maine" },
                new SelectListItem() { ItemValue = "MD", Name = "Maryland" },
                new SelectListItem() { ItemValue = "MA", Name = "Massachusetts" },
                new SelectListItem() { ItemValue = "MI", Name = "Michigan" },
                new SelectListItem() { ItemValue = "MN", Name = "Minnesota" },
                new SelectListItem() { ItemValue = "MS", Name = "Mississippi" },
                new SelectListItem() { ItemValue = "MO", Name = "Missouri" },
                new SelectListItem() { ItemValue = "MT", Name = "Montana" },
                new SelectListItem() { ItemValue = "NE", Name = "Nebraska" },
                new SelectListItem() { ItemValue = "NV", Name = "Nevada" },
                new SelectListItem() { ItemValue = "NH", Name = "New Hampshire" },
                new SelectListItem() { ItemValue = "NJ", Name = "New Jersey" },
                new SelectListItem() { ItemValue = "NM", Name = "New Mexico" },
                new SelectListItem() { ItemValue = "NY", Name = "New York" },
                new SelectListItem() { ItemValue = "NC", Name = "North Carolina"},
                new SelectListItem() { ItemValue = "ND", Name = "North Dakota"},
                new SelectListItem() { ItemValue = "OH", Name = "Ohio" },
                new SelectListItem() { ItemValue = "OK", Name = "Oklahoma" },
                new SelectListItem() { ItemValue = "OR", Name = "Oregon" },
                new SelectListItem() { ItemValue = "PA", Name = "Pennsylvania" },
                new SelectListItem() { ItemValue = "RI", Name = "Rhode Island" },
                new SelectListItem() { ItemValue = "SC", Name = "South Carolina" },
                new SelectListItem() { ItemValue = "SD", Name = "South Dakota" },
                new SelectListItem() { ItemValue = "TN", Name = "Tennessee" },
                new SelectListItem() { ItemValue = "TX", Name = "Texas" },
                new SelectListItem() { ItemValue = "UT", Name = "Utah" },
                new SelectListItem() { ItemValue = "VT", Name = "Vermont" },
                new SelectListItem() { ItemValue = "VA", Name = "Virginia" },
                new SelectListItem() { ItemValue = "WA", Name = "Washington" },
                new SelectListItem() { ItemValue = "WV", Name = "West Virginia" },
                new SelectListItem() { ItemValue = "WI", Name = "Wisconsin" },
                new SelectListItem() { ItemValue = "WY", Name = "Wyoming" }
            };
    
            return states;
        }
    
    0 讨论(0)
提交回复
热议问题