我过去常常使用嵌套的三元组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);
感谢您提供的任何帮助!
首先,请听听评论者.切换/案例看起来确实会产生更易读,可维护的代码.
第二,你在第二个代码片段中尝试做的是将属性更改处理程序设置为订阅处理程序的结果,这是无效的.
具体来说,_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; }