Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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

C#
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:

VB
<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
Posted
Updated 19-Jan-15 19:39pm
v2
Comments
Sergey Alexandrovich Kryukov 20-Jan-15 1:15am    
It's too wrong from the very beginning to discuss any bugs. Stop hard-coding, start programming. First of all, get rid of all the immediate string constants, stop repeating any code, such as this assignment of -1 and 215; this is totally non-maintainable...
—SA
Hesham el Masry 20-Jan-15 1:34am    
Thank you so much , but how i do it without switch case or what iam still a beginner just asking for small help
Sergey Alexandrovich Kryukov 20-Jan-15 2:19am    
I did not say "without switch case", I meant all those constants. But long case is also bad. It's all about desing of reasonable code. At least define some enumeration instead of constants.
—SA
Tejas Vaishnav 20-Jan-15 1:30am    
can you please also add you vb code too, so we can help you. just use improve question functionality to edit you question, and add you vb code for same.
Hesham el Masry 20-Jan-15 1:39am    
thank you Tejas i updated my vb code you can check it now

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.
 
Share this answer
 
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 your ht_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...

C#
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.
 
Share this answer
 
v3
Take a look at this example:
C#
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900