用对象变量切换案例问题 [英] switch case problem with object variable
问题描述
嗨我有一个问题,这个代码我从vb转换为c#当我转换我发现这个错误可以任何人帮助我:
这里是我的方法我在这里声明KEY作为一个对象,我想使用switch case但它给我的错误,我应该声明只有int或bool或字符串,但不是对象:一个开关表达式或案例标签必须是一个布尔, char,string,integral,enum或相应的可空类型
i仍然是将代码从vb转换为c#的新方法我需要一个小帮助
static private bool validateSiteAddressDetails( string sCountry, long sNr, string sNrAlf, long sPC, string sPhone, string sEm ail, string sFax, string sStreet, string sCity,Hashtable ht_AddressDetails)
{
long rv = 0 ;
rv = 0 ;
object key = null ;
foreach ( object key_loopVariable in ht_AddressDetails.Keys){
key = key_loopVariable;
switch (key){
case city_text:
if (sCity!= ht_AddressDetails(key) ).ToString()){
rv = -1;
key = 215 ;
}
break ;
case street_text :
if (sStreet!= ht_AddressDetails(key).ToString()){
rv = -1;
key = 215 ;
}
break ;
case house_nr :
if (sNr!= Convert.ToInt32(ht_AddressDetails(key).ToString())){
rv = -1;
key = 215 ;
}
break ;
case house_alf :
if (sNrAlf!= ht_AddressDetails(key).ToString()){
rv = -1;
key = 215 ;
}
break ;
case contact_phone :
if (sPhone!= ht_AddressDetails(key).ToString()){
rv = -1;
key = 215 ;
}
break ;
case contact_email :
if (sEmail!= ht_AddressDetails(key).ToString()){
rv = -1;
key = 215 ;
}
break ;
case contact_fax :
if (sFax!= ht_AddressDetails(key).ToString()){
rv = -1;
key = 215 ;
}
break ;
case country :
if (sCountry!= ht_AddressDetails(key).ToString()){
rv = -1;
key = 215 ;
}
break ;
case postal_code :
if (sPC!= Convert.ToInt32(ht_AddressDetails(key).ToString())){
rv = -1;
key = 215 ;
}
break ;
}
}
key = 215 ;
return Convert.ToBoolean(rv);
}
这是你的代码:
< pre lang = xml> ; Private 函数 validateSiteAddressDetails( ByVal sCountry As 字符串,_
ByVal sNr As 长, ByVal sNrAlf 作为 < span class =code-keyword> String , ByVal sPC As 长,_
ByVal sPhone As String , ByVal sEmail 作为 字符串, ByVal sFax 作为 字符串, ByVal sStreet 作为 字符串, ByVal sCity 作为 字符串,_
ByVal ht_AddressDetails As Hashtable) As 布尔
Dim rv 作为 很长
rv = 0
Dim key As 对象
对于 每个 key 在 ht_AddressDetails.Keys
选择 案例键
案例 city_text
如果 sCity<> ht_AddressDetails(key).ToString()然后
rv = -1
GoTo 215
结束 如果
案例 street_text
如果 sStreet<> ht_AddressDetails(key).ToString()然后
rv = -1
GoTo 215
结束 如果
案例 house_nr
如果 sNr<> Convert.ToInt32(ht_AddressDetails(key).ToString())然后
rv = -1
GoTo 215
结束 如果
案例 house_alf
如果 sNrAlf<> ht_AddressDetails(key).ToString()然后
rv = -1
GoTo 215
结束 如果
案例 contact_phone
如果 sPhone<> ht_AddressDetails(key).ToString()然后
rv = -1
GoTo 215
结束 如果
案例 contact_email
如果 sEmail<> ht_AddressDetails(key).ToString()然后
rv = -1
GoTo 215
结束 如果
案例 contact_fax
如果 sFax<> ht_AddressDetails(key).ToString()然后
rv = -1
GoTo 215
结束 如果
案例 country
如果 sCountry<> ht_AddressDetails(key).ToString()然后
rv = -1
GoTo 215
结束 如果
案例 postal_code
如果 sPC<> Convert.ToInt32(ht_AddressDetails(key).ToString())然后
rv = -1
GoTo 215
结束 如果
结束 选择
下一步
215:
返回 rv
结束 功能
确定。顺序:
第一:为什么要换开?为每个字段分别设置验证功能。或者,有一系列的elseifs(因为你已经在开关盒内有ifs。
第二:GoTos在上个世纪90年代过时了。
第三:你在ht_AddresssDetails(key)中使用.ToString - 显然你知道这个方法。为什么不在密钥上使用它?为什么不把密钥当作字符串呢?你也可以试试对于每个键作为字符串来自...它将为你调用.ToString对象。
如果这有帮助请花时间接受解决方案。
实际上你正在从你的has-table访问value,所以你需要先检查你是如何创建表的,因为has-table接受所有像'Key'和'Value'这样的东西作为对象数据类型,这意味着你可以添加值组合,如key是整数类型,value是string,对于key是字符串的类型,value是整数,你可以创建自己的与任何原始数据类型以及用户定义dat的组合也是一种类型。
所以在将代码从vb转换为c#时,你需要手动进行一些更改,就像你的代码需要一样。
所以请检查你的ht_AddressDetails
有表,如果它具有字符串数据类型的键,那么使用声明为字符串的'key'变量而不是对象类型。
并对你的代码进行适当的修改......
string key = string .Empty;
bool rv = true ;
foreach ( string key_loopVariable in ht_AddressDetails.Keys){
key = key_loopVariable;
switch (key){
case city_text:
if (sCity!= Convert.ToString (ht_AddressDetails [key])){
rv = false ;
}
break ;
}
// 您的其他验证就在这里
if (!rv)
break ;
}
我刚刚给出了示例,您需要根据自己的要求以自己的方式修改代码。
看一下这个例子:Hashtable ht = new Hashtable();
private void TestHashTable()
{
ht.Add( what, 12 );
ht.Add( how, 14 跨度>);
foreach ( var key in ht.Keys)
{
switch (key.ToString())
{
< span class =code-keyword> case what
控制台.WriteLine( 它是什么);
break ;
case how :
Console.WriteLine( 这是一个);
break ;
}
}
}HashTable的密钥是对象的ICollection;如果您对所有Keys使用Type String,那么'ToString()会将Key Object恢复为原始的String Type,它可以用作Switch / Case语句的选择器,如上所示。
如果您使用了字典< string,int>对于你的数据结构,你可以直接使用Key而无需使用'ToString进行转换。
HashTable让你逃脱添加Key-Value对是不同的类型对象;字典是强类型的,所有键必须是相同的类型。
Hi I have a problem with this code im converting from vb to c# and when while im converting i found this error can any one help me :
here is my method i declared here the KEY as an object and i want to use the switch case but it gives me that error that i should declare only int or bool or string but not object : "A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type"
i still new for converting code from vb to c# i need a small help
static private bool validateSiteAddressDetails(string sCountry, long sNr, string sNrAlf, long sPC, string sPhone, string sEmail, string sFax, string sStreet, string sCity, Hashtable ht_AddressDetails)
{
long rv = 0;
rv = 0;
object key = null;
foreach (object key_loopVariable in ht_AddressDetails.Keys) {
key = key_loopVariable;
switch ( key) {
case "city_text":
if (sCity != ht_AddressDetails(key).ToString()) {
rv = -1;
key= 215;
}
break;
case "street_text":
if (sStreet != ht_AddressDetails(key).ToString()) {
rv = -1;
key= 215;
}
break;
case "house_nr":
if (sNr != Convert.ToInt32(ht_AddressDetails(key).ToString())) {
rv = -1;
key= 215;
}
break;
case "house_alf":
if (sNrAlf != ht_AddressDetails(key).ToString()) {
rv = -1;
key= 215;
}
break;
case "contact_phone":
if (sPhone != ht_AddressDetails(key).ToString()) {
rv = -1;
key = 215;
}
break;
case "contact_email":
if (sEmail != ht_AddressDetails(key).ToString()) {
rv = -1;
key = 215;
}
break;
case "contact_fax":
if (sFax != ht_AddressDetails(key).ToString()) {
rv = -1;
key = 215;
}
break;
case "country":
if (sCountry != ht_AddressDetails(key).ToString()) {
rv = -1;
key = 215;
}
break;
case "postal_code":
if (sPC != Convert.ToInt32(ht_AddressDetails(key).ToString())) {
rv = -1;
key = 215;
}
break;
}
}
key = 215;
return Convert.ToBoolean(rv);
}
and this is y vb code:
<pre lang="xml">Private Function validateSiteAddressDetails(ByVal sCountry As String, _
ByVal sNr As Long, ByVal sNrAlf As String, ByVal sPC As Long, _
ByVal sPhone As String, ByVal sEmail As String, ByVal sFax As String, ByVal sStreet As String, ByVal sCity As String, _
ByVal ht_AddressDetails As Hashtable) As Boolean
Dim rv As Long
rv = 0
Dim key As Object
For Each key In ht_AddressDetails.Keys
Select Case key
Case "city_text"
If sCity <> ht_AddressDetails(key).ToString() Then
rv = -1
GoTo 215
End If
Case "street_text"
If sStreet <> ht_AddressDetails(key).ToString() Then
rv = -1
GoTo 215
End If
Case "house_nr"
If sNr <> Convert.ToInt32(ht_AddressDetails(key).ToString()) Then
rv = -1
GoTo 215
End If
Case "house_alf"
If sNrAlf <> ht_AddressDetails(key).ToString() Then
rv = -1
GoTo 215
End If
Case "contact_phone"
If sPhone <> ht_AddressDetails(key).ToString() Then
rv = -1
GoTo 215
End If
Case "contact_email"
If sEmail <> ht_AddressDetails(key).ToString() Then
rv = -1
GoTo 215
End If
Case "contact_fax"
If sFax <> ht_AddressDetails(key).ToString() Then
rv = -1
GoTo 215
End If
Case "country"
If sCountry <> ht_AddressDetails(key).ToString() Then
rv = -1
GoTo 215
End If
Case "postal_code"
If sPC <> Convert.ToInt32(ht_AddressDetails(key).ToString()) Then
rv = -1
GoTo 215
End If
End Select
Next
215:
Return rv
End Function
OK. In order:
First: Why have the switch at all? Have separate validating function for each field. Alternatively, have series of elseifs (since you already have ifs within switch case.
Second: GoTos went out of style in the 90s last century.
Third: You use .ToString in ht_AddresssDetails(key) - obviously you know about the method. Why not use it on the key? And why not have key as String? You could also try for each key as string from ... it will call .ToString on the object for you.
If this helps please take time to accept the solution.
Actually you are accessing value from your has-table, so you need to first of check how you has-table has been created, as has-table is accepting all the things like 'Key' and 'Value' as object data type, that means you can add combination of value like key is type of integer and value is string, for key is type of string and value is integer, you can create your own combination with any primitive data type as well as user define data type too.
so while converting your code from vb to c#, you need to make some changes manually as here in your code needs.
so please check yourht_AddressDetails
has table, if it will having key of string data type then use your 'key' variable declared as string instead of object type.
and make appropriate changes to your code like this...
string key = string.Empty; bool rv = true; foreach (string key_loopVariable in ht_AddressDetails.Keys) { key = key_loopVariable; switch ( key) { case "city_text": if (sCity != Convert.ToString(ht_AddressDetails[key])) { rv = false; } break; } //your other validation goes here if(!rv) break; }
I have just given example, you need to modify your code by your own way as per you requirement.
Take a look at this example:Hashtable ht = new Hashtable(); private void TestHashTable() { ht.Add("what", 12); ht.Add("how", 14); foreach (var key in ht.Keys) { switch (key.ToString()) { case "what" Console.WriteLine("it's a what"); break; case "how": Console.WriteLine("it's a how"); break; } } }A HashTable's Keys are an ICollection of Objects; in the case you used Type String for all the Keys, then 'ToString() will case the Key Object back to its original String Type where it can be used as the selector for a Switch/Case statement as shown above.
If you had used a Dictionary<string, int> for your Data Structure, you could use the Key directly without casting using 'ToString.
A HashTable lets you "get away with" adding Key-Value pairs where the Keys are different Type Objects; a Dictionary is strongly typed and all Keys must be of the same Type.
这篇关于用对象变量切换案例问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!