使用单选按钮修改字符串数组值
本文关键字:数组 字符串 修改 单选按钮 | 更新日期: 2023-09-27 18:11:39
我遇到了一个问题,根据选择的单选按钮改变字符串数组的值,它是用c#使用Visual Studio 2013专业版编写的。
基本上所有需要发生的是,如果单选按钮被称为"smallCarRadBtn"被选中,那么字符串数组调用"carSize"必须容纳单词"Small",同样对于其他两个单选按钮"medCarRadBtn"answers"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());
}
}
}
}
carSize
是一个字符串数组,但您试图将其分配给char
:
carSize = ("small"[entryCount]);
这里"small"
是一个字符串, "small"[entryCount]
返回索引entryCount
如果你想存储字符,你应该将carSize
更改为char[]
,并使用索引器设置元素,而不是直接分配数组。或者如果你想存储text + entryCount
,那么你应该连接字符串:
carSize[index] = "small" + entryCount;
或如果您只想设置carSize[entryCount]
,则:
carSize[entryCount] = "small";