Re: [RESOLVED] Cant Quit :(
if you use the Type.InvokeMethod of your object, you can often avoid having to specify extra paramters ( it's a way around the Optional items of a method / function )
eg:
take the Webbrowser Control, in C# you must specify the ref objects when navigating
VB Code:
[COLOR=Green]/// if you use the normal method, you pass all the parameters...[/COLOR]
browser.Navigate2("the url" , ref object , ref object , ref object , ref object);
but you can avoid using all the extra bits like this ...
VB Code:
[COLOR=Green]/// to bypass any optional paramters ...[/COLOR]
[COLOR=Blue]object[/COLOR][] args = {"http://google.co.uk"}; [COLOR=Green]/// ignore the extra parameters[/COLOR]
axWebBrowser1.GetType().InvokeMember("Navigate2" , System.Reflection.BindingFlags.InvokeMethod , [COLOR=Blue]null[/COLOR], axWebBrowser1 , args);
btw, thanks for the Congrats :wave:
Re: [RESOLVED] Cant Quit :(
How come if you can do it that shorter way you dont have to explicitly instanciate the object as you dont get an "Object not set to an instance ..." error?
Re: [RESOLVED] Cant Quit :(
If you cast something to an object, you make an object instance, and you can pass a reference to it without keeping one yourself. So my code is the same as yours without actually storing local references to the newly created objects.
Re: [RESOLVED] Cant Quit :(
I'm still new to C# PG. Could you explain a little more? :blush: "(object) false" is casting false to an object varaible type?
Re: [RESOLVED] Cant Quit :(
Yep that's it. "false" is obviously a Boolean which is a value type. The brackets () in front of a variable is the cast operator. Inside the brackets you specify what type you want to cast to. In this case we are just casting to a generic object. The process here of casting a value type to a reference type (object) containing the value is known as "boxing", and there is a decent page detailing it here. Going the other way is, as you would expect, "unboxing". In VB.NET, I think the one-line version is unachievable, without using an object-typed variable and setting that variable to your value-type variable, due to the lack of a cast operator.
Re: [RESOLVED] Cant Quit :(
Ok, Thanks. Seems C# is reverse from vb.net when it comes to variables. Maybe now my dyslexia will come in handy. :)
Re: [RESOLVED] Cant Quit :(
"CObj(False)" is the VB.NET equivalent of "(object)false" in C#. I doubt that the IL is exactly the same but the effect within your app is the same: a reference-type Object instance containing the boolean value False is created.
Re: [RESOLVED] Cant Quit :(
I thought C...() were functions, so it would be a little bit different under the hood. Although, the JIT compiler might look for and optimise that.
Re: [RESOLVED] Cant Quit :(
I'm quite sure that the IL is different in each case, but using CObj, et al. is not like using most VB.NET functions:
Quote:
These functions are compiled inline, meaning the conversion code is part of the code that evaluates the expression. Execution is faster because there is no call to a procedure to accomplish the conversion.