|
I suggest you go and talk to Prof. S.M. Aqil Burney, you can get his e-mail here University of Karachi CS Faculty[^] he seems to be the expert at your Uni. I also suggest you stop spamming the lounge.
|
|
|
|
|
|
inayathussaintoori wrote: who are you
He is the one giving you good advise.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
|
|
|
|
|
Read my profile[^] on the code project, it's all there.
|
|
|
|
|
I have a dialog where I need to return a result. The result can be either a "standard class defined value" (an enum) or a user defined value (an int).
So I end up with something like:
enum SomeEnum
{
Val1 = 1,
Val2 = 2,
Val3 = 4,
Custom = 8
}
public SomeEnum Result
{
get
{
...
}
}
public int UserResult
{
get
{
...
}
}
The idea here is that if Result is Custom, they call UserResult to get the custom value. I'm not really keen on that idea because they have to look at 2 properties.
I could cast Result to an int and return it that way, but that idea, I'm not really happy with either because they'll be using the built in values 75% of the time, so having the code littered with casts isn't that great.
I could return (or accept) both as object, but that'd be a lot of "if (Result is ...)" checks.
On the drive in to work, I thought about having the user derive an enum from the built in enum and extend it, but apperently C# doesn't allow that .
Any other ideas?
|
|
|
|
|
Two options off the top of my head:
1) Return a struct containing an enum and an int. If the enum is custom, process the int.
2) Just return the enum, but consider custom to be the "else" case... It may be bad practice, but an enum can contain integer values that aren't listed. So if the user picks custom, return the UserResult cast to an enum.
|
|
|
|
|
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.
|
|
|
|