首页 > 解决方案 > 将字符串从组合框中获取到类变量中的问题 [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;

标签: c#wpf

解决方案


推荐阅读