sbelgrave
asked on
Passing User Controls By Reference From a Stand Alone Class File in C#.net
Hi there,
Hoping someone can tell me if this is even possible. I'm having trouble passing a user control by ref .
Heres the scenario.:
File 1 : General_Functions.cs (located in the App_Code folder)
-------------------------- ---------- ---------- ---------- ---------- ---------- --
public class General_Functions
{
:
:
public void myFunc(ref UserControl uc){
// blah blah blah
}
}
File 2 : MyPage.aspx.cs
-------------------------- ---------- ---------- ---------- ---------- ---------- --
General_Functions gf = new General_Functions();
gf.MyFunc(ref my_uc); // my_uc is some user control on my page
PROBLEM : baseicallly, the compiler continues to tell me that my syntax is incorrect. the error message is :
cannot convert from 'ref ASP.user_controls_sample_u c_ascx' to 'ref System.Web.UI.UserControl'
I clearly get that somehow it doesn't see the two as the same but i don't understand why. Isn't an .ascx file a USER CONTROL ? So why won't it let me pass in an instance (by ref) of the user control that I am using on MyPage.aspx ? Please HELP. I've been staring this down all day and I've run out of ideas.
Hoping someone can tell me if this is even possible. I'm having trouble passing a user control by ref .
Heres the scenario.:
File 1 : General_Functions.cs (located in the App_Code folder)
--------------------------
public class General_Functions
{
:
:
public void myFunc(ref UserControl uc){
// blah blah blah
}
}
File 2 : MyPage.aspx.cs
--------------------------
General_Functions gf = new General_Functions();
gf.MyFunc(ref my_uc); // my_uc is some user control on my page
PROBLEM : baseicallly, the compiler continues to tell me that my syntax is incorrect. the error message is :
cannot convert from 'ref ASP.user_controls_sample_u
I clearly get that somehow it doesn't see the two as the same but i don't understand why. Isn't an .ascx file a USER CONTROL ? So why won't it let me pass in an instance (by ref) of the user control that I am using on MyPage.aspx ? Please HELP. I've been staring this down all day and I've run out of ideas.
ASKER
I tried your suggestion but it looks like it still gives error. The error when i try to cast it as a UserControl is :
"A ref or out argument must be an assignable variable"
My thinking would be that this can't work because when you're passing by ref you are 'pointing' to that object so it makes no difference what you cast it as, it just wants to know what to point to when you write 'ref X' . But of course, that could be completely wrong. Any other suggestions ?
"A ref or out argument must be an assignable variable"
My thinking would be that this can't work because when you're passing by ref you are 'pointing' to that object so it makes no difference what you cast it as, it just wants to know what to point to when you write 'ref X' . But of course, that could be completely wrong. Any other suggestions ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hey Dash,
Maybe my example above wasn't clear enough. Referencing a user control from a code behind page is not the problem. The two files i have in my example above show a code behind page (MyPage.aspx.cs ) and a Stand ALONE cs class defined in the App_Code directory. The goal is to try and pass in a user control located on the MyPage.aspx page. So in the code behind for MyPage.aspx I need to be able to pass the user control by reference to the method located in the App_Code folder . I hope that makes this clearer. Thank you for looking into it.
Maybe my example above wasn't clear enough. Referencing a user control from a code behind page is not the problem. The two files i have in my example above show a code behind page (MyPage.aspx.cs ) and a Stand ALONE cs class defined in the App_Code directory. The goal is to try and pass in a user control located on the MyPage.aspx page. So in the code behind for MyPage.aspx I need to be able to pass the user control by reference to the method located in the App_Code folder . I hope that makes this clearer. Thank you for looking into it.
ASKER
My apologies Dash,
While youre example didn't exactly mimic my situation it was completely correct in what to do. Apparently if you have a user contol on your aspx page it is not really of type UserContol unless you cast it as another variable like you did in your code above. While I find that really strange it is clearly A SOLUTION to the problem and I glady accept it. Thank you! - Scott
While youre example didn't exactly mimic my situation it was completely correct in what to do. Apparently if you have a user contol on your aspx page it is not really of type UserContol unless you cast it as another variable like you did in your code above. While I find that really strange it is clearly A SOLUTION to the problem and I glady accept it. Thank you! - Scott
Good to know problem got Solved.
Thanks
Thanks
gf.MyFunc(ref (System.Web.UI.UserControl
You would only be able to use properties and methods from UserControl inside of myFunc, though.