|
Write a character 7, or use System.Console.Beep which allows specifying the frequency and duration of the beep.
|
|
|
|
|
In .NET:
using System.Media;<br />
<br />
SystemSounds.Beep.Play()
You can also play any of the other Windows sounds.
|
|
|
|
|
|
Hi,
I'd like to know if casting based on a Type object is possible. Consider the following example:
using System;
namespace ConsoleApplication1
{
class BaseClass
{
}
class ClassA : BaseClass
{
}
class ClassB : BaseClass
{
}
class Program
{
static void Main(string[] args)
{
BaseClass obj1 = new ClassA();
BaseClass obj2 = new ClassB();
Program prog = new Program();
prog.TreatAnything(obj1);
prog.TreatAnything(obj2);
}
public void TreatAnything(BaseClass obj)
{
if (obj is ClassA)
TreatClass((ClassA)obj);
else if (obj is ClassB)
TreatClass((ClassB)obj);
else
{
}
}
private void TreatClass(ClassA obj)
{
}
private void TreatClass(ClassB obj)
{
}
}
}
The TreatAnything function receives a BaseClass object, and is forced to check its type by the is operator before casting it and calling the appropriate function. Now, I know the example looks simplistic, but imagine there are tens of classes derived rom BaseClass. Sure, I could make tens of if clauses and call the correct function each time... What I would really love to do, though, is turn the TreatAnything function into something like this:
public void TreatAnything(BaseClass obj)
{
Type t = obj.GetType();
TreatClass(SomeHypotheticalCastingFunction(t, obj));
}
So the tens of if clauses would be reduced to a small number of statements. Is there any way to do this kind of thing?
In case you want to know what I'm trying to do: I made a socket that parses what is receives and builds an object which is then passed around various methods. I have a Message base class, and then several derived classes. Each derived class represents a specific type of message, whose reception puts in motion very different actions on the server side. No, I don't think it's feasible to make a virtual method Treat() on the Message class and override it on each derived class, as the actions involved require much interaction with other objects the messages know nothing about.
I thank you in advance for any suggestions you might have.
Leonardo Pelisoli
|
|
|
|
|
Hi
You could use Reflection to invoke the appropriate method. something like this:
public class Program
{
public Program()
{
}
public void DoStuff()
{
BaseClass obj1 = new ClassA();
BaseClass obj2 = new ClassB();
ProcessObject(obj1);
ProcessObject(obj2);
}
void ProcessObject(BaseClass obj)
{
Console.WriteLine("BaseClass");
GetType().GetMethod("ProcessObject",System.Reflection.BindingFlags.NonPublic|System.Reflection.BindingFlags.Instance,null,new Type[]{obj.GetType()},null).Invoke(this,new object[]{obj});
}
void ProcessObject(ClassA obj)
{
Console.WriteLine("ClassA");
}
void ProcessObject(ClassB obj)
{
Console.WriteLine("ClassB");
}
}
public class BaseClass
{
}
public class ClassA:BaseClass
{
}
public class ClassB:BaseClass
{
}
|
|
|
|
|
I'm not sure you need to cast at all in that case, have you tried it without casting? And why have the TreatAnything method at all?
Well, once again I've spoken too soon. But there should still be a simpler way...
modified on Monday, December 10, 2007 7:09:54 PM
|
|
|
|
|
OK, here's another way, using Reflection as m@u suggested, but rather than going to Reflection for each incoming message (which is very costly) I do it once in a static constructor and populate a static dictionary (my latest favourite toy). It's still not as clean as I'd like, but it's fun!
namespace ConsoleApplication1
{
class BaseClass { }
class ClassA : BaseClass { }
class ClassB : BaseClass { }
class Program
{
private static readonly System.Collections.Generic.Dictionary<System.Type,System.Reflection.MethodInfo> dic ;
static Program
(
)
{
System.Reflection.ParameterInfo[] pi ;
System.Type pt ;
dic = new System.Collections.Generic.Dictionary<System.Type,System.Reflection.MethodInfo>() ;
foreach
(
System.Reflection.MethodInfo mi
in
typeof(Program).GetMethods()
)
{
if ( ( mi.Name == "TreatClass" ) && ( mi.ReturnType == typeof(void) ) )
{
pi = mi.GetParameters() ;
if ( pi.Length == 1 )
{
pt = pi [ 0 ].ParameterType ;
if ( pt.IsSubclassOf ( typeof(BaseClass) ) )
{
dic.Add
(
pt
,
mi
) ;
}
}
}
}
}
static void Main(string[] args)
{
BaseClass obj1 = new ClassA();
BaseClass obj2 = new ClassB();
Program prog = new Program();
prog.TreatAnything(obj1);
prog.TreatAnything(obj2);
}
public void TreatAnything(BaseClass obj)
{
if ( dic.ContainsKey ( obj.GetType() ) )
{
dic [ obj.GetType() ].Invoke ( this , new object[] { obj } ) ;
}
}
public void TreatClass(ClassA obj)
{
System.Console.WriteLine ( "A" ) ;
}
public void TreatClass(ClassB obj)
{
System.Console.WriteLine ( "B" ) ;
}
}
}
|
|
|
|
|
My thanks to you both, PIEBALDconsult and m@u. That's effectively a very elegant solution to my problem.
Best regards,
Leonardo Pelisoli
|
|
|
|
|
I'm a java programmer and am not sure how to do property
This code:
public DateTime StartDate<br />
{<br />
get<br />
{<br />
return this.StartDate;<br />
}<br />
set<br />
{<br />
this.StartDate = value;<br />
}<br />
}
creates a stack overflow, whereby if it's set like this:
projectA.StartDate = new DateTime(2007, 12, 15);
This is only call once to initalize it, however it keeps looping around inside the property and overflows the callstack.
Anyone have any ideas what I'm doing wrong?
|
|
|
|
|
gunner_uk2000 wrote: return this.StartDate;
that is refering itself.
you probably want a member variable (say private DateTime startDate; and have your
get and set manipulate it, rather than refer itself.
|
|
|
|
|
You need a member variable, say:
private DateTime _startDate;
and then set and get the member variable using:
get{ return _startDate; }
set{ _startDate = value; }
what you code does is to keep calling the property's setter.
"On one of my cards it said I had to find temperatures lower than -8. The numbers I uncovered were -6 and -7 so I thought I had won, and so did the woman in the shop. But when she scanned the card the machine said I hadn't.
"I phoned Camelot and they fobbed me off with some story that -6 is higher - not lower - than -8 but I'm not having it."
-Tina Farrell, a 23 year old thicky from Levenshulme, Manchester.
|
|
|
|
|
I think it should be this
private DateTime _startDate;<br />
<br />
public DateTime StartDate<br />
{<br />
get<br />
{<br />
return _startDate;<br />
}<br />
set<br />
{<br />
_startDate = value;<br />
}<br />
}
The property works as a public getter/setter, but needs to assign the value to an actual (usually private) variable.
|
|
|
|
|
I am getting a mouse click event on a User Control object, which has not been clicked or manipulated in any way, immediately after another class object, base on a User Control, is dropped in the Panel where these objects have been added.
I am using the standard Drag and Drop events to move an object around within the same Panel. I initiate the "DoDragDrop(...)" from a Mouse_Down event in the User Control object that I am dragging.
Does anyone know from where this mouse click event might originates.
|
|
|
|
|
hi to all, i want to do a job per 40 milisecond but i can't !;
timer component can not support this resolution ,
can i access to this resolution in other way?
|
|
|
|
|
you can go for waitable timers
|
|
|
|
|
may I suggest you read my timers article.
|
|
|
|
|
Luc Pattyn wrote: may I suggest you read my timers article.
Shameless plugging going on?
Seriously though, I was going to point him at your article anyway.
|
|
|
|
|
Pete O'Hanlon wrote: Shameless plugging going on?
Of course. That's what articles are for, I would say.
|
|
|
|
|
Luc Pattyn wrote: Of course. That's what articles are for, I would say.
It's all right. I'll forgive you since you link to one of mine.
I'm very magnanimous that way.
|
|
|
|
|
Pete O'Hanlon wrote: I'm very magnanimous that way.
and Mrs. Slocombe is unanimous on that.
|
|
|
|
|
Luc Pattyn wrote: and Mrs. Slocombe is unanimous on that.
Indeed - did you spend all night stroking your poor pussy because it was quivering?
|
|
|
|
|
You'll make Captain Peacock frown once more.
|
|
|
|
|
Did you try all three types of timers?
System.Timers.Timer
System.Threading.Timer
System.Windows.Forms.Timer
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
Hi All,
I have created one project and have installed on GAC. After that I am not able to "debug" my code. Could you please help me to resolve my problem.
Exactly saying, I have created one IE toolbar. That assembly, I have kept it in GAC. But, I am unable to debug that assembly.
Thanks in advance
Thanks and regards,
Srinivas
|
|
|
|
|
Why not just remove it from the GAC?
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|