|
Very true, and I'll have to keep that in mind, but one of my more complicated uses of is + cast is this sort of thing:
if ( o is typeA ) do something with (typeA) o
else if ( o is typeB ) do something with (typeB) o
else if ( o is typeC ) do something with (typeC) o
...
I don't think that would translate well to using as :
typeA tempA ;
typeB tempB ;
typeC tempC ;
...
if ( ( tempA = o as typeA ) != null ) do something with tempA
else if ( ( tempB = o as typeB ) != null ) do something with tempB
else if ( ( tempC = o as typeC ) != null ) do something with tempC
...
And is + cast works with all types, not just reference types.
|
|
|
|
|
Hi,
OK, I guess that's why both forms exist.
My use is mostly simple, as in find all Buttons in myForm.Controls
(So I would appreciate a forgiving variant of foreach that does this automatically
without throwing when some non-Button Controls are present)
Type checking value types is something I havent needed much, if at all.
If and when I need it, the compiler will remind me to use is (and then
I will remember this thread).
Regards.
|
|
|
|
|
Hello,
Colin Angus Mackay wrote: You can't instantiate an interface.
True, but what does that mean to his problem. Hmmm!
The method returns an IResponsableProcessingCSP instance and can not be casted too an derived class.
Would it have the same effect (exception) if it was originaly instanciated as on other derived class from IResponsableProcessingCSP?
Colin Angus Mackay wrote: Perhaps there would be a situation where you do want an exception. Perhaps a case where the situation should never exist and so will be an exceptional case. Use "as" only when the posibility is frequent and expected.
Personaly, I would also use the as operator for that and check !=null, and throw my own Exception!
But that's maybe a matter of taste.
All the best,
Martin
|
|
|
|
|
Martin# wrote: Colin Angus Mackay wrote:
You can't instantiate an interface.
True
Martin# wrote: The method returns an IResponsableProcessingCSP instance
You argree with me, then contradict yourself.
You cannot have an instance of an interface. You can only have a reference to one. The instance is of some concrete object that happens to implement the interface.
Upcoming events:
* Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ...
"I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless."
Ready to Give up - Your help will be much appreciated.
My website
|
|
|
|
|
Hello Colin,
Colin Angus Mackay wrote: You cannot have an instance of an interface. You can only have a reference to one.
Ok, ok!
It should be obvious that I'm not using the write programming vocabulary!
Colin Angus Mackay wrote: The instance is of some concrete object that happens to implement the interface.
That's what I was trying to explain! (Did it in a poor way, off course!)
So again my question:
Would it have the same effect (Exception):
The method returns a reference to the interface, which was implemented from classA but casted from classB (which also implements the interface)?
-- modified at 1:31 Tuesday 10th July, 2007
Wa sjust reading the answere from Vikram A Punathambekar [^]
Thanks for your time and patience!
All the best,
Martin
|
|
|
|
|
Hello Colin,
Maybe you find time to look threw my example[^]!
Hope you can help me!
Thanks again for your time!
All the best,
Martin
|
|
|
|
|
Martin# wrote: But it was instanciated as IResponseProcessingCSP
Apart from the fact that an interface cannot be instantiated, this code will work perfectly fine:
ITheInterface iface = new ClassThatImplementsITheInterface(); As far as the underlying object is concerned, it does NOT matter what type you use to reference it*, what matters is the ctor used to instantiate the type.
* As long as it represents a valid cast, of course.
Cheers,
Vıkram.
After all is said and done, much is said and little is done.
|
|
|
|
|
Hello,
Thanks for your help!
I think I have to do a little test programm to try something out!
All the best,
Martin
|
|
|
|
|
I just did the test project and found out what I allready assumed!
My interface:
public interface ITest
{
bool PTest
{
get;
set;
}
}
My ClassA (derived from Control, implementing ITest plus an additional property)
public class CTest_A : System.Windows.Forms.Control, ITest
{
public CTest_A()
{
}
#region ITest Member
private bool _ptest=false;
public bool PTest
{
get
{
return _ptest;
}
set
{
_ptest = value;
}
}
#endregion
private bool _padditional_A=false;
public bool PAdditional_A
{
get
{
return _padditional_A;
}
set
{
_padditional_A = value;
}
}
}
My ClassB (also derived from Control, also implementing ITest plus an additional property)
public class CTest_B : System.Windows.Forms.Control, ITest
{
public CTest_B()
{
}
#region ITest Member
private bool _ptest=false;
public bool PTest
{
get
{
return _ptest;
}
set
{
_ptest = value;
}
}
#endregion
private bool _padditional_B=false;
public bool PAdditional_B
{
get
{
return _padditional_B;
}
set
{
_padditional_B = value;
}
}
}
My test class:
Has a an instance of CTest_B and whant's to cast it to CTest_A over the Interface, which is returned by a method!
CTest_B C_B = new CTest_B();
private ITest GetIReference()
{
return C_B as ITest;
}
public Form1()
{
InitializeComponent();
CTest_A C_A = (CTest_A)GetIReference();
}
Do I get something totaly wrong, or is it like I assumed?
Please help!
All the best,
Martin
|
|
|
|
|
Full disclosure: I didn't read all of your code. That said...
Your GetIReference() returns an ITest reference, according to the sig. What it actually returns is a CTest_B object. No problems there. However, when you say CTest_A C_A = (CTest_A)GetIReference(); you are essentially trying to cast a CTest_B object to a CTest_A reference. This will work only if CTest_A is an ancestor of CTest_B or a cast is defined. You haven't, so it bombs.
Try making GetIReference() return an instance of CTest_A (don't change the sig!) and you will see what I meant. Greetings to Österreich.
Cheers,
Vıkram.
After all is said and done, much is said and little is done.
|
|
|
|
|
Hello,
Vikram A Punathambekar wrote: you are essentially trying to cast a CTest_B object to a CTest_A reference. This will work only if CTest_A is an ancestor of CTest_B or a cast is defined. You haven't, so it bombs.
YEP,
That is what I was assuming from the beginning on and trying to explain the whole time!
Because I think that's the problem of joelbasson[^].
Vikram A Punathambekar wrote: Try making GetIReference() return an instance of CTest_A (don't change the sig!) and you will see what I meant
You mean that the compiler tells me that it's not possible?
Vikram A Punathambekar wrote: Greetings to Österreich.
Thanks very much!
Nice greetings to India as well.
All the best,
Martin
|
|
|
|
|
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...
|
|
|
|
|