Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Can you macro in C#/.NET?
Message
From
29/05/2012 11:32:56
 
 
To
29/05/2012 10:55:01
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 2.0
Miscellaneous
Thread ID:
01544610
Message ID:
01544620
Views:
31
>>>>Hi,
>>>>
>>>>Before I do the IF/ELSE, or SWITCH, I am wondering if I could macro expand in C#. Here is an example. I am passing a value of ParameterDirection as a string (e.g. 'Input', 'InputOutput', 'Output') I need to set ParameterDirection based on the passed value.
>>>>
>>>>Here is the pseudo code:
>>>>
>>>>
>>>>string cDirection = 'Input';
>>>>param.Direction = ParameterDirection. + 'cDirection';
>>>>
>>>>
>>>>I am pretty sure that the above syntax will not work.
>>>
>>>It won't :-}
>>>
>>> But is there way to make it work?
>>>
>>>In general, no. But for enums you can use this:
string cDirection = "Input";
>>>param.Direction= (ParameterDirection) Enum.Parse(typeof(ParameterDirection),  cDirection);
>>
>>Thank you. I will try this approach.
>
>Using the enum directly is shorter
>
>
>param.Direction = ParameterDirection.Input;
>
Of course, but I'm assuming Dmitry had reasons for using a string.
Dimitry: If the enum value could not be used then an int might be a better choice - but whether a string or an int it would be wise to check that it is a valid value. eg:
           int direction = 1;
            if (Enum.IsDefined(typeof(ParameterDirection), (ParameterDirection)direction))
                param.Direction = (ParameterDirection)1;
            else
                throw (new InvalidCastException("Invalid ParameterDirection"));
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform