Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to populate a dropdownlist with a class.
C#
public CascadingDropDownNameValue[] GetCountries(string knownCategoryValues, string category)  
  
{ 
  
//var countryList = new List<CascadingDropDownNameValue>(); 
  
  
//DataSet dsCountry = OmegaLove.BLL.Country.GetCountries("omegaloveConnectionString"); 
  
LookupService ls = new LookupService("C:/inetpub/wwwroot/OmegaLove/OmegaLove.Web/GeoData/maxmind-geoip-city-org-02-2010/GeoIPCity.dat", LookupService.GEOIP_STANDARD);  
  
   
  
foreach (DataRow row in dsCountry.Tables[0].Rows)  
  
{ 
  
countryList.Add(new CascadingDropDownNameValue(row["Country"].ToString(), row["CountryID"].ToString()));  
  
} 
  
return countryList.ToArray();  
  
} 
not sure how to populat countrylist with a class.
using System; 
using System.IO; 
using System.Net; 
using System.Runtime.CompilerServices; 
  
public class LookupService{ 
    private FileStream file = null; 
    private DatabaseInfo databaseInfo = null; 
    byte databaseType = Convert.ToByte(DatabaseInfo.COUNTRY_EDITION); 
    int[] databaseSegments; 
    int recordLength; 
    int dboptions; 
    byte[] dbbuffer; 
  
    String licenseKey; 
    int dnsService = 0; 
    private static Country UNKNOWN_COUNTRY = new Country("--", "N/A"); 
    private static int COUNTRY_BEGIN = 16776960; 
    private static int STATE_BEGIN   = 16700000; 
    private static int STRUCTURE_INFO_MAX_SIZE = 20; 
    private static int DATABASE_INFO_MAX_SIZE = 100; 
    private static int FULL_RECORD_LENGTH = 100;//??? 
    private static int SEGMENT_RECORD_LENGTH = 3; 
    private static int STANDARD_RECORD_LENGTH = 3; 
    private static int ORG_RECORD_LENGTH = 4; 
    private static int MAX_RECORD_LENGTH = 4; 
    private static int MAX_ORG_RECORD_LENGTH = 1000;//??? 
    private static int FIPS_RANGE = 360; 
    private static int STATE_BEGIN_REV0 = 16700000; 
    private static int STATE_BEGIN_REV1 = 16000000; 
    private static int US_OFFSET = 1; 
    private static int CANADA_OFFSET = 677; 
    private static int WORLD_OFFSET = 1353; 
    public static int GEOIP_STANDARD = 0; 
    public static int GEOIP_MEMORY_CACHE = 1; 
    public static int GEOIP_UNKNOWN_SPEED = 0; 
    public static int GEOIP_DIALUP_SPEED = 1; 
    public static int GEOIP_CABLEDSL_SPEED = 2; 
    public static int GEOIP_CORPORATE_SPEED = 3; 
  
    private static String[] countryCode = { 
        "--","AP","EU","AD","AE","AF","AG","AI","AL","AM","AN","AO","AQ","AR", 
        "AS","AT","AU","AW","AZ","BA","BB","BD","BE","BF","BG","BH","BI","BJ", 
        "BM","BN","BO","BR","BS","BT","BV","BW","BY","BZ","CA","CC","CD","CF", 
        "CG","CH","CI","CK","CL","CM","CN","CO","CR","CU","CV","CX","CY","CZ", 
        "DE","DJ","DK","DM","DO","DZ","EC","EE","EG","EH","ER","ES","ET","FI", 
        "FJ","FK","FM","FO","FR","FX","GA","GB","GD","GE","GF","GH","GI","GL", 
        "GM","GN","GP","GQ","GR","GS","GT","GU","GW","GY","HK","HM","HN","HR", 
        "HT","HU","ID","IE","IL","IN","IO","IQ","IR","IS","IT","JM","JO","JP", 
        "KE","KG","KH","KI","KM","KN","KP","KR","KW","KY","KZ","LA","LB","LC", 
        "LI","LK","LR","LS","LT","LU","LV","LY","MA","MC","MD","MG","MH","MK", 
        "ML","MM","MN","MO","MP","MQ","MR","MS","MT","MU","MV","MW","MX","MY", 
        "MZ","NA","NC","NE","NF","NG","NI","NL","NO","NP","NR","NU","NZ","OM", 
        "PA","PE","PF","PG","PH","PK","PL","PM","PN","PR","PS","PT","PW","PY", 
        "QA","RE","RO","RU","RW","SA","SB","SC","SD","SE","SG","SH","SI","SJ", 
        "SK","SL","SM","SN","SO","SR","ST","SV","SY","SZ","TC","TD","TF","TG", 
        "TH","TJ","TK","TM","TN","TO","TL","TR","TT","TV","TW","TZ","UA","UG", 
        "UM","US","UY","UZ","VA","VC","VE","VG","VI","VN","VU","WF","WS","YE", 
        "YT","RS","ZA","ZM","ME","ZW","A1","A2","O1","AX","GG","IM","JE","BL", 
    "MF"}; 
  
    private static String[] countryName = { 
            "N/A","Asia/Pacific Region","Europe","Andorra","United Arab Emirates", 
            "Afghanistan","Antigua and Barbuda","Anguilla","Albania","Armenia", 
            "Netherlands Antilles","Angola","Antarctica","Argentina","American Samoa", 
            "Austria","Australia","Aruba","Azerbaijan","Bosnia and Herzegovina", 
            "Barbados","Bangladesh","Belgium","Burkina Faso","Bulgaria","Bahrain", 
            "Burundi","Benin","Bermuda","Brunei Darussalam","Bolivia","Brazil","Bahamas", 
            "Bhutan","Bouvet Island","Botswana","Belarus","Belize","Canada", 
            "Cocos (Keeling) Islands","Congo, The Democratic Republic of the", 
            "Central African Republic","Congo","Switzerland","Cote D'Ivoire", 
            "Cook Islands","Chile","Cameroon","China","Colombia","Costa Rica","Cuba", 
            "Cape Verde","Christmas Island","Cyprus","Czech Republic","Germany", 
            "Djibouti","Denmark","Dominica","Dominican Republic","Algeria","Ecuador", 
            "Estonia","Egypt","Western Sahara","Eritrea","Spain","Ethiopia","Finland", 
            "Fiji","Falkland Islands (Malvinas)","Micronesia, Federated States of", 
            "Faroe Islands","France","France, Metropolitan","Gabon","United Kingdom", 
            "Grenada","Georgia","French Guiana","Ghana","Gibraltar","Greenland","Gambia", 
            "Guinea","Guadeloupe","Equatorial Guinea","Greece", 
            "South Georgia and the South Sandwich Islands","Guatemala","Guam", 
            "Guinea-Bissau","Guyana","Hong Kong","Heard Island and McDonald Islands", 
            "Honduras","Croatia","Haiti","Hungary","Indonesia","Ireland","Israel","India", 
            "British Indian Ocean Territory","Iraq","Iran, Islamic Republic of", 
            "Iceland","Italy","Jamaica","Jordan","Japan","Kenya","Kyrgyzstan","Cambodia", 
            "Kiribati","Comoros","Saint Kitts and Nevis", 
            "Korea, Democratic People's Republic of","Korea, Republic of","Kuwait", 
            "Cayman Islands","Kazakhstan","Lao People's Democratic Republic","Lebanon", 
            "Saint Lucia","Liechtenstein","Sri Lanka","Liberia","Lesotho","Lithuania", 
            "Luxembourg","Latvia","Libyan Arab Jamahiriya","Morocco","Monaco", 
            "Moldova, Republic of","Madagascar","Marshall Islands", 
            "Macedonia, the Former Yugoslav Republic of","Mali","Myanmar","Mongolia", 
            "Macau","Northern Mariana Islands","Martinique","Mauritania","Montserrat", 
            "Malta","Mauritius","Maldives","Malawi","Mexico","Malaysia","Mozambique", 
            "Namibia","New Caledonia","Niger","Norfolk Island","Nigeria","Nicaragua", 
            "Netherlands","Norway","Nepal","Nauru","Niue","New Zealand","Oman","Panama", 
            "Peru","French Polynesia","Papua New Guinea","Philippines","Pakistan", 
            "Poland","Saint Pierre and Miquelon","Pitcairn","Puerto Rico","" + 
            "Palestinian Territory, Occupied","Portugal","Palau","Paraguay","Qatar", 
            "Reunion","Romania","Russian Federation","Rwanda","Saudi Arabia", 
            "Solomon Islands","Seychelles","Sudan","Sweden","Singapore","Saint Helena", 
            "Slovenia","Svalbard and Jan Mayen","Slovakia","Sierra Leone","San Marino", 
            "Senegal","Somalia","Suriname","Sao Tome and Principe","El Salvador", 
            "Syrian Arab Republic","Swaziland","Turks and Caicos Islands","Chad", 
            "French Southern Territories","Togo","Thailand","Tajikistan","Tokelau", 
            "Turkmenistan","Tunisia","Tonga","Timor-Leste","Turkey","Trinidad and Tobago", 
            "Tuvalu","Taiwan","Tanzania, United Republic of","Ukraine","Uganda", 
            "United States Minor Outlying Islands","United States","Uruguay","Uzbekistan", 
            "Holy See (Vatican City State)","Saint Vincent and the Grenadines", 
            "Venezuela","Virgin Islands, British","Virgin Islands, U.S.","Vietnam", 
            "Vanuatu","Wallis and Futuna","Samoa","Yemen","Mayotte","Serbia", 
            "South Africa","Zambia","Montenegro","Zimbabwe","Anonymous Proxy", 
            "Satellite Provider","Other", 
        "Aland Islands","Guernsey","Isle of Man","Jersey","Saint Barthelemy", 
        "Saint Martin"}; 
  
    public LookupService(String databaseFile, int options){ 
        try { 
       this.file = new FileStream(databaseFile, FileMode.Open, FileAccess.Read); 
       dboptions = options; 
           init(); 
        } catch(System.SystemException) { 
           Console.Write("cannot open file " + databaseFile + "\n"); 
        } 
    } 
  
    public LookupService(String databaseFile):this(databaseFile, GEOIP_STANDARD){ 
    } 
  
    private void init(){ 
       int i, j; 
       byte [] delim = new byte[3]; 
       byte [] buf = new byte[SEGMENT_RECORD_LENGTH]; 
       databaseType = (byte)DatabaseInfo.COUNTRY_EDITION; 
       recordLength = STANDARD_RECORD_LENGTH; 
       //file.Seek(file.Length() - 3,SeekOrigin.Begin); 
       file.Seek(-3,SeekOrigin.End); 
        for (i = 0; i < STRUCTURE_INFO_MAX_SIZE; i++) { 
            file.Read(delim,0,3); 
            if (delim[0] == 255 && delim[1] == 255 && delim[2] == 255){ 
                databaseType = Convert.ToByte(file.ReadByte()); 
                if (databaseType >= 106) { 
                    // Backward compatibility with databases from April 2003 and earlier 
                    databaseType -= 105; 
                } 
                // Determine the database type. 
                if (databaseType == DatabaseInfo.REGION_EDITION_REV0) { 
                    databaseSegments = new int[1]; 
                    databaseSegments[0] = STATE_BEGIN_REV0; 
                    recordLength = STANDARD_RECORD_LENGTH; 
                } else if (databaseType == DatabaseInfo.REGION_EDITION_REV1) { 
                    databaseSegments = new int[1]; 
                    databaseSegments[0] = STATE_BEGIN_REV1; 
                    recordLength = STANDARD_RECORD_LENGTH; 
                } else if (databaseType == DatabaseInfo.CITY_EDITION_REV0 || 
                        databaseType == DatabaseInfo.CITY_EDITION_REV1 || 
                        databaseType == DatabaseInfo.ORG_EDITION || 
                        databaseType == DatabaseInfo.ISP_EDITION || 
                        databaseType == DatabaseInfo.ASNUM_EDITION) 
                { 
                    databaseSegments = new int[1]; 
                    databaseSegments[0] = 0; 
                    if (databaseType == DatabaseInfo.CITY_EDITION_REV0 || 
                        databaseType == DatabaseInfo.CITY_EDITION_REV1) { 
                        recordLength = STANDARD_RECORD_LENGTH; 
                    } 
                    else { 
                        recordLength = ORG_RECORD_LENGTH; 
                    } 
                    file.Read(buf,0,SEGMENT_RECORD_LENGTH); 
                    for (j = 0; j < SEGMENT_RECORD_LENGTH; j++) { 
                        databaseSegments[0] += (unsignedByteToInt(buf[j]) << (j * 8)); 
                    } 
                } 
                break; 
            } 
            else { 
                //file.Seek(file.getFilePointer() - 4); 
                file.Seek(-4,SeekOrigin.Current); 
            //file.Seek(file.position-4,SeekOrigin.Begin); 
        } 
        } 
        if ((databaseType == DatabaseInfo.COUNTRY_EDITION) |  
            (databaseType == DatabaseInfo.PROXY_EDITION) | 
            (databaseType == DatabaseInfo.NETSPEED_EDITION)) { 
            databaseSegments = new int[1]; 
            databaseSegments[0] = COUNTRY_BEGIN; 
            recordLength = STANDARD_RECORD_LENGTH; 
        } 
        if ((dboptions & GEOIP_MEMORY_CACHE) == 1) { 
            int l = (int) file.Length; 
            dbbuffer = new byte[l]; 
            file.Seek(0,SeekOrigin.Begin); 
            file.Read(dbbuffer,0,l); 
        } 
    } 
    public void close(){ 
            try { 
            file.Close(); 
            file = null; 
        } 
        catch (Exception) { } 
    }
Posted
Updated 11-Feb-11 14:12pm
v3
Comments
Manfred Rudolf Bihy 11-Feb-11 19:56pm    
Added code tags.
DaveAuld 11-Feb-11 20:24pm    
And the question is what exactly?
Sandeep Mewara 11-Feb-11 23:51pm    
Too much of code. Might be good if you post what issue/error you get if you try it.

1 solution

This code, a bulk of hard-coded immediate constants (especially of string constants) is truly depressing. Before thinking of population of any controls (which will be easy) try to re-think it.

Mixing up of code with data is a big dirty anti-pattern (http://en.wikipedia.org/wiki/Anti-pattern[^]).

Mixing UI with "business logic" as another anti-pattern.

Missing all together… too bad. Use some data files, configurations file, XML, JSON or something, then we will be able to talk about population…

—SA
 
Share this answer
 
v3
Comments
Sandeep Mewara 11-Feb-11 23:50pm    
Good suggestion! 5.

BTW, you knew this 'Anti-pattern' thingy from before? I mean I read it today based on your answers.
Sergey Alexandrovich Kryukov 12-Feb-11 0:08am    
Thank you. I tried to identify anti-pattens before I read about those. Not so long time ago I worked for several month for a company with huge and mostly defunct legacy code. I claimed those people use every single anti-pattern listed (including the button I mentioned in another answer you just remembered) :-)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900