【问题标题】:Altering string array value with radio button使用单选按钮更改字符串数组值
【发布时间】:2014-07-07 10:14:07
【问题描述】:

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

基本上所有需要发生的事情是如果选择了名为“smallCarRadBtn”的单选按钮,那么调用“carSize”的字符串数组必须包含“Small”一词,其他两个单选按钮“medCarRadBtn”和“大汽车RadBtn”。

此刻它告诉我:

"不能将类型'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());
            }
        }

    }
}

【问题讨论】:

  • 您在哪一行收到此错误?
  • 星号之间:carSize = ("small"[entryCount]); carSize = ("中"[entryCount]);和 carSize = ("Large"[entryCount]);

标签: c# arrays string char implicit-conversion


【解决方案1】:

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

carSize = ("small"[entryCount]);

这里"small"是一个字符串"small"[entryCount]返回索引处的字符entryCount

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

carSize[index] = "small" + entryCount;

或者如果你只想设置carSize[entryCount]那么:

carSize[entryCount] = "small";

【讨论】:

  • 非常感谢,您为我节省了很多时间,我刚刚做了 carSize[entryCount] = "small";其他两个单选按钮也是如此。传奇!
猜你喜欢
  • 1970-01-01
  • 2016-01-14
  • 1970-01-01
  • 2010-11-22
  • 1970-01-01
  • 1970-01-01
  • 2019-01-18
  • 2020-05-17
  • 1970-01-01
相关资源
最近更新 更多