none
Multidimensional Arraylist

    Question

  • It's for learning purpose.
    A program that returns has the user enter a male and female height, calculates a table of ideal weights for males and females, and stores the results in an array. The program should then display the table in either a listbox or listview object.
    Female = 3.5 * height (in inches) - 108
    Male = 4.0 * height (in inches) - 128

    But i'm having problem with the output, when i try to enter for example 2 values for male and 2 values for female it should calculate the weight individually for both the values of famle and male and display all the 4 values,but what it's doing it's just calculating the last value whichever i added for male & female and displaying it twice,if i add 2 values.Can any one help me with this
     I know there is some thing wrong here
    I'm not knowing how to get the 1st values from arrylist,it's always taking the second value.

    double[][] Heights = new double[2][];
               
            for (i=0;i<Height.Count;i++)
            {
                Heights[0][i] = 3.5 * inputfe - 108;  //Something Wrong here
                Heights[1][i] = 4.0 * inputme - 128; //Something Wrong here

    using System;
    
    using System.Collections;
    
    using System.Windows.Forms;
    
    
    
    public class frmMain : Form
    
    {
    
        ArrayList Height = new ArrayList();
    
        //ArrayList mHeight = new ArrayList();
    
        //ArrayList feHeight= new ArrayList();
    
        public double inputme=0.0;
    
        public double inputfe=0.0;
    
        private Label label1;
    
        private TextBox txtFeHeight;
    
        private Label label2;
    
        private Button btnCalculate;
    
        private Button btnExit;
    
        private ListView lstShow;
    
        private ColumnHeader colFemale;
    
        private ColumnHeader colMale;
    
        private Button btnAdd;
    
        private TextBox txtMaleHeight;
    
        #region Windows code
    
        private void InitializeComponent()
    
        {
    
            this.label1 = new System.Windows.Forms.Label();
    
            this.txtFeHeight = new System.Windows.Forms.TextBox();
    
            this.label2 = new System.Windows.Forms.Label();
    
            this.txtMaleHeight = new System.Windows.Forms.TextBox();
    
            this.btnCalculate = new System.Windows.Forms.Button();
    
            this.btnExit = new System.Windows.Forms.Button();
    
            this.lstShow = new System.Windows.Forms.ListView();
    
            this.colFemale = new System.Windows.Forms.ColumnHeader();
    
            this.colMale = new System.Windows.Forms.ColumnHeader();
    
            this.btnAdd = new System.Windows.Forms.Button();
    
            this.SuspendLayout();
    
            // 
    
            // label1
    
            // 
    
            this.label1.AutoSize = true;
    
            this.label1.Location = new System.Drawing.Point(3, 26);
    
            this.label1.Name = "label1";
    
            this.label1.Size = new System.Drawing.Size(130, 13);
    
            this.label1.TabIndex = 0;
    
            this.label1.Text = "Female height(in inches):  ";
    
            // 
    
            // txtFeHeight
    
            // 
    
            this.txtFeHeight.Location = new System.Drawing.Point(238, 26);
    
            this.txtFeHeight.Name = "txtFeHeight";
    
            this.txtFeHeight.Size = new System.Drawing.Size(143, 20);
    
            this.txtFeHeight.TabIndex = 1;
    
            // 
    
            // label2
    
            // 
    
            this.label2.AutoSize = true;
    
            this.label2.Location = new System.Drawing.Point(6, 68);
    
            this.label2.Name = "label2";
    
            this.label2.Size = new System.Drawing.Size(115, 13);
    
            this.label2.TabIndex = 2;
    
            this.label2.Text = "Male Height(in inches):";
    
            // 
    
            // txtMaleHeight
    
            // 
    
            this.txtMaleHeight.Location = new System.Drawing.Point(238, 68);
    
            this.txtMaleHeight.Name = "txtMaleHeight";
    
            this.txtMaleHeight.Size = new System.Drawing.Size(143, 20);
    
            this.txtMaleHeight.TabIndex = 3;
    
            // 
    
            // btnCalculate
    
            // 
    
            this.btnCalculate.Location = new System.Drawing.Point(12, 103);
    
            this.btnCalculate.Name = "btnCalculate";
    
            this.btnCalculate.Size = new System.Drawing.Size(75, 23);
    
            this.btnCalculate.TabIndex = 4;
    
            this.btnCalculate.Text = "&Calculate";
    
            this.btnCalculate.UseVisualStyleBackColor = true;
    
            this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
    
            // 
    
            // btnExit
    
            // 
    
            this.btnExit.Location = new System.Drawing.Point(285, 103);
    
            this.btnExit.Name = "btnExit";
    
            this.btnExit.Size = new System.Drawing.Size(75, 23);
    
            this.btnExit.TabIndex = 6;
    
            this.btnExit.Text = "&Exit";
    
            this.btnExit.UseVisualStyleBackColor = true;
    
            this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
    
            // 
    
            // lstShow
    
            // 
    
            this.lstShow.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
    
                this.colFemale,
    
                this.colMale});
    
            this.lstShow.Location = new System.Drawing.Point(13, 148);
    
            this.lstShow.Name = "lstShow";
    
            this.lstShow.Size = new System.Drawing.Size(549, 126);
    
            this.lstShow.TabIndex = 7;
    
            this.lstShow.UseCompatibleStateImageBehavior = false;
    
            this.lstShow.View = System.Windows.Forms.View.Details;
    
            // 
    
            // colFemale
    
            // 
    
            this.colFemale.Text = "Female Weight";
    
            this.colFemale.Width = 205;
    
            // 
    
            // colMale
    
            // 
    
            this.colMale.Text = "Male Weight";
    
            this.colMale.Width = 153;
    
            // 
    
            // btnAdd
    
            // 
    
            this.btnAdd.Location = new System.Drawing.Point(132, 102);
    
            this.btnAdd.Name = "btnAdd";
    
            this.btnAdd.Size = new System.Drawing.Size(75, 23);
    
            this.btnAdd.TabIndex = 8;
    
            this.btnAdd.Text = "Add";
    
            this.btnAdd.UseVisualStyleBackColor = true;
    
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
    
            // 
    
            // frmMain
    
            // 
    
            this.ClientSize = new System.Drawing.Size(616, 286);
    
            this.Controls.Add(this.btnAdd);
    
            this.Controls.Add(this.lstShow);
    
            this.Controls.Add(this.btnExit);
    
            this.Controls.Add(this.btnCalculate);
    
            this.Controls.Add(this.txtMaleHeight);
    
            this.Controls.Add(this.label2);
    
            this.Controls.Add(this.txtFeHeight);
    
            this.Controls.Add(this.label1);
    
            this.Name = "frmMain";
    
            this.ResumeLayout(false);
    
            this.PerformLayout();
    
    
    
        }
    
        #endregion
    
    
    
        public frmMain()
    
        {
    
            InitializeComponent();
    
        }
    
    
    
        public static void Main()
    
        {
    
            frmMain main = new frmMain();
    
            Application.Run(main);
    
        }
    
        private void btnAdd_Click(object sender, EventArgs e)
    
        {
    
            //double femaleHeight;
    
            //double maleHeight;
    
            //bool flag;
    
            
    
            //double inputme=0.0;
    
            //double inputfe=0.0;
    
            //int i;
    
            inputfe = double.Parse(txtFeHeight.Text);
    
            inputme = double.Parse(txtMaleHeight.Text);
    
            int length = Height.Count;
    
            Height.Add(new object[]{inputfe,inputme});
    
            txtFeHeight.Clear();
    
            txtMaleHeight.Clear();
    
            txtFeHeight.Focus();
    
        
    
            if (inputme < inputfe)
    
            {
    
                MessageBox.Show("Male sld have heigher value", "Invalid");
    
                txtFeHeight.Clear();
    
                txtFeHeight.Focus();
    
                return;
    
            }
    
          }
    
    
    
        private void btnCalculate_Click(object sender, EventArgs e)
    
        {
    
            int i; 
    
            double[][] Heights = new double[2][];
    
                
    
            for (i=0;i<Height.Count;i++)
    
            {
    
                Heights[0][i] = 3.5 * inputfe - 108;
    
                Heights[1][i] = 4.0 * inputme - 128;
    
                  ListViewItem which;
    
                  which = new ListViewItem(Heights[0][i].ToString());
    
                  which.SubItems.Add(Heights[1][i].ToString());
    
                  lstShow.Items.Add(which);
    
                }
    
              
    
                 
    
        }
    
    
    
         private void btnExit_Click(object sender, EventArgs e)
    
        {
    
            this.Close();
    
        }
    
    
    
    }
    
    
    Thursday, November 19, 2009 3:12 AM

Answers

  • Hi,
    This is not a forum for C# related Post anyway.i done some changes in your code.hope it helps.
      private void btnCalculate_Click(object sender, EventArgs e)
        {
            int i;
            int count = Height.Count;
           double[][] Heights ={ new double[2],new double[count]};
            for (i = 0; i < Height.Count; i++)
            {
                Heights[0][i] =3.5 * inputfe - 108;
    
                Heights[1][i] = 4.0 * inputme - 128;
    
                ListViewItem which;
    
                which = new ListViewItem(Heights[0][i].ToString());
    
                which.SubItems.Add(Heights[1][i].ToString());
    
                lstShow.Items.Add(which);
    
            }
    
            Height.Clear();
        }



    Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
    • Marked as answer by Priya Gill Thursday, November 19, 2009 7:34 AM
    • Unmarked as answer by Priya Gill Thursday, November 19, 2009 8:21 AM
    • Unmarked as answer by Priya Gill Thursday, November 19, 2009 8:21 AM
    • Marked as answer by Priya Gill Thursday, November 19, 2009 8:28 AM
    Thursday, November 19, 2009 5:03 AM
  • hi, i reworked the whole project and then correct some of the code in two of the buttons; you would see that the logic remains the same except for some corrections here and there. i have used listview and i am building the columns at runtime. have a look at the code below; let me know if there are any issues.

            private void btnAdd_Click(object sender, EventArgs e)
            {
                if ((double.Parse(txtMaleHeight.Text)) < (double.Parse(txtFeHeight.Text)))
                {
                    MessageBox.Show("Male sld have heigher value", "Invalid");
                }
                else
                {
                    Height.Add(new object[]{double.Parse(txtMaleHeight.Text),double.Parse(txtFeHeight.Text)});
                }
                txtFeHeight.Clear();
                txtMaleHeight.Clear();
                txtFeHeight.Focus();
            }
    
            private void btnCalculate_Click(object sender, EventArgs e)
            {
                // Set to Details view.
                this.lstShow.View = View.Details;
    
                // Add columns using the ColHeader class. The fourth
                // parameter specifies true for an ascending sort order.
                lstShow.Columns.Add("Male Weight", -2, HorizontalAlignment.Left);
                lstShow.Columns.Add("Female Weight", -2, HorizontalAlignment.Left);
                lstShow.Columns.Add("Ideal Male Weight", -2, HorizontalAlignment.Left);
                lstShow.Columns.Add("Ideal Female Weight", -2, HorizontalAlignment.Left);         
    
                foreach(Object obj in Height)
                {
                    string maleLBS = ((object[])(obj))[0].ToString();
                    string femaleLBS = ((object[])(obj))[1].ToString();
                    string idealMaleLBS = Convert.ToString((4.0 * Convert.ToDouble(maleLBS)) - 128);
                    string idealFemaleLBS = Convert.ToString((3.5 * Convert.ToDouble(maleLBS)) - 108);
                    // Add the data.
                    lstShow.Items.Add(new ListViewItem(new string[] { maleLBS, femaleLBS, idealMaleLBS, idealFemaleLBS }));
                }
            }
    


    Balaji Baskar [Please mark the post as answer if it answers your question]
    • Marked as answer by Priya Gill Thursday, November 19, 2009 7:45 AM
    • Unmarked as answer by Priya Gill Thursday, November 19, 2009 8:21 AM
    • Unmarked as answer by Priya Gill Thursday, November 19, 2009 8:21 AM
    • Marked as answer by Priya Gill Thursday, November 19, 2009 8:28 AM
    Thursday, November 19, 2009 6:09 AM

All replies

  • Hi,
    This is not a forum for C# related Post anyway.i done some changes in your code.hope it helps.
      private void btnCalculate_Click(object sender, EventArgs e)
        {
            int i;
            int count = Height.Count;
           double[][] Heights ={ new double[2],new double[count]};
            for (i = 0; i < Height.Count; i++)
            {
                Heights[0][i] =3.5 * inputfe - 108;
    
                Heights[1][i] = 4.0 * inputme - 128;
    
                ListViewItem which;
    
                which = new ListViewItem(Heights[0][i].ToString());
    
                which.SubItems.Add(Heights[1][i].ToString());
    
                lstShow.Items.Add(which);
    
            }
    
            Height.Clear();
        }



    Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
    • Marked as answer by Priya Gill Thursday, November 19, 2009 7:34 AM
    • Unmarked as answer by Priya Gill Thursday, November 19, 2009 8:21 AM
    • Unmarked as answer by Priya Gill Thursday, November 19, 2009 8:21 AM
    • Marked as answer by Priya Gill Thursday, November 19, 2009 8:28 AM
    Thursday, November 19, 2009 5:03 AM
  • hi, i reworked the whole project and then correct some of the code in two of the buttons; you would see that the logic remains the same except for some corrections here and there. i have used listview and i am building the columns at runtime. have a look at the code below; let me know if there are any issues.

            private void btnAdd_Click(object sender, EventArgs e)
            {
                if ((double.Parse(txtMaleHeight.Text)) < (double.Parse(txtFeHeight.Text)))
                {
                    MessageBox.Show("Male sld have heigher value", "Invalid");
                }
                else
                {
                    Height.Add(new object[]{double.Parse(txtMaleHeight.Text),double.Parse(txtFeHeight.Text)});
                }
                txtFeHeight.Clear();
                txtMaleHeight.Clear();
                txtFeHeight.Focus();
            }
    
            private void btnCalculate_Click(object sender, EventArgs e)
            {
                // Set to Details view.
                this.lstShow.View = View.Details;
    
                // Add columns using the ColHeader class. The fourth
                // parameter specifies true for an ascending sort order.
                lstShow.Columns.Add("Male Weight", -2, HorizontalAlignment.Left);
                lstShow.Columns.Add("Female Weight", -2, HorizontalAlignment.Left);
                lstShow.Columns.Add("Ideal Male Weight", -2, HorizontalAlignment.Left);
                lstShow.Columns.Add("Ideal Female Weight", -2, HorizontalAlignment.Left);         
    
                foreach(Object obj in Height)
                {
                    string maleLBS = ((object[])(obj))[0].ToString();
                    string femaleLBS = ((object[])(obj))[1].ToString();
                    string idealMaleLBS = Convert.ToString((4.0 * Convert.ToDouble(maleLBS)) - 128);
                    string idealFemaleLBS = Convert.ToString((3.5 * Convert.ToDouble(maleLBS)) - 108);
                    // Add the data.
                    lstShow.Items.Add(new ListViewItem(new string[] { maleLBS, femaleLBS, idealMaleLBS, idealFemaleLBS }));
                }
            }
    


    Balaji Baskar [Please mark the post as answer if it answers your question]
    • Marked as answer by Priya Gill Thursday, November 19, 2009 7:45 AM
    • Unmarked as answer by Priya Gill Thursday, November 19, 2009 8:21 AM
    • Unmarked as answer by Priya Gill Thursday, November 19, 2009 8:21 AM
    • Marked as answer by Priya Gill Thursday, November 19, 2009 8:28 AM
    Thursday, November 19, 2009 6:09 AM
  • Gnanadurai,

    If you see sombody is abusing the forum, then you can always give the answer in Visual Basic.

    Now you are abusing the MSDN forums in my idea in a very big way.


    Success
    Cor
    Thursday, November 19, 2009 6:11 AM
  • Balaji,

    Stop using this old way of programming from the previous century.

    You are using old VB prefix style in C#. So you could have done it in VB the same without abusing this forum.

    Beside that has Visual Basic extra features for a kind of compatibility with old VB programs (the Redim for Arrays).


    The main answer can only be use generic list and not anymore an multi column array.



     

    Success
    Cor
    Thursday, November 19, 2009 6:19 AM
  • Stop using this old way of programming from the previous century.

    You are using old VB prefix style in C#. So you could have done it in VB the same without abusing this forum.

    Beside that has Visual Basic extra features for a kind of compatibility with old VB programs (the Redim for Arrays).


    The main answer can only be use generic list and not anymore an multi column array.
    Cor.. thanks for your suggestion, i just meant to correct the program already written by the user. If i was to write my own i would have implement all new way of programming styles from this century.

    Balaji Baskar [Please mark the post as answer if it answers your question]
    Thursday, November 19, 2009 6:34 AM
  • Sorry,I didn't know it was a VB forum.
    I'm new to coding,just wanted to know whether whatever i have posted ie my code,does it look like an old century code? If that is the case, can any one tell me how does new century code look? Sorry i didn't mean to be rude.
    Thursday, November 19, 2009 8:18 AM
  • hi Priya,

    The other way of implementing this test project is by using Generics.
    Refer: http://msdn.microsoft.com/en-us/library/ms379608(VS.80).aspx

    i have listed an example in VB.NET version.

    create a class with necessary properties, see sample given below:

    Public Class WeightManagement
    
        Private _maleLBS As Double
        Private _femaleLBS As Double
        Private _idealMaleLBS As Double
        Private _idealFemaleLBS As Double
    
        Public Property MaleLBS() As Double
            Get
                Return _maleLBS
            End Get
            Set(ByVal value As Double)
                _maleLBS = value
            End Set
        End Property
    
        Public Property FemaleLBS() As Double
            Get
                Return _femaleLBS
            End Get
            Set(ByVal value As Double)
                _femaleLBS = value
            End Set
        End Property
    
        Public ReadOnly Property IdealMaleLBS() As Double
            Get
                Return _idealMaleLBS
            End Get
        End Property
    
        Public ReadOnly Property IdealFemaleLBS() As Double
            Get
                Return _idealFemaleLBS
            End Get
        End Property
    
        Public Sub New(ByVal _maleLBS As Double, ByVal _femaleLBS As Double)
            Me._maleLBS = _maleLBS
            Me._femaleLBS = _femaleLBS
            Me._idealMaleLBS = (4.0 * _maleLBS) - 128
            Me._idealFemaleLBS = (3.5 * _femaleLBS) - 108
        End Sub
    
    End Class
    
    

    Now the code for your main project is listed below:

    Imports System.IO
    Imports System.Collections.Generic
    
    Public Class Form1
    
        Dim weightMgmt As New System.Collections.Generic.List(Of WeightManagement)
    
        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
            Try
                ' Set to Details view.
                Me.lstShow.View = View.Details
                ' Add columns using the ColHeader class. The fourth
                ' parameter specifies true for an ascending sort order.
                Me.lstShow.Columns.Add("Male Weight", -2, HorizontalAlignment.Left)
                Me.lstShow.Columns.Add("Female Weight", -2, HorizontalAlignment.Left)
                Me.lstShow.Columns.Add("Ideal Male Weight", -2, HorizontalAlignment.Left)
                Me.lstShow.Columns.Add("Ideal Female Weight", -2, HorizontalAlignment.Left)
                ' Populate the List View
                Dim wm As WeightManagement
                For Each wm In weightMgmt
                    ' Add the Data
                    Me.lstShow.Items.Add(New ListViewItem(New String() {CStr(wm.MaleLBS), CStr(wm.FemaleLBS), CStr(wm.IdealMaleLBS), CStr(wm.IdealFemaleLBS)}))
                Next
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error")
            End Try
        End Sub
    
        Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
            Try
                If ((Double.Parse(txtMaleHeight.Text) < (Double.Parse(txtFeHeight.Text)))) Then
                    MessageBox.Show("Male sld have heigher value", "Invalid")
                Else
                    weightMgmt.Add(New WeightManagement(Double.Parse(txtMaleHeight.Text), Double.Parse(txtFeHeight.Text)))
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error")
            Finally
                txtFeHeight.Clear()
                txtMaleHeight.Clear()
                txtFeHeight.Focus()
            End Try
        End Sub
    End Class
    
    



    have a good learning!


    Balaji Baskar [Please mark the post as answer if it answers your question]
    Friday, November 20, 2009 3:58 PM