使用单选按钮更改字符串数组值

时间:2014-07-07 10:14:08

标签: c# arrays string char implicit-conversion

我遇到了根据选择的单选按钮更改字符串数组值的问题,它是使用Visual Studio 2013专业人员用C#编写的。

基本上所需要发生的是,如果选择了名为“smallCarRadBtn”的单选按钮,则字符串数组调用“carSize”必须保持单词“Small”,同样对于其他两个单选按钮“medCarRadBtn”和“largeCarRadBtn”。

目前它告诉我:

  

“无法将类型'char'隐式转换为'string []'

我用'星号'''突出显示了包含此代码的区域。任何帮助将不胜感激。

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;
using System.IO;

namespace Assignment2
{
    public partial class Form1 : Form
    {
        TimeSpan daysHiredIn;
        DateTime startDate, endDate;
        DateTime dateToday = DateTime.Today;
        public static string[] names = new string[50];
        public static string[] carSize = new string[50];
        public static int[] cardNumb = new int[50];
        public static int[] cost = new int[50];       
        public static TimeSpan[] daysHired = new TimeSpan[50];
        public static int entryCount = 0, cardNumbIn, carFee = 45, intDaysHired;
        public Form1()
        {
            InitializeComponent();
            smallCarRadBtn.Checked = true;
        }

        private void confirmBtn_Click(object sender, EventArgs e)
        {
            if (entryCount >= 50)
            {
                MessageBox.Show("Arrays are Full");//if array is full
            }
            else if (nameTxtBox.Text == "")
            {
                MessageBox.Show("You must enter a name");//Nothing entered
            }
            else if (!int.TryParse(cardNumbTxtBox.Text, out cardNumbIn))
            {
                MessageBox.Show("You must enter an integer number");
                cardNumbTxtBox.SelectAll();
                cardNumbTxtBox.Focus();
                return;
            }
            else if (hireStartDatePicker.Value < dateToday)
            {
                MessageBox.Show("You cannot enter a date earlier than today");
            }
            else if (hireEndDatePicker.Value < dateToday)
            {
                MessageBox.Show("You cannot enter a date earlier than today");
            }
            else
            {

*******************************************************************************************

                if (smallCarRadBtn.Checked)
                {
                    carSize = ("small"[entryCount]);
                }
                else if (MedCarRadBtn.Checked)
                {
                    carSize = ("Medium"[entryCount]);
                }
                else if (largeCarRadBtn.Checked)
                {
                    carSize = ("Large"[entryCount]);
                }   

*******************************************************************************************

                names[entryCount] = nameTxtBox.Text;
                cardNumb[entryCount] = cardNumbIn;
                endDate = (hireEndDatePicker.Value);
                startDate = (hireStartDatePicker.Value);
                daysHiredIn = (endDate - startDate);
                cost[entryCount] = (carFee * daysHiredIn);
                daysHired[entryCount] = daysHiredIn;
                entryCount++;
                nameTxtBox.SelectAll();
                nameTxtBox.Focus();
                }
            }


        private void viewBtn_Click(object sender, EventArgs e)
        {
            for (entryCount = 0; entryCount < 50; entryCount++)
            {
                listBox1.Items.Add(names[entryCount]+"\t"+daysHired[entryCount].Days.ToString());
            }
        }

    }
}

1 个答案:

答案 0 :(得分:1)

carSize是一个字符串数组,但您尝试为其分配char

carSize = ("small"[entryCount]);

此处"small"字符串"small"[entryCount]返回索引entryCount处的字符

如果要存储字符,则应将carSize更改为char[],并使用索引器设置元素,而不是直接分配数组。或者如果你想存储text + entryCount,那么你应该连接字符串:

carSize[index] = "small" + entryCount;

如果您只想设置carSize[entryCount]

carSize[entryCount] = "small";