c# - 将字符串从组合框中获取到类变量中的问题 [C#]
问题描述
按钮功能应采用组合框中的任何文本并将其放在 sleeper.traintype 中
private void Btn_Apply_Click(object sender, RoutedEventArgs e)
{
try
{
sleeper.trainType = CmbBox_TrainType.Text;
if (CmbBox_TrainType.Text == "Sleeper")
{
// instantiate the sleeper train
sleeper.trainType = CmbBox_TrainType.Text;
}
}
我的卧铺火车班(继承自整个火车班)
public class Sleeper : Train
{
private string _intermediate, _intermediate1, _intermediate2, _intermediate3;
private bool _cabin;
public string intermediate
{
get
{
return _intermediate;
}
set
{
_intermediate = value;
}
}
public string intermediate1
{
get
{
return _intermediate1;
}
set
{
_intermediate1 = value;
}
}
public string intermediate2
{
get
{
return _intermediate2;
}
set
{
_intermediate2 = value;
}
}
public string intermediate3
{
get
{
return _intermediate3;
}
set
{
_intermediate3 = value;
}
}
火车班:
public class Train
{
private string _trainID, _departureDay, _departureStation, _destinationStation, _departureTime, _trainType;
private bool _firstClass;
public string timePunctuation = ":";
public string dayPunctuation = "/";
public string trainID
{
get
{
return _trainID;
}
set
{
// check if the vlaue has letters & numbers and that the length is correct
if(value.Length == 4 && Regex.IsMatch(value, "[A-Z][0-9]"))
{
_trainID = value;
}
else
{
throw new FormatException("That train ID is not valid! (Example: AA11)");
}
}
}
public string departureDay
{
get
{
return _departureDay;
}
set
{
if(value.Length == 0)
{
throw new FormatException("You need to choose a departure day!");
} else
{
_departureDay = value;
}
}
}
public string departureTime
{
get
{
return _departureTime;
}
set
{
if(value.Length != 5 || value.Contains(timePunctuation) == false)
{
throw new FormatException("The time must be in this format: (11:11 or 03:22)");
} else
{
_departureTime = value;
}
}
}
public string departureStation
{
get
{
return _departureStation;
}
set
{
if(value.Length == 0)
{
throw new FormatException("You must enter a departure station!");
} else
{
_departureStation = value;
}
}
}
public string destinationStation
{
get
{
return _destinationStation;
}
set
{
if(value.Length == 0)
{
throw new FormatException("You must enter a destination!");
} else
{
_departureStation = value;
}
}
}
public string trainType
{
get
{
return _trainType;
}
set
{
value = _trainType;
}
}
}
我正在使用具有三个选项"Sleeper"
和"Stopping"
“Express”的组合框。当在它旁边使用断点时,sleeper.trainType = CmbBox_TrainType.Text;
它会创建我的类,但声明我的sleeper.trainType
变量是null
. 但它说
CmbBox_TrainType = "卧铺"
在开始时实例化睡眠者,Sleeper sleeper = new Sleeper();
但也尝试将其放入if
和之前sleeper.trainType = CmbBox_TrainType.Text;
解决方案
推荐阅读
- python - 熊猫计算行中字符串并发的最快方法(句子中的单词)
- hadoop - Hive:有什么方法可以提前预测 Parquet 存储表的大小?
- php - oci_fetch_array 未返回预期值
- w3c - 如何解决此错误 - 信息:检测到的方言:W3C
- php - 雄辩的保存数据而不进行强制转换以避免绕过“有效负载无效”。
- python - 从 tkinter 按钮按下创建事件日志列表
- wordpress - 在 Wordpress 多站点的子站点文件夹中创建一个文件夹
- azure - Azure Logic App For Each 循环每次发送多封电子邮件只想发送一封电子邮件
- r - 按不同组划分 geom_col 很热?
- python - Python Panda 的行选择