将字符串从组合框转换为类变量[C#]时出现问题

时间:2018-11-19 16:26:49

标签: c# wpf

按钮功能应将组合框中的所有文本放置在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;

之前

0 个答案:

没有答案