Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 875
george mano Avatar asked Dec 22 '25 05:12

george mano


1 Answers

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.

like image 166
Jonathan Wood Avatar answered Dec 23 '25 19:12

Jonathan Wood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!