About Me

My photo
I'm an undergraduate of Sri Lanka Institute of Information Technology(SLIIT).

Matrix with List<>

Hello everyone!!!

Have you ever deal with matrix in you coding? If yes, how did you represent a matrix? Most probably you answer will 2D array. 2 D array is the most common matrix representation that all most all the coders using. But I have tried out different way and its successes. I used list to represent matrix. I believe we can easily have matrices graphical representation using this method.


 
You can consider single element as an integer variable.
By adding this kind of set of integer variable you can create an integer list. It is a matrix row.
By adding set of rows you can create a matrix.
I used this method to add a matrix addition question with two matrix with one answer and three wrong answers.

public class Row
{
    public List<int> cell = new List<int>();

    public void addCell(int element)
    {
            cell.Add(element);//add a single element
    }
}
public class Matrices
{
     public List<Row> matrix = new List<Row>();

     //add a row
     public void addMatrix(Row matrixRow)
     {
         matrix.Add(matrixRow);
     }

 }

Add following matrix in to lists using above classes

int A[,]=new int[4,2];
            A[,]={{1,2,3,4},{5,6,7,8}};
            int row=2;
            int col=4;
            Matrices mA = new Matrices ();

            //adding values for matrix
            for (int i = 0; i < row; i++)
            {
                Row rowA = new Row();

                for (int j = 0; j < col; j++)
                {
                    rowA.addCell(A[i, j]);
                }

                mA.addMatrix(rowA);
            }

Retrieve matrix elements

      List<int> elemntList= new List<int>();
            foreach (Row r1 in rowA)
            {

                  elemntList = r1.cell;

                  foreach (int element in elemntList)
                  {
                                Console.Write(element);
                  }
                                                               
                 Console.WriteLine();
                           
             }

Hope this post will help to you.
Don’t forget to leave a comment. Your suggestions are highly welcome.
Cheers!!!



Combobox items in wpf forms

Hello everyone!!!

You may use drop down lists in your projects recently. If you want to change the items in a combobox according to the selection of previous combobox what you will do? I’m going to tell how I do that in my wpf form.
In my case when it selects a mathematical operation it should load relevant formats to another combobox.

 ComboBoxItem item0 = new ComboBoxItem();
        ComboBoxItem item1 = new ComboBoxItem();
        ComboBoxItem item2 = new ComboBoxItem();
        ComboBoxItem item3 = new ComboBoxItem();
        ComboBoxItem item4 = new ComboBoxItem();
        ComboBoxItem item5 = new ComboBoxItem();
        ComboBoxItem item6 = new ComboBoxItem();
        ComboBoxItem item7 = new ComboBoxItem();
        ComboBoxItem item8 = new ComboBoxItem();
        ComboBoxItem item9 = new ComboBoxItem();
        ComboBoxItem item10 = new ComboBoxItem();
        ComboBoxItem item11 = new ComboBoxItem();

     if (cmbsimpleOperation.SelectedIndex == 0)
            {
                cmbFormat.IsEnabled = true;//enable format selection

                //load relevent items to index 0
                item0.Content = "A+B";
                item0.Background = Brushes.White;
                item0.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item0);

                item1.Content = "A+B'";
                item1.Background = Brushes.White;
                item1.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item1);

                item2.Content = "A'+B";
                item2.Background = Brushes.White;
                item2.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item2);

                item3.Content = "A'+B'";
                item3.Background = Brushes.White;
                item3.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item3);

                //remove already loaded items
                cmbFormat.Items.Remove(item4);
                cmbFormat.Items.Remove(item5);
                cmbFormat.Items.Remove(item6);
                cmbFormat.Items.Remove(item7);

                cmbFormat.Items.Remove(item8);
                cmbFormat.Items.Remove(item9);
                cmbFormat.Items.Remove(item10);
                cmbFormat.Items.Remove(item11);

          
            }

            if (cmbsimpleOperation.SelectedIndex == 1)
            {
                cmbFormat.IsEnabled = true;//enable format selection

                //load relevent items to index 1
                item4.Content = "A-B";
                item4.Background = Brushes.White;
                item4.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item4);

                item5.Content = "A-B'";
                item5.Background = Brushes.White;
                item5.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item5);

                item6.Content = "A'-B";
                item6.Background = Brushes.White;
                item6.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item6);

                item7.Content = "A'-B'";
                item7.Background = Brushes.White;
                item7.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item7);

                //removealready loaded items
                cmbFormat.Items.Remove(item0);
                cmbFormat.Items.Remove(item1);
                cmbFormat.Items.Remove(item2);
                cmbFormat.Items.Remove(item3);

                cmbFormat.Items.Remove(item8);
                cmbFormat.Items.Remove(item9);
                cmbFormat.Items.Remove(item10);
                cmbFormat.Items.Remove(item11);

               
            }

            if (cmbsimpleOperation.SelectedIndex == 2)
            {
                cmbFormat.IsEnabled = true;//enable format selection

                //load relevent items to index 2
                item8.Content = "A*B";
                item8.Background = Brushes.White;
                item8.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item8);

                item9.Content = "A*B'";
                item9.Background = Brushes.White;
                item9.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item9);

                item10.Content = "A'*B";
                item10.Background = Brushes.White;
                item10.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item10);

                item11.Content = "A'*B'";
                item11.Background = Brushes.White;
                item11.Foreground = Brushes.Black;
                cmbFormat.Items.Add(item11);

                //remove alreadyloaded items
                cmbFormat.Items.Remove(item0);
                cmbFormat.Items.Remove(item1);
                cmbFormat.Items.Remove(item2);
                cmbFormat.Items.Remove(item3);

                cmbFormat.Items.Remove(item4);
                cmbFormat.Items.Remove(item5);
                cmbFormat.Items.Remove(item6);
                cmbFormat.Items.Remove(item7);

               
            }

This is little bit different format comparing to windows form. Try this out.
Hope this post will help to you.
Don’t forget to leave a comment. Your suggestions are highly welcome.
Cheers!!!