I'm having problem in my conversion of a case statement from VB.Net to C# and need some help. I'm not reading the right material or I'm missing something somewhere . . .

Here is what I have:

switch (true)
           {
                //Case strRequesterEmail <> "" And strAssigneeEmail <> ""
                case !string.IsNullOrEmpty(strRequesterEmail) & (strAssigneeEmail != "donotreply@dimtpa.com" & !string.IsNullOrEmpty(strAssigneeEmail)):
                    objMessage.To.Add(strRequesterEmail);
                    objMessage.To.Add(strAssigneeEmail);
                    break;
                case !string.IsNullOrEmpty(strRequesterEmail):
                    objMessage.To.Add(strRequesterEmail);
                    break;
                case !string.IsNullOrEmpty(strAssigneeEmail):
                    objMessage.To.Add(strAssigneeEmail);
                    break;
                default:
                    objMessage.To.Add(BuildUserList(1));
                    break;
            }

I'm getting an error message of "A constant value is expected" at

!string.IsNullOrEmpty(strRequesterEmail)

- I tried

!(string.IsNullOrEmpty(strRequesterEmail))

- but that didn't help

Recommended Answers

All 4 Replies

Case statements as a rule usually take the form

test value is x

if test is 1 do this and end
if test is do this and end
if test isdo this and end
othewise do the this and end
end test.

rest of code..

Your test there is asking different questions, and would only cover 1 aspect should anything go wrong. Eg,

!string.IsNullOrEmpty(strRequesterEmail) & (strAssigneeEmail != "donotreply@dimtpa.com" & !string.IsNullOrEmpty(strAssigneeEmail)

If that was true and it added the 2 parts listed, that would be it, it wouldnt then go on to do the rest. Which in that case might be ok, but for the single parts you would then have to additionally test which one or neither happened.

For this instance Id suggest some if statements, but Id also create a flag at the top to test if all aspects were set, eg a requester and an assignee. As if those 2 parts are not filled in, your email wouldnt be vaoid anyway.

Your case statement reads like this

test nothing
if something_1 is a value then do this
if something potentially totally unrelated is a value then do this..
end test..

This isnt what case statements were really designed for..

They are much more..

// WARNING FAKE CODE

Menu option?  valid 1-9 get option

switch  option
{
  case 1 : do menuoption1
  case 2 : do menuoption2
 ...
  default : do invalid_option
}

Should I combine these if statements into one larger or is it even possible? For some reason (and I'm not finished) it seems like it's a mess already .. .

if (strRequesterEmail != string.Empty)
            {
                    if (strAssigneeEmail != "donotreply@dimtpa.com")
                    {
                        if (strAssigneeEmail != null)
                        {
                            objMessage.To.Add(strRequesterEmail);
                            objMessage.To.Add(strAssigneeEmail);
                        }
                    }
                }

                if (strRequesterEmail != string.Empty)
                {
                    if (strRequesterEmail != null)
                    {
                        objMessage.To.Add(strRequesterEmail);
                    }
                }

                if (strAssigneeEmail != string.Empty)
                {
                    if (strAssigneeEmail != null)
                    {
                        objMessage.To.Add(strAssigneeEmail);
                    }
                    //        objMessage.To = BuildUserList(1)
            }

You dont have to do each of the test conditions in different ifs, eg..

you can do

if (!String.IsNullOrEmpty(strAssigneeEmail)  &&strAssigneeEmail != "donightreply@dimtpa.com") 
{
  // 
}

However, one thing that does strike me is that in your case above as long as assigneeemail isnt empty and doesnt equal the donot reply address, its going to have added the requester email and the assignee email irrelevant of wether the requestermail is empty or not.

My view?

pseudo code:

If both are not null or empty & assigneeemail isnt donot reply then add them both and build user list..

otherwise

whine it wasnt right.

1 big if and 3 items if it passes 1 if it fails.

Thanks again LizR

I finally caught on to it - I wasn't thinking about the difference between the if statements and the case on this one. I got it to work properly this way: (might still not be the best way though)

if ((strRequesterEmail != string.Empty) && (strRequesterEmail != null))
                {                   
                        objMessage.To.Add(strRequesterEmail);   
                }

            if ((strAssigneeEmail != string.Empty) && (strAssigneeEmail != null) && (strAssigneeEmail != "donotreply@dimtpa.com"))
                {
                    objMessage.To.Add(strAssigneeEmail);
                }                 
            
           if ((strAssigneeEmail == string.Empty) && if (strRequesterEmail == string.Empty))
                {               
                    objMessage.To.Add(BuildUserList(1));                
                }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.