|
Martin,
I was just commenting on your original assertion - "But it was instanciated as IResponseProcessingCSP, and you can not Up'cast' it , I think!"
This is wrong on two counts. First, you cannot instantiate an interface, as Colin pointed out. I'm sure you know this; it's probably just that you weren't using the correct terminology. Don't worry, your English is far better than my German.
Second, when you say
Type1 reference1 = new Type2(); what is created is always Type2. It simply doesn't matter what type you use to hold the reference (in this case, Type1); the underlying object type is ALWAYS Type2. Therefore, assuming Type2 inherits from Type1 (directly or indirectly, it doesn't matter), a subsequent cast like
Type1 reference2 = reference1; will always work. (You don't even need a cast!)
Hope this clears things up.
Cheers,
Vıkram.
After all is said and done, much is said and little is done.
|
|
|
|
|
Hello,
Vikram A Punathambekar wrote: I'm sure you know this; it's probably just that you weren't using the correct terminology.
YEP!
Vikram A Punathambekar wrote: Don't worry, your English is far better than my German
But I'm sure your German is much better than my Hindi. So the point goes to you!
Vikram A Punathambekar wrote: Second, when you say
Type1 reference1 = new Type2();
Did I?
Vikram A Punathambekar wrote: Therefore, assuming Type2 inherits from Type1 (directly or indirectly, it doesn't matter), a subsequent cast like
Type1 reference2 = reference1;
will always work. (You don't even need a cast!)
I hope I never suggested something else!
I just wanted to find a possible explanation for the original problem, and tried to proof it with a project which is not working (means the project throws an exception, therefor the proof was ok).
Too bad that the one who had the problem seems not to care any more.
-- modified at 5:05 Tuesday 10th July, 2007
All the best,
Martin
|
|
|
|
|
Martin# wrote: Vikram A Punathambekar wrote:
Second, when you say
Type1 reference1 = new Type2();
Did I?
Hee hee, when I said "When you say..." I did not imply that you said those words. I was presenting a hypothetical scenario, not saying you said something.
BTW, there are far too many languages spoken in India to list. While Hindi is something I'm proficient in, I'm neither very fond of it nor is it my native tongue.
About the casting, I think there's just a misunderstanding. Forget all we talked about in this thread and keep up the good work.
Cheers,
Vıkram.
After all is said and done, much is said and little is done.
|
|
|
|
|
Vikram A Punathambekar wrote: Hee hee, when I said "When you say..." I did not imply that you said those words. I was presenting a hypothetical scenario, not saying you said something.
Vikram A Punathambekar wrote: About the casting, I think there's just a misunderstanding. Forget all we talked about in this thread and keep up the good work.
Vikram A Punathambekar wrote: Forget all we talked about in this thread and keep up the good work.
This perfectly fitts to you Signature (Which allways make me laugh when reading)
Vikram A Punathambekar wrote: BTW, there are far too many languages spoken in India to list
Actually, I looked it up bevore answering you! I choosed the one with the most percentage
Here is the link to the German Wikipedia page, for your training. http://de.wikipedia.org/wiki/Indische_Sprachen[^]
All the best,
Martin
|
|
|
|
|
Martin# wrote: This perfectly fitts to you Signature (Which allways make me laugh when reading)
I thought my sig was profound and thought-provoking.
Martin# wrote: I choosed the one with the most percentage
My mother tongue is Marathi. The Marathas were one of the most powerful groups in medieval India, resisting the Muslim invaders and even driving them back.
Martin# wrote: Here is the link to the German Wikipedia page, for your training.
Seeing words like sprachwissenschaftliche and gleichberechtigten makes my head spin. German seems to have a lot of long words. Is it true that there is no neuter gender in German and everything is male/female?
Cheers,
Vıkram.
After all is said and done, much is said and little is done.
|
|
|
|
|
Vikram A Punathambekar wrote: Is it true that there is no neuter gender in German and everything is male/female?
No, there is also neuter words: "das Kind" the child
All the best,
Martin
|
|
|
|
|
check Type.IsAssignableFrom
|
|
|
|
|
I have a C# app. that must send a string to another program. How can i do that?
I tried to use SendKeys.Send(myString) but it sends the string to all of the programs that are running a process. HELP!
|
|
|
|
|
Hi,
there are many ways to do this. Basically you need to:
1. either store the data at an agreed location (say a file), then signal it has
been updated (or wait for a FileWatcher to notice it);
2. or send it as a message to the process directly; this combines the info and
the signal.
2.can be done using different techniques; in each case it is assumed you are
in charge of the code at both ends; and most often you will need several
P/Invoke operations calling win32 functions:
- sockets dont need win32; I stopped doing it because my firewall (ZoneAlarm)
threw up some dialogs when I did
- SendMessage (needs a buffer in the destination process' memory, see
VirtualAllocEx, and WriteProcessMemory and the like
- named pipes; this is the way I currently use; uses CreateNamedPipe,
ConnectNamedPipe, WriteFile, ReadFile and the like.
- MSMQ (Microsoft Message Queues); I havent tried this one yet.
- and probably more
All of them will take some code (say 50 to 100 lines, to set it up and tear
it down correctly at both ends).
On all of these a lot of articles and documentation is available, both on
CodeProject and MSDN.
|
|
|
|
|
Hello experts,
Is there a way to iterate through all of the values assigned to an enum (in my case the Keys enum)?
Something like:
Keys keys = Keys.Control | Keys.A;
foreach (Something goes here)
I know how to iterate through all POSSIBLE values of an enum, but I would like to iterate through all ASSIGNED values of an enum variable.
Thanks in advance,
Shy.
|
|
|
|
|
Hello,
First let me say that I think it's a very good question!
And I have no idea if there is a nice solution for that.
I looked thre the Enum members and haven't found something obvious.
An (very ugly) solution (or let's say workaround), would be.
Use the ToString() method to get a string like: "Control, A";
Than Trim and Split it.
and with the help of Parse you will get the Enum Members:
iterate over the stringarray which is returned from the string.Split(',') method.
Keys actKey = (Keys)Enum.Parse(typeof(Keys), stringarray[x], true);
I hope there is a nicer solution out there! (But maybe it helps you)
All the best,
Martin
All the best,
Martin
|
|
|
|
|
Hi,
first of all I hope your enum behaves as a flags collection (and hence
it better have the [Flags] attribute).
There is a very nice trick to enumerate the individual bits in an int
without using a loop that checks each and every bit:
int work=val;
while (work!=0) {
int singleBit=work & -work;
work=work^singleBit;
... do whatever you need to do with singleBit
}
Applied to enums, this leads to:
[Flags]
enum myFlags {a=1, b=2, c=4, d=8}
public void Run() {
myFlags flags=myFlags.a | myFlags.c | myFlags.d;
int work=(int)flags;
while (work!=0) {
int singleBit=work & -work;
work=work^singleBit;
myFlags mySingleFlag=(myFlags)singleBit;
log(mySingleFlag.ToString());
}
}
this little example prints the characters a, c and d as it should.
|
|
|
|
|
Gah! Looks like you beat me to it while I was writing up my example, and you got the enumeration part down too! I bow to you.
-Phil
|
|
|
|
|
Great!
How do people come up with this stuff?!
I can never get myself to think outside of the box like that!
Anywayz... Why didn't Microsoft implement this kind of iteration too??
It can be very useful in some cases where you want to create something generic...
|
|
|
|
|
Hi,
casting back and forth between an enum and an int is standard practice;
examples are everywhere, even in MS documentation.
Tricky code snippets to manipulate bits (and do many other things), have been
discovered one by one over time; I started my collection of those many years ago...
I even have some books on them, some work only in assembly code, some only
on specific processors, and some (like the current one) can be applied everywhere
even in a high-level language.
|
|
|
|
|
shyagam wrote: Why didn't Microsoft implement this kind of iteration too??
You are absolutely right: they could have provided a GetEnumerator() method
that would throw an exception if the enum is not [Flags], and returns an
enumeration of the enum values that are present in the enum at hand.
But they didnt.
|
|
|
|
|
|
Hello,
So your solution would mean to iterate threw the enum members and do the bit test?
Luc Pattyn wrote: (and hence
it better have the [Flags] attribute).
Is there a rule in the Framework when they use the Attribute?
All the best,
Martin
|
|
|
|
|
Hi Martin,
[Flags] or [FlagsAttribute] is explained in the FlagsAttribute page on MSDN.
It is rather long and not very important.
One thing is it influences how ToString() works, which returns symbolic names
when possible; with Flags it returns a combintation of one or more symbolic names,
without Flags it does not.
Unfortunately, when specifying [Flags] it still is possible to assign values
that are NOT powers of 2 as in:
[FlagsAttribute]
enum myFlags3 { a=1, b=2, c=15, d=8 }
In fact they do recommend you do this with the example Read=1, Write=2, ReadWrite=3
but I would like the possibility to get an error when I do this accidentally.
Greetings.
|
|
|
|
|
Hi,
Martin# wrote: So your solution would mean to iterate threw the enum members and do the bit test?
No, my code does not even has to know what all the possible enum values are;
the only thing it is interested in is the fact that they are Flags, not arbitrary
values.
Watch my while-loop carefully, it does not perform an Enum.GetValues()
and it does not iterate over the bits (as in for(int bit=0; bit<32; bit++)...);
it automatically loops a number of times, equal to the number of bits set
in the enum value !
|
|
|
|
|
Fantastic, got it now!
Again my '5' for that!
All the best,
Martin
|
|
|
|
|
Well, I don't know how to iterate through all POSSIBLE values of an enum, but given that you say you do, I think I have a solution for you.
Keys keys = Keys.Control | Keys.A;
foreach(Keys key in POSSIBLEVALUES){
if(keys & key) == key){
}
}
Note: the enum value have to be lined up in powers of 2 in order for this to work properly so you can distinguish the values when you do a bitwise-or.
The following is a bit of code that shows this:
enum Keys
{
A = 1,
B = 2,
C = 4,
D = 8
}
static void Main(string[] args)
{
Keys keys = Keys.A | Keys.B | Keys.D;
if ((keys & Keys.A) == Keys.A)
{
Console.WriteLine("A");
}
if ((keys & Keys.B) == Keys.B)
{
Console.WriteLine("B");
}
if ((keys & Keys.C) == Keys.C)
{
Console.WriteLine("C");
}
if ((keys & Keys.D) == Keys.D)
{
Console.WriteLine("D");
}
|
|
|
|
|
Yeah, well I guess that's the first solution I came up with too, but this would mean iterating through the entire enum, which in my case is very "expensive", as the Keys enum is quite large...
|
|
|
|
|
FYI, here's another way to do it w/ Enum.Parse method, which is undoubtably slower and takes more memory, but improves readability (maybe )
[Flags]
enum Keys
{
A = 1,
B = 2,
C = 4,
D = 8
}
static void Main(string[] args)
{
Keys keys = Keys.A | Keys.B | Keys.D;
string[] strKeys = keys.ToString().Split(',');
foreach(string key in strKeys){
Keys newKey = (Keys)Enum.Parse(keys.GetType(),key);
Console.WriteLine(newKey.ToString());
}
}
|
|
|
|
|
System.Media won't do event with multi-threading
DirectX... well I just don't know much MDX...
[ still getting Loader-lock warnings... ]
thx
Murphy
|
|
|
|
|