|
I got another idea too... instead of using an enum, I can use consts...
public class SomeEnum
{
public static readonly int Val1 = 1;
public static readonly int Val2 = 2;
}
this way, I can just return an int for both cases and not have to worry about typecasting or exceptions from something like enum.ToString() when it contains an unknown value.
I guess this is a little better then having the 2 properties... and eliminates checking for the custom type.
|
|
|
|
|
Hi,
What about this piece of code ?
public class MyDialog : Form
{
public SomeEnum Result { get; private set; }
public int UserResult { get; private set; }
}
private void btnChoose_Click(object sender, EventArgs e)
{
using (MyDialog dlg = new MyDialog())
{
(if (dlg.ShowDialog() == DialogResult.OK)
{
if (dlg.Result == SomeEnum.Custom)
{
}
else
{
}
}
}
}
Thus, your dialog should only care to set both its properties according to your logic. Only your main form should care about values and decide what to do next, according to your particular logic.
And, if you want to store the final value in a variable in order to use it later, i'm afraid you won't have another choice than storing it to an int and doing casts from/to your SomeEnum type.
Or, you could hold two variables (one of int type and another one of SomeEnum type) and repeat your logic everytime you have to decide something from them (this part could be encapsulated in a small utility class that would hold a few methods implementing the logic).
I can't think of other solutions since I don't know what are your actual requirements/logic.
Hope this helps.
Regards.
|
|
|
|
|
if your int result needs its full range, I see no way other than having two results, as you stated.
if OTOH part of the int range will not be needed, then you could map your special values in there; the easiest would be when the int result only has positive values, then you could do:
const int val1=-1;
const int val2=-2;
const int val3=-3;
int res=MyFunction(...);
if (res>=0) log ("custom value = "+result);
or
switch (res) {
case val1:
log("val1");
break;
case val2:
log("val2");
break;
case val3:
log("val3");
break;
default:
log ("custom value = "+result);
break;
}
|
|
|
|
|
Yeah, I'm thinking "simulating an enum with const ints" might be the best solution since there won't be typecasts and/or special case checks for the custom values.
|
|
|
|
|
Would something like this work?
[Flags]
enum BarEnum
{
none = 0,
Bar1 = 1,
Bar2 = 2,
Bar3 = 4,
Custom = 8
}
static void Main(string[] args)
{
BarEnum e = Foo();
if( (e & BarEnum.Custom) == BarEnum.Custom)
{
int result = (int)(e ^ BarEnum.Custom);
}
}
private static BarEnum Foo()
{
return BarEnum.Custom + 80;
}
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
public SomeEnum GetResult(int x)
{
return IntToEnum(x, SomeValue.Val1);
}
public int GetResult(SomeEnum x)
{
return (int)x;
}
public static T IntToEnum<T>(int value, T defaultValue)
{
T enumValue = (Enum.IsDefined(typeof(T), value)) ? (T)(object)value : defaultValue;
return enumValue;
}
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
As mentioned, just use the enum and allow the user to set any value he wants -- it's no big deal.
Or perhaps persist the values somehow (XML?) and allow the user to define new values as he sees fit.
Clippy: I see that you chose a number that is not currently defined. Would you like to define a name for it?
|
|
|
|
|
Hey All,
We all prefix interface names with I but does anyone (else) prefix their abstract class names with A?
Hope we're all well!
Jam
|
|
|
|
|
No. Abstract class names should perhaps have the word "Base" in them.
interface ISomething
abstract class SomethingBase : ISomething
class SomethingSpecial : SomethingBase
|
|
|
|
|
I personally use A, considering base does not make sense.
For example:
public abstract class ShapeBase{...}
public class Triangle : ShapeBase {...}
or:
public abstract class AShape{...}
public class Triangle : AShape{...}
|
|
|
|
|
I just prefix interfaces with "I", but do not use anything special for naming classes, normal, abstract, sealed or static, I don't mind.
|
|
|
|
|
I think that would make it too confusing when discussing things since A is the
definite article[^]. No one is going to think you are claiming to be a MembershipProvider when you are talking about IMembershipProvider. But distinguishing in conversation the difference between "a MembershipProvider and AMembershipProvider" is much more difficult.
|
|
|
|
|
I dunno; IMembershipProvider just sounds arrogant. I'm definitely going to name an interface ISpy sometime.
|
|
|
|
|
|
No.
But I occasionally add the postfix Base to a class that I want to be the parent class to be inherited.
For e.g. EmployeeBase .
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
Abhinav S wrote: But I occasionally add the postfix Base to a class that I want to be the parent class to be inherited.
It's been long time abinav i have used postfix in sentence, always prefers suffix, you remembered that word again, thanks.
thatraja |Chennai|India|
Brainbench certifications Down-votes are like kid's kisses don't reject it Do what you want quickly because the Doomsday on 2012
|
|
|
|
|
Hi All
I've been looking in to sending information to an application that is already open, the objective being for the app to behave like Excel when you have the app open and double click on a file in explorer.
I came across this article http://msdn.microsoft.com/en-us/library/ms996475[^] but couldn't get it to work.
To make it even more interesting(!) we need to deploy this over Citrix servers and the app itself is installed using Softgrid, so we may have an app running on one server that needs to see if an instance of another app is running on any server for that user and get it to open something else.
Does anyone know if this is possible? We use .NET3.5
Thanks
The FoZ
|
|
|
|
|
hey, i want to know how to disable certain keys from being pressed if another one has been pressed before, i am making a game which moves the player accress the screen, and there are certain restrictions to move the player, hence my question... i have programmed my keys below :
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
Player.Direction = 1;
}
if (e.KeyCode == Keys.Right)
{
Player.Direction = 3;
}
if (e.KeyCode == Keys.Down)
{
Player.Direction = 2;
}
if (e.KeyCode == Keys.Left)
{
Player.Direction = 4;
}
}
I want to disable the "Keys.Up" if the "Keys.Down" is pressed but if another key is pressed, for example, the "Keys.Left" then the "Keys.Up" is re-enabled .....
Thanks in advance :P
|
|
|
|
|
To disable a key, set e.Handled = true before you return.
See here[^]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
same code as you have - perfect, on whatever key you want to hit, have a boolean, check if you want it to be enabled/disabled based on that boolean,
put return False where you want the key to do nothing
otherwise leave out the return false
MVC
|
|
|
|
|
Don't mean to step on the other guys who answered, but it looks like you're trying to have Up and Down cancel each other out (And Left+Right)... This'll take a little more than what you have now, but not very much.
The easiest way would probably be to just keep track of which keys are currently pressed. Now, you can use API calls to query them directly, but if you want to stick to simple events, here's one way to do it:
1) Create a dictionary mapping keys to a boolean
2) Whenever a key you're watching is pressed OR released (Handle the KeyUp event too!), set its value in that dictionary to true (Pressed) or false (Not pressed)... If you want to make this more readable and intuitive, you can even use an enum instead of a boolean.
3) Whenever you make a change to the dictionary, run a separate function to test the current key states... Then you can check for things like if (IsKeyPressed[Keys.Left] && !IsKeyPressed[Keys.Right]) ...
There are plenty of other ways to do this, but this is one of the simplest... If you want to be more efficient, you can use a single flags-type enum value instead of a dictionary, and use binary operations to set and unset the various components:
[Flags]
enum ArrowKeyStates {
None = 0,
Left = 1,
Up = 2,
Right = 4,
Down = 8,
UpLeft = 3,
UpRight = 6,
DownLeft = 9,
DownRight = 12
}
MyKeyState |= ArrowKeyStates.Left;
MyKeyState ^= ArrowKeyStates.Left;
|
|
|
|
|
thanks everyone, helped me a lot,
|
|
|
|
|
i need a formula through which i can increase the font size of records in details section by passing value like 12,13,14
|
|
|
|
|
What is this to do with C#?
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
Hey,What you will do with value 12,13,14. your requirement is not clear.
we can help you when you try to help us by clear formatting of your question.
|
|
|
|