阅读背景:

C#空数组不接受输入

来源:互联网 

What I'm attempting to do is enter data into the empty arrays using a form textbox. The form will display the first array(values are already entered), but it will not accept the values that entering into the textbox. No matter what value I enter, it posts

What I'm attempting to do is enter data into the empty arrays using a form textbox. The form will display the first array(values are already entered), but it will not accept the values that entering into the textbox. No matter what value I enter, it posts $0.00 in the whole row. It won't even take in a name(string). Can anyone guide me in the right direction.

我正在尝试做的是使用表单文本框将数据输入到空数组中。表单将显示第一个数组(已输入值),但不接受进入文本框的值。无论我输入什么价值,它都会在整行中发布0.00美元。它甚至不会采用名称(字符串)。任何人都可以指导我正确的方向。

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

namespace WP_Week3_Part1
{
    public partial class ArrayForm : Form
    {
        int[] deptArray = new int[5] { 1, 2, 3, 4, 5 };
        double[] contArray = new double[5] { 0, 0, 0, 0, 0};
        string[] nameArray = new string[5] { "", "", "", "", "" };
        public ArrayForm()
        {
            InitializeComponent();
        }

        private void buttonValue_Click(object sender, EventArgs e)
        {
            int dept;
            dept = Convert.ToInt32(textBoxDept.Text);
            double cont;
            cont = Convert.ToDouble(textBoxCont.Text);
            textBoxDept.Text = "";
            textBoxCont.Text = "";
            textBoxDonor.Text = "";
            labelOutput.Text = "Printing Contribution Table \n dept          amount          donor";
            for (int i = 0; i < deptArray.Length; i = i + 1)
            {
                nameArray[i] = textBoxDonor.Text;
                labelOutput.Text += String.Format("\n {0}          {1}          {2} ", deptArray[i], contArray[i].ToString("C"), nameArray[i]);
            }

        }
    }
}

2 个解决方案

#1


1  

You have two problems:

你有两个问题:

  1. You get the cont value and then do nothing with it. You print contArray[i] but that is not assigned after being initialized to 0, so of course it always prints 0. Not sure what the logic is supposed to be based on your code, but either print cont or assign the array member(s).

    你获得了cont值,然后不做任何事情。你打印contArray [i]但是在初始化为0后没有分配,所以当然它总是打印0.不确定根据你的代码应该是什么逻辑,但是打印cont或分配数组成员(s )。

  2. You clear the donor text box (textBoxDoneor.Text = "") before using it in the for loop. So when you assign the element of nameArray, you are always assigning the empty string. You also always assign every member of the array, not sure if that is intentional or you just haven't gotten that far with your program yet.

    您在for循环中使用之前清除了供体文本框(textBoxDoneor.Text =“”)。因此,当您指定nameArray元素时,您始终指定空字符串。你也总是分配数组的每个成员,不确定这是否是有意的,或者你还没有完成你的程序。

#2


1  

It returns $0.00 because:

它返回0.00美元,因为:

You set your text textBoxDonor to empty

您将文本textBoxDonor设置为空

it should be like this:

它应该是这样的:

            int dept;
            dept = Convert.ToInt32(textBoxDept.Text);
            double cont;
            cont = Convert.ToDouble(textBoxCont.Text);
            //textBoxDept.Text = "";
            //textBoxCont.Text = "";
            //textBoxDonor.Text = "";
            labelOutput.Text = "Printing Contribution Table \n dept          amount          donor";
            for (int i = 0; i < deptArray.Length; i = i + 1)
            {
                nameArray[i] = textBoxDonor.Text;
                labelOutput.Text += String.Format("\n {0}          {1}          {2} ", deptArray[i], contArray[i].ToString("C"), nameArray[i]);


     }

Checking the content of your nameArray right after the execution

执行后立即检查nameArray的内容

        string arrlist = string.Empty;
        foreach (var arr in nameArray.ToList())
        { 
           arrlist = arrlist +' ' + arr.ToString();
        }
        MessageBox.Show(arrlist);

.00 in the whole row. It won't even take in a name(string). Can anyone guide me in the right direction.What I'm attempting to do is enter data into th




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: