Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I put some values in it in run time, but if user put less then 10 values like if user put 5 values in it instead of 10. After that when i display the array first it's show the five value and after that start displaying 0 0 0 0 0. I just want to know is there any method to get rid of using dynamic array in c#?


What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace hassan_algo
{
    public partial class Input_Form : Form
    {
        int min = 0, max = 0, step = 0;
       
        double[] array = new double[10];
     
       /* double[] array = { };//notice I did not declare a size for the array
        List<double> tmpList = new List<double>(); */
        int i = 0;

        public Input_Form()
        {
            InitializeComponent();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void exit_Click(object sender, EventArgs e)
        {
            Environment.Exit(0); 
        }

        private void clear_Click(object sender, EventArgs e)
        {
            textbox_min.Clear();
            textbox_max.Clear();
            textbox_step.Clear();
            textbox_min.Focus(); 
        }

        private void execute_Click(object sender, EventArgs e)
        {
            calculat();
        }

        public int calculat()
        {

            if (String.IsNullOrEmpty(textbox_min.Text) || String.IsNullOrWhiteSpace(textbox_max.Text) || String.IsNullOrWhiteSpace(textbox_step.Text))
            {
                MessageBox.Show("Please fill the textbox");
            }
            else
            {
                min = Convert.ToInt32(textbox_min.Text);
                max = Convert.ToInt32(textbox_max.Text);
                step = Convert.ToInt32(textbox_step.Text);
                while (min <= max)
                {
                    array[i] = min;
                    min = min + step;
                    i++;
                }
                string output = string.Empty;
                foreach (var item in array)
                {
                    output += item + "\n";
                }

                MessageBox.Show(output);
            }

         
            return 0; 
        }
    }
}
Posted
Updated 21-Dec-17 10:01am

1 solution

When declaring an array ion C# you must specify a size. This determines the number of elements in the array.
Each element is initialised with the default value of the element type - hence if you create an array of type double with 10 elements then all elements are set to 0.
Arrays cannot be re-sized in C#, you need to create a new array & copy your elements to the new array.
The typical method of working around this is to use something that can be dynamically sized and then copying to an array if required for instance;
C#
// Create a List of type double - Lists can be initialised with no size
List<double> myList = new List<double>();
// add elements to the list - example only
for(double i = 0D; i < 10D; i++)
{
    myList.Add(i);
}
// create an array of type double from the list object
double[] myArray = myList.ToArray();


Kind Regards
 
Share this answer
 

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