ref typecast between form derived from Form class and Form class
-
Hi, I have a function like foo(ref System.Windows.Form objForm) and I need to pass a custom form which is derived from System.Windows.Form that is Custom form :- FormX : System.Windows.Form So, my question is how can I call function foo with object of FormX as ref argument that is something like FormX obj; foo (ref obj); // it is giving compilation error Please let me know the solution
Regards, Pankaj Sachdeva There is no future lies in any job but future lies in the person who holds the job
-
Hi, I have a function like foo(ref System.Windows.Form objForm) and I need to pass a custom form which is derived from System.Windows.Form that is Custom form :- FormX : System.Windows.Form So, my question is how can I call function foo with object of FormX as ref argument that is something like FormX obj; foo (ref obj); // it is giving compilation error Please let me know the solution
Regards, Pankaj Sachdeva There is no future lies in any job but future lies in the person who holds the job
You haven't stated the error but I guess it's because you haven't assigned
obj
to eithernull
or aFormX
instance. When passing by reference the object is passed into the function so it must exist (or be assigndnull
if a reference type). If you want the function to pass out theFormX
instance and not provide it to the method yourself, you should useout
instead ofref
. By the way, unless you've messed with theSystem.Windows
namespace, it should beSystem.Windows.Forms.Form
.Dave
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Hi, I have a function like foo(ref System.Windows.Form objForm) and I need to pass a custom form which is derived from System.Windows.Form that is Custom form :- FormX : System.Windows.Form So, my question is how can I call function foo with object of FormX as ref argument that is something like FormX obj; foo (ref obj); // it is giving compilation error Please let me know the solution
Regards, Pankaj Sachdeva There is no future lies in any job but future lies in the person who holds the job
Pass-by-reference parameters must match type exactly. The workaround is to do:
FormX objX = new FormX();
// cast down
Form obj = objX;// now you can call the foo method
foo(ref obj);However, since FormX is a reference type, your foo method doesn't even need the ref parameter keyword. c# passes it as a reference by default.
foo(System.Windows.Forms.Form obj)
{
// obj is passed as a reference by default since it's a reference type
} -
Pass-by-reference parameters must match type exactly. The workaround is to do:
FormX objX = new FormX();
// cast down
Form obj = objX;// now you can call the foo method
foo(ref obj);However, since FormX is a reference type, your foo method doesn't even need the ref parameter keyword. c# passes it as a reference by default.
foo(System.Windows.Forms.Form obj)
{
// obj is passed as a reference by default since it's a reference type
}Member 2463256 wrote:
However, since FormX is a reference type, your foo method doesn't even need the ref parameter keyword. c# passes it as a reference by default.
Not quite the same thing. The difference is this:
foo(ref Form obj)
{
// The caller's variable is changed to a new
// Form instance.
obj = new Form();
}foo(Form obj)
{
// Our LOCAL variable 'obj' is set to a new form, but
// the calling function will see no change.
obj = new Form();
}The "ref" keyword is similar to the "out" keyword, except that "out" only works in one direction, while "ref" both provides a value and allows it to be changed. Of course, in either case, using "ref" or not, changing a property of 'obj' will affect the caller's instance.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
Member 2463256 wrote:
However, since FormX is a reference type, your foo method doesn't even need the ref parameter keyword. c# passes it as a reference by default.
Not quite the same thing. The difference is this:
foo(ref Form obj)
{
// The caller's variable is changed to a new
// Form instance.
obj = new Form();
}foo(Form obj)
{
// Our LOCAL variable 'obj' is set to a new form, but
// the calling function will see no change.
obj = new Form();
}The "ref" keyword is similar to the "out" keyword, except that "out" only works in one direction, while "ref" both provides a value and allows it to be changed. Of course, in either case, using "ref" or not, changing a property of 'obj' will affect the caller's instance.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)Ian Shlasko wrote:
except that "out" only works in one direction, while "ref" both provides a value and allows it to be changed.
Not quite. The only difference between ref and out is that out ensures the value will be set before you return from the method. When ref is used, it does not ensure the variable will be set... it merely allows it. This allows for one to do something like this:
// Notice myForm was not initialized.
Form myForm;
SomeMethod(out myForm);
// This will work.
myForm.ShowDialog();Notice how this differs from ref:
// Notice myForm was not initialized.
Form myForm;
SomeMethod(ref myForm);
// This will NOT work (compile time error), because myForm may not have been initialized.
myForm.ShowDialog(); -
Ian Shlasko wrote:
except that "out" only works in one direction, while "ref" both provides a value and allows it to be changed.
Not quite. The only difference between ref and out is that out ensures the value will be set before you return from the method. When ref is used, it does not ensure the variable will be set... it merely allows it. This allows for one to do something like this:
// Notice myForm was not initialized.
Form myForm;
SomeMethod(out myForm);
// This will work.
myForm.ShowDialog();Notice how this differs from ref:
// Notice myForm was not initialized.
Form myForm;
SomeMethod(ref myForm);
// This will NOT work (compile time error), because myForm may not have been initialized.
myForm.ShowDialog();Well, your example illustrates the inverse, but still, I stand corrected. Forgot about the requirement that "out" variables are set. But basically... Normal: Values in, unmodified (Aside from setters/methods of object types) Ref: Values in, possibly different values out Out: Nothing in, values out
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
Pass-by-reference parameters must match type exactly. The workaround is to do:
FormX objX = new FormX();
// cast down
Form obj = objX;// now you can call the foo method
foo(ref obj);However, since FormX is a reference type, your foo method doesn't even need the ref parameter keyword. c# passes it as a reference by default.
foo(System.Windows.Forms.Form obj)
{
// obj is passed as a reference by default since it's a reference type
}Thank you everybody but in my case Member 2463256 reply was useful. Problem resolved!
Regards, Pankaj Sachdeva There is no future lies in any job but future lies in the person who holds the job