开发者

C#: Checking That ArrayList Elements have specific type

               ArrayList fileList = new ArrayList();


      private void button2_Click(object sender, EventArgs e)
            {
          if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
            string line;

           开发者_运维技巧 // Read the file and display it line by line.
            System.IO.StreamReader file = new System.IO.StreamReader(openFileDialog1.FileName);
            while ((line = file.ReadLine()) != null)
            {
                // Puts elements in table
                fileList.Add(line.Split(';'));
            }

            file.Close();
        }

        for (int i = 0; i < fileList.Count; i++)
        {
            for (int x = 0; x < (fileList[i] as string[]).Length; x++)
            {
               // if (x ==0)
              //  { 
                    //fileList[0] must Be int 
              //  }
               // if (x==1)
                    //fileList[1] must be string 

                this.textBox2.Text += ((fileList[i] as string[])[x] + " ");
            }

            this.textBox2.Text += Environment.NewLine;
        }
    }

I am so far here.

I take the elements from a CSV file.

I need now to be sure that the 1 column has only numbers-integers (1,2,3,4,5), the second column has only names(so it will have the type string or character), the third surnames etc. etc.

The rows are presented like this : 1;George;Mano;

How can I be sure that the CSV file has the correct types?

I think that any more code about this problem will be placed inside the 2 for statements.

Thank you very much,

George.


I think your question needs more work.

You don't show your declaration for filelist. Whatever it is, there is no reason to convert it to string[] just to get the length. The length with be the same no matter what type it is. You cannot use this method to determine which items are strings.

You'll need to loop through the items and see if they contain only digits or whatever.

Also, your code to read CSV files is not quote right. CSV files are comma-separated. And it's possible that they could contain commas within double quotes. These commas should be ignored. A better way to read CSV files can be seen here.


An Arraylist contains object.

System.IO.StreamReader.ReadLine returns a String.

Checking the value of the first line read and trying to convert the string into an integer would be a valid approach.

Your current approach is adding the String that is returned by System.IO.StreamReader.ReadLine into your collection which you later turn into a String[] by using the String.Split method.

Your other requirements will be a greal more difficult because every line you are reading is a String already. So you would have to look at each character within the string to determine if it appears to be a name.

In other words you might want to find a different way to provide an input. I would agree that a regular expression might be the best way to get rid of junk data.


Edit: Now that we know it's really CSV, here's a columnar answer ;-)

Your ArrayList contains string[], so you need to verify that each array has the appropriate type of string.

for (int i = 0; i < fileList.Count; i++)
{
   string[] lineItems = (string[])fileList[i];
   if (!Regex.IsMatch (lineItems[0], "^\d+$")) // numbers
      throw new ArgumentException ("invalid id at row " + i);
   if (!Regex.IsMatch (lineItems[1], "^[a-zA-Z]+$")) // surnames - letters-only
      throw new ArgumentException ("invalid surname at row " + i);
   if (!Regex.IsMatch (lineItems[2], "^[a-zA-Z]+$")) // names - letters-only
      throw new ArgumentException ("invalid name at row " + i);
}


You can use Regex class.


fileList[0] must Be int:

int x;

if(int.TryParse(fileList[0], out x)){ //do whatever here and x will have that integer value. TryParse will return false if it's not an integer so the if will not fire}

fileList[1] must be string :

iterate over the string and check each element is a letter. look at the char. methods for the appropriate one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜