嵌套三元隐式类型转换问题:无法确定条件表达式的类型,因为

时间:2015-12-03 20:30:32

标签: c# ternary-operator

我过去经常使用嵌套的三元组c#。在我介绍了一个事件处理程序后,作为赋值操作,我看到了错误:"无法确定条件表达式的类型,因为' void'之间没有隐式转换。和' void'"

以下是我正在编写的代码的两个片段,最上面的一个是功能性的,底部的代码提供了类型转换错误:

   AppTableConfigCollection collection = 
            tableType == TableType.Attribute
                ? _attributeTableConfigs = new AppTableConfigCollection(_db, AppTableConfigCollection.ATTRIBUTE) 
                    : tableType == TableType.Core
                        ? _coreTableConfigs = new AppTableConfigCollection(_db, AppTableConfigCollection.CORE)
                        : tableType == TableType.Domain
                            ?_domainTableConfigs = new AppTableConfigCollection(_db, AppTableConfigCollection.DOMAIN)
                            : tableType == TableType.Configuration
                                ?_configTableConfigs = new AppTableConfigCollection(_db, AppTableConfigCollection.CONFIG)
                                    :_offlineTableConfigs = new AppTableConfigCollection(_db, AppTableConfigCollection.OFFLINE);





        PropertyChangedEventHandler property =
            tableType == TableType.Attribute
                ? _attributeTableConfigs.PropertyChanged += new PropertyChangedEventHandler(appTableConfigs_PropertyChanged)
                : tableType == TableType.Core
                    ? _coreTableConfigs.PropertyChanged += new PropertyChangedEventHandler(appTableConfigs_PropertyChanged)
                    : tableType == TableType.Domain
                        ? _domainTableConfigs.PropertyChanged += new PropertyChangedEventHandler(appTableConfigs_PropertyChanged)
                        : tableType == TableType.Configuration
                            ? _configTableConfigs.PropertyChanged += new PropertyChangedEventHandler(appTableConfigs_PropertyChanged)
                                : _offlineTableConfigs.PropertyChanged += new PropertyChangedEventHandler(appTableConfigs_PropertyChanged);

感谢您提供的任何帮助!

2 个答案:

答案 0 :(得分:5)

首先,请听听评论者。切换/案例看起来确实会产生更易读,可维护的代码。

其次,您在第二个代码段中尝试执行的操作是将属性更改处理程序设置为订阅处理程序的结果,这是无效的。

具体来说,_attributeTableConfigs.PropertyChanged += new PropertyChangedEventHandler(appTableConfigs_PropertyChanged)不返回值 - 它返回void。所以,你的任务是不可能的。

你必须按照不同的步骤做你想做的事。

例如:

PropertyChangedEventHandler handler;
switch (tableType)
{
    case TableType.Attribute:
        handler = new PropertyChangedEventHandler(appTableConfigs_PropertyChanged);
        _attributeTableConfigs.PropertyChanged += handler;
        break;
    case TableType.Core:
        handler = new PropertyChangedEventHandler(appTableConfigs_PropertyChanged);
        _coreTableConfigs.PropertyChanged += handler;
        break;
    case TableType.Domain:
        handler = new PropertyChangedEventHandler(appTableConfigs_PropertyChanged);
        _domainTableConfigs.PropertyChanged += handler;
        break;
    case TableType.Configuration:
        handler = new PropertyChangedEventHandler(appTableConfigs_PropertyChanged);
        _configTableConfigs.PropertyChanged += handler;
        break;
    default:
        handler = new PropertyChangedEventHandler(appTableConfigs_PropertyChanged);
        _offlineTableConfigs.PropertyChanged += handler;
        break;
}

或者,如果处理程序始终相同:

PropertyChangedEventHandler handler = appTableConfigs_PropertyChanged;
switch (tableType)
{
    case TableType.Attribute:
        _attributeTableConfigs.PropertyChanged += handler;
        break;
    case TableType.Core:
        _coreTableConfigs.PropertyChanged += handler;
        break;
    case TableType.Domain:
        _domainTableConfigs.PropertyChanged += handler;
        break;
    case TableType.Configuration:
        _configTableConfigs.PropertyChanged += handler;
        break;
    default:
        _offlineTableConfigs.PropertyChanged += handler;
        break;
}

答案 1 :(得分:4)

我建议使用switch语句,因为它更容易阅读和更新

switch (tableType)
        {
            case TableType.Attribute:
                _attributeTableConfigs = new AppTableConfigCollection(_db, AppTableConfigCollection.ATTRIBUTE);
                _attributeTableConfigs.PropertyChanged += new PropertyChangedEventHandler(appTableConfigs_PropertyChanged);
                break;
            case TableType.Core:
                _coreTableConfigs = new AppTableConfigCollection(_db, AppTableConfigCollection.CORE);
                _coreTableConfigs.PropertyChanged += new PropertyChangedEventHandler(appTableConfigs_PropertyChanged)
                break;
            case TableType.Domain:
                _domainTableConfigs = new AppTableConfigCollection(_db, AppTableConfigCollection.DOMAIN);
                _domainTableConfigs.PropertyChanged += new PropertyChangedEventHandler(appTableConfigs_PropertyChanged)
                break;
            case TableType.Configuration:
                _configTableConfigs = new AppTableConfigCollection(_db, AppTableConfigCollection.CONFIG);
                _configTableConfigs.PropertyChanged += new PropertyChangedEventHandler(appTableConfigs_PropertyChanged)
                break;
            default:
                _offlineTableConfigs = new AppTableConfigCollection(_db, AppTableConfigCollection.OFFLIINE);
                _offlineTableConfigs.PropertyChanged += new PropertyChangedEventHandler(appTableConfigs_PropertyChanged);
                break;
        }