Click here to Skip to main content
15,883,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have several arrays to collect data from a postgreSQL query. The data comes from RF receiver deployed in the field. As the user establishes the range of frequencies and time for the query the data is displayed on gridview and passed from there to the arrays indicated by an I value and a I + value matching the cell number in the column containing the latitude of the RF receiver. This data is use to plot xy scatter graphs representing (frequency,amplitude), (frequency,bandwidth) etc. I would like to create a list and objects instead of using arrays.

What I have tried:

C#
dataGridView1.SelectAll();
                    numCells = dataGridView1.SelectedCells.Count;
for (i = 14; i < (numCells); i += 14)
                       
                    {

                        if (i < numCells)
                        {
                            
                            if (Convert.ToDouble(dataGridView1.SelectedCells[i + 11].Value.ToString()) == 28.464258839)
                            {
                                UCS1size += 1;
                            }
                            else if (Convert.ToDouble(dataGridView1.SelectedCells[i + 11].Value.ToString()) == 28.42859146)
                            {
                                MOCsize += 1;
                            }
                            else if (Convert.ToDouble(dataGridView1.SelectedCells[i + 11].Value.ToString()) == 28.490616471)
                            {
                                AMsize += 1;
                            }
                            else if (Convert.ToDouble(dataGridView1.SelectedCells[i + 11].Value.ToString()) == 28.525409911)
                            {
                                UCS2size += 1;
                            }
                            else if (Convert.ToDouble(dataGridView1.SelectedCells[i + 11].Value.ToString()) == 28.560529988)
                            {
                                LC40size += 1;
                            }
                        }

                        else
                        {
                            MessageBox.Show("exiting for loop. numcells: " + numCells);
                        }

                    }
 double[] freqLC40 = new double[LC40size];
                    double[] ampMaxLC40 = new double[LC40size];
                    
                    double[] freqMOC = new double[MOCsize];
                    double[] ampMaxMOC = new double[MOCsize];

                    double[] freqUCS1 = new double[UCS1size];
                    double[] ampMaxUCS1 = new double[UCS1size];


                    double[] freqUCS2 = new double[UCS2size];
                    double[] ampMaxUCS2 = new double[UCS2size];


                    double[] freqAM = new double[AMsize];
                    double[] ampMaxAM = new double[AMsize];


                    int LC40idx = 0;
                    int MOCidx = 0;
                    int UCS1idx = 0;
                    int UCS2idx = 0;
                    int AMsidx = 0;


                    for (i = 14; i < (numCells); i += 14)
                    {

                        if (i < numCells)
                        {
                            if (Convert.ToDouble(dataGridView1.SelectedCells[i + 11].Value.ToString()) == 28.464258839)
                            {
                                freqUCS1[UCS1idx] = Convert.ToDouble(dataGridView1.SelectedCells[i].Value.ToString());
                                ampMaxUCS1[UCS1idx] = Convert.ToDouble(dataGridView1.SelectedCells[i + 9].Value.ToString());
                                UCS1idx += 1;
                            }
                            else if (Convert.ToDouble(dataGridView1.SelectedCells[i + 11].Value.ToString()) == 28.42859146)
                            {
                                freqMOC[MOCidx] = Convert.ToDouble(dataGridView1.SelectedCells[i].Value.ToString());
                                ampMaxMOC[MOCidx] = Convert.ToDouble(dataGridView1.SelectedCells[i + 9].Value.ToString());
                                MOCidx += 1;
                            }
                            else if (Convert.ToDouble(dataGridView1.SelectedCells[i + 11].Value.ToString()) == 28.490616471)
                            {
                                freqAM[AMsidx] = Convert.ToDouble(dataGridView1.SelectedCells[i].Value.ToString());

                                ampMaxAM[AMsidx] = Convert.ToDouble(dataGridView1.SelectedCells[i + 9].Value.ToString());
                                AMsidx += 1;
                            }
                            else if (Convert.ToDouble(dataGridView1.SelectedCells[i + 11].Value.ToString()) == 28.525409911)
                            {
                                freqUCS2[UCS2idx] = Convert.ToDouble(dataGridView1.SelectedCells[i].Value.ToString());
                                ampMaxUCS2[UCS2idx] = Convert.ToDouble(dataGridView1.SelectedCells[i + 9].Value.ToString());
                                UCS2idx += 1;
                            }
                            else if (Convert.ToDouble(dataGridView1.SelectedCells[i + 11].Value.ToString()) == 28.560529988)
                            {
                                freqLC40[LC40idx] = Convert.ToDouble(dataGridView1.SelectedCells[i].Value.ToString());
                                ampMaxLC40[LC40idx] = Convert.ToDouble(dataGridView1.SelectedCells[i + 9].Value.ToString());
                                LC40idx += 1;
                            }

                        }
                        else
                        {
                            MessageBox.Show("exiting for loop. numcells: " + numCells);
                        }

                    }


            //get XY form LC40
            xyForm = new XYplotForm();
            xyForm.Plot(freqLC40, ampMaxLC40, "LC-40 Max Amplitude");
            xyForm.Show();
Posted
Updated 27-Jun-16 20:54pm
v2
Comments
Maciej Los 28-Jun-16 2:34am    
You should be more specific and provide more details about your expected result!
Philippe Mori 28-Jun-16 10:15am    
Learn to write robust code...

There are so many bad things in that code that will make it unmaintainable. You should avoid hard-coded values and repetitive code among other things.
FreddyPR 28-Jun-16 10:53am    
Well that's why I'm posting. I'm looking for constructive advice. I'm not a programmer by trade if I knew better I wouldn't need to ask.

As an alternative, you might employ Linq, e.g.
C#
using System.Linq;
...
double[] array = new double[10];
List<double> list = array.ToList();
Cheers
Andi
 
Share this answer
 
Comments
FreddyPR 28-Jun-16 10:57am    
Thank you very much, will look into that.
C#
double[] array = new double[10];
List<double> list = new List<double>(array);
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 28-Jun-16 0:56am    
I up-voted the answer, it's correct. Perhaps you should better ignore the morons voting in idiotic ways; that happens all the time...
—SA
Maciej Los 28-Jun-16 1:49am    
And one upvote from me. A 5!
Andreas Gieriet 28-Jun-16 2:51am    
Mine too. My 5!
Cheers
Andi
FreddyPR 28-Jun-16 10:56am    
Thank you for inputs, will apply and implemen.

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