In lack of a wiser answer... I don't mind... Cheers
Gonçalo A.
In lack of a wiser answer... I don't mind... Cheers
Gonçalo A.
bwahahahahaha I really had to laugh on this one... And you're a Microsoft MVP??? Well... you're from VB, so... I guess I have to understand. First... it's not a variable matter. It's a closures matter. And I said from the start what would happen; you were the one saying otherwise... so... don't try to cover up your stupidity now... And... weren't you supposed to be here to help? Get over your frustration, it's unhealthy for this forum users. Cheers
Gonçalo A.
Exactly! Now that's an answer. Thank you. Cheers
Gonçalo A.
If the code does the same, it WAS IRRELEVANT; if it was relevant, the code would do something different. I'll just state the output of that code... When you close the form, two message boxes will appear... One will say "closing", and the other one will say "closing". I said this right from the start.
Gonçalo A.
You don't even need to put the if there... Now... all this not to answer my question?? Come one... you can do better than that, can't you? If I said that it wasn't relevant, it's because it wasn't... Now... you can remove the if... are you gonna answer my question or are you just gonna pretend you're so very awesome? Cheers
Gonçalo A.
Cannot, or it doesn't happen? Or... if it could not happen, then why, instead of just... cannot happen. Try it. Just as I described on my previous post. Create a new project, with a form, and type that out... Even I did this, to prove myself I wasn't fooling anyone; so I am not freaking ommiting anything... sheesh... This happens because when you add code like that, in runtime, the compiler will hold to the references and not to the values; the result is that both will grab the last value of the reference. That's why it won't happen if you switch the text variable to inside the if, or if you added static text like MessageBox( "closed" );. Advice... do try it first, before saying it CANNOT happen. I won't hold a grudge on you if you try, I promise... Cheers
Gonçalo A.
Ok... the thing is... I had solved it since I posted the first message here.. I said it even... I only spoke about the positioning of the variable and the association to runtime delegate. This is que only thing that I questioned:
Kensho wrote:
Now... can anyone explain me why does this happen? The delegate uses the reference instead of the value?
delegate... value... I don't understand why the big fuss over a simple question. But nevermind... I already got my answer... maybe someone can pick this up and find it useful, as a simple piece of information, as it is. Cheers
Gonçalo A.
Skippums wrote:
Why not just send the strings directly, without assigning to a variable?
ahahahahahah :laugh: that's right, that would solve it. :) But that example is just an illustration of my problem. The actual code gets the contents from somewhere else dynamically, and it doesn't display a message box, it does something else... this was just to simplify. I actually solved this by moving the value holder inside the context, and this way, a new reference is always created.
Gonçalo A.
Cannot... or won't?? You tried it? Oh, there's just one detail on the code... replace the '1's with 'true'. Forgot that small detail in c#. Just Create a form, and on the constructor, after the initializations, add these lines: string text; if ( true ) { text = "closed"; FormClosed += delegate( object sender, FormClosedEventArgs e ) { MessageBox.Show( text ); }; } if ( true ) { text = "closing"; FormClosing += delegate( object sender, FormClosingEventArgs e ) { MessageBox.Show( text ); }; }
And tell me if the output isn't "closing", "closing".
Gonçalo A.
Actually, no.. it has nothing to do with the something or something_else... that's why I didn't post it. I'll try to simplify the code... string text; if ( 1 ) { text = "closing"; someForm.FormClosing += delegate( object sender, FormClosingEventArgs e ) { MessageBox.Show( text ); }; } if ( 1 ) { text = "closed"; someForm.FormClosed += delegate( object sender, FormClosedEventArgs e ) { MessageBox.Show( text ); }; }
Hope this clears out.
Gonçalo A.
Consider the following code... string text; foreach ( something ) { if ( something ) { text = "closing"; someForm.FormClosing += delegate( object sender, FormClosingEventArgs e ) { MessageBox.Show( text ); }; } if ( something_else ) { text = "closed"; someForm.FormClosed += delegate( object sender, FormClosedEventArgs e ) { MessageBox.Show( text ); }; } }
The foreach will have a something, and then a something_else, in this order. So, what will happen here, is that both events will trigger a MessageBox displaying "closed" text. To fix this, I passed the string text inside the foreach, and the issue isn't an issue anymore. Now... can anyone explain me why does this happen? The delegate uses the reference instead of the value?
Gonçalo A.
Ok, continuing on the context of executing reflected code... Is there any way of adding a web reference in runtime? Alternatively, what options do I have on invoking a web service without adding the web reference? Thank you
Gonçalo A.
Yes, I noticed that already. Thanks.
Gonçalo A.
Yes, that was how I was doing it. The original was a virtual method, but I expected him to override automatically. Though, it does make more sense this way, and offers more possibilities. Thank you, once again, for the explanation.
Gonçalo A.
override.... right. I feel like a newbie, but I guess these things happen when moving on to a new language :p Thank you for the quick reply, you helped a lot. :)
Gonçalo A.
Let's see if I can explain my small doubt. I can go around this, but as I was coding something, I got curious if something wasn't possible to do in c#. In C++, you could have something like this: class A { public virtual void method() { // something } } class AA : public A { public void method() { // something else } } class AB : public B { public void method() { // even something else } } A *obj; if ( something ) { obj = (A)( new AA() ); } else { obj = (A)( new AB() ); } obj->method();
What happens here is that when you invoke the method function, you'll be executing the implementation from AA or from AB and not from A. Though, in C#, if I try to do something like this, you'll be executing the implementation from A. That fact to me, is a shame... I liked this behavior. And as pointers in C# only exist for values, I suppose this is not possible with C#. Best regards
Gonçalo A.
Yes, I kind of wanted something very weird, I said it. But it's only logical that it's not possible. That would mean to alter a dll in runtime... The decision I came up to go around this is using reflection. I send in a sort of a sandbox with variables that I might want to alter in the code, fetch the code from the file, encapsulate it inside a neat namespace with a class and a method, compile it, and run it. Seems to do the trick. I had to create a few "syntax hacks" to access the sandbox, like... $var as Label.Text = "something";
and in runtime replace the "pretty" line into something more ugly to access the sandbox. It works, and does the job very well. I also needed to create two more "syntax hacks" in case I need an extra reference, or to use an extra namespace that is not included by default in the assembly chunk. #assembly "System.Drawing.dll"; #using System.IO;
Those three "hacks" are not very elegant, but... it allows me to have this done in a very generic way, which, in the current project, is the only way. Thank you all for the replies... and... if anyone as any thoughts on this, please let me know.
Gonçalo A.
Those were my thoughts exactly, that I was stuck with reflection. Though, the context is a problem... I cannot fit the code in the context that I want. Anyways, thank you for the replies.
Gonçalo A.
Well, the thing is, the code I do want to run, is not something I know. It's something I retrieve at runtime, let's say, from a file. I open a file, read the content, and then, have an event to run that content. That is what I want, so I can't have a B.DoWhatever function. I know this is something awkward to want... but this would be what I need. Any thoughts? :) Thank you for the reply
Gonçalo A.
Even though... if I create an EventHandler, I'll still have the same problem. If the inner code is coming from another source, I still don't have a way to "reflect" that code. Am I right? Thanks for the reply, btw
Gonçalo A.