why I can't use variables in razor conditional statements - c#

Below is the .cshtml code:
#switch (ViewBag.StockLevel)
{
case 0:
#:Out of Stock
break;
case 1:
case 2:
case 3:
<b>Low Stock (#ViewBag.StockLevel)</b>
break;
default:
#:#ViewBag.StockLevel
break;
}
I don't understand why I can't modify the default as:
default:
ViewBag.StockLevel
I mean after the default keyword, it is C# statement, isn't that plain ViewBag.StockLevel is what we need?

Related

what the opcode.code that represent Logical Condition Operators (&& or || ) in conditional branch C#

public int IS_Conditinal(Mono.Cecil.Cil.OpCode code )
{
switch (code)
{
case Code.Beq:
case Code.Beq_S:
case Code.Bge:
case Code.Bge_S:
case Code.Bge_Un:
case Code.Bge_Un_S:
case Code.Bgt:
case Code.Bgt_S:
case Code.Bgt_Un:
case Code.Bgt_Un_S:
case Code.Ble:
case Code.Ble_S:
case Code.Ble_Un:
case Code.Ble_Un_S:
case Code.Blt:
case Code.Blt_S:
case Code.Blt_Un:
case Code.Blt_Un_S:
case Code.Bne_Un:
case Code.Bne_Un_S:
case Code.Brfalse:
case Code.Brfalse_S:
case Code.Brtrue:
case Code.Brtrue_S:
case Code.Endfilter:
case Code.Endfinally:
return 1;
case Code.And:
case Code.Or:
return -1;
default:
return 0;
}
}
I want to calculate the conditional branch statement, but only when the conditional branch statement that contain Logical Condition Operators
I will count it more than once as it contain logical operators,
(case Code.And:
case Code.Or:
return -1;)
dose not solve the problem because it represent(& , | ) and not represent(&& ,||),please help me very quickly

Why can't I use an OR operator in a switch case for strings in C#?

It says I cannot use an OR on two strings in a switch case (Dental || Vision). Would placing each option in its own separate case work or am I messing up the syntax? Much thanks
switch (GR.planTypeFocus){
case "Medical":
CreatePlanForMedical();
break;
case "Dental" || "Vision":
//createPlanForDental_Vision
break;
case "LTD" || "Life":
//createPlanForLTD_Life
break;
}
}
Technically you achieve 'OR' by using fall through
switch (GR.planTypeFocus){
case "Medical":
CreatePlanForMedical();
break;
case "Dental":
case "Vision":
//createPlanForDental_Vision
break;
case "LTD":
case "Life":
//createPlanForLTD_Life
break;
}
Of course you can use an "or", based on the context of your example:
switch (GR.planTypeFocus){
case "Medical":
CreatePlanForMedical();
break;
case "Dental":
case "Vision": //<-- here's how you do it
//createPlanForDental_Vision
break;
case "LTD":
case "Life": //<-- this is how you do it
//createPlanForLTD_Life
break;
}

convert type int to my custom enum

My model has property
public enum CheckStatus
{
A = 1,
B = 2,
C = 3,
}
public CheckStatus Status { get; set; }
and inside razor view I want to switch this property like
#switch (Model.Status)
{
case 1:
default:
<div>Selected A</div>
break;
case 2:
<div>Selected B</div>
break;
case 3:
<div>Selected C</div>
break;
}
Cannot implicitly convert type 'int' to 'CheckStatus'. An explicit
conversion exists (are you missing a cast?)
Your switch statement parameter and the case Label must be of the same datatype.
so cast your enum to int like this
switch ((int)Model.Status)
{
case 2:
<div>Selected B</div>
break;
case 3:
<div>Selected C</div>
break;
default:
<div>Selected A</div>
break;
}
or use the CheckStatus in your case statement as well
switch (Model.Status)
{
case CheckStatus.B:
<div>Selected B</div>
break;
case CheckStatus.C:
<div>Selected C</div>
break;
default:
<div>Selected A</div>
break;
}
I removed the first case as you are not doing anything in that case. Also put the default case at the end which make things readable. You can also use the Case 1 and remove the default (if you want)
try this
switch((int) Model.Status) { }
to reach your goal!
Appendix: Model.Status would just return A, B etc, not the integer values behind.

Whats stopping C# compiler from implementing 'Switch' statement on 'Type'? [duplicate]

This question already has answers here:
Is there a better alternative than this to 'switch on type'?
(31 answers)
C# switch on type [duplicate]
(5 answers)
Closed 9 years ago.
I understand that C# compiler as it stands does not let switching on Type like
switch (typeof(MyObj))
case Type1:
case Type2:
case Type3:
There are solutions where a Dictionary of Type and Action could be used or a static class (link), but I am just curious, why is this a bad practice or not implemented in the compiler yet ?
Thank you in advance.
If you're talking about primitive types you can do switch over TypeCode.
var typeCode = Type.GetTypeCode(type);
switch (typeCode)
{
case TypeCode.Empty:
break;
case TypeCode.Object:
break;
case TypeCode.DBNull:
break;
case TypeCode.Boolean:
break;
case TypeCode.Char:
break;
case TypeCode.SByte:
break;
case TypeCode.Byte:
break;
case TypeCode.Int16:
break;
case TypeCode.UInt16:
break;
case TypeCode.Int32:
break;
case TypeCode.UInt32:
break;
case TypeCode.Int64:
break;
case TypeCode.UInt64:
break;
case TypeCode.Single:
break;
case TypeCode.Double:
break;
case TypeCode.Decimal:
break;
case TypeCode.DateTime:
break;
case TypeCode.String:
break;
}
BTW answer for your question you may need to read How many Microsoft employees does it take to change a lightbulb?
I would refer you to the accepted answer for : switch statement in C# and "a constant value is expected" . The compiler needs to know at compile time that there won't be any duplicates, so it accepts only constants. By the way, you can achieve the same effect with
switch (typeof(MyObj).FullName
and use the names of each type as case conditions, like:
case "MyNamespace.Type1":
/*stuff*/
break;
case "MyNamespace.Type2":
/*other stuff*/
break;
default:
/*default stuff*/
When would you use switching on type? Most of the cases I can think of are better solved with inheritance, i.e. rather than doing:
switch (typeof(MyObj)) {
case Type1: doSomethingForType1; break;
case Type2: doSomethingForType2; break;
case Type3: doSomethingForType3; break;
You would set it up in a much more object-oriented manner:
Interface ISpecialType {
void doSomething();
}
Type1 : ISpecialType {
doSomething() {}
}
Type2 : ISpecialType {
doSomething() {}
}
Type3 : ISpecialType {
doSomething() {}
}
then, no matter what, you just call MyObj.doSomething(); It's a little bit more typing at the start, but a lot more robust.
Also, if it's really important to you to switch on, you can always use typeof(MyObj).toString() and switch on that. It's not recommended practice, since you're then hard-coding strings that are allowed to change into your switch, but you can do it.

Why a SWITCH...CASE not taking 'char'?

I have two questions with this code:
public int InsertOrUpdateRecord(char _code, string _databaseFileName)
{
switch(_code)
{
case 'N':
// Some code here
case 'U':
// Some code here
}
return 0;
}
It is not accepting char single quote and double quote value.
If I pass _code as string, it gives red underline in case with this error:
Control cannot fall through one case label to another.
The reason for the compilation error is that the case is missing the break
switch (_code)
{
case 'N':
// Some code here
break; // break that closes the case
case 'U':
// Some code here
break; // break that closes the case
}
You need to do a break at the end of the case:
switch (_code)
{
case 'N':
// Some code here
Console.WriteLine("N was passed");
break;
case 'U':
// Some code here
Console.WriteLine("U was passed");
break;
}
Unless you want in either cases to do the same like so:
switch(_char) {
case 'N':
case 'U':
// Common code for cases N and U here...
}
You have to specifically tell the compiler where the case statement halts:
switch(_char) {
case 'N':
// Code here...
break; // The N case ends here.
case 'U':
// Code here with different behaviour than N case...
break; // The U case ends here.
}
The break statement tells the compiler that you're done with that case, and that it has to get out of the switch instruction.
you can either write a break or return statement like the below code.
char _code = 'U';
switch (_code)
{
case 'N':
case 'U':
return;
}
OR,
char _code = 'u';
switch (_code)
{
case 'N':
case 'U':
break;
}
There is no problem with char. See this msdn article about your error.

Categories

Resources