Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
What type is THIS page when passing it?
Message
From
19/02/2008 10:11:23
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 2.0
Miscellaneous
Thread ID:
01293614
Message ID:
01293768
Views:
12
>>>I want to pass object 'this' from a page code to a static method in a class in .cs file. In the called method, I need to define what type is this object. How do I define it?
>>
>>If you use standard ASP.NET projects the class name is dynamically generated so the name is not available to you, at least not for the final class. You CAN pass the name of your code file class however.
>>
>>loObj.MyMethod( (MyNamespace.MyPage) this );
>>
>>With Web Application projects you can define the name of the generated class as well and you can use that name instead and get full support.
>>
>>In general though I don't think this is a good idea. You should never pass a Page object to anything. If anything you should create an interface that has the properties/methods you want to expose and then have the page in question implement that interface, but even that is not a good idea. THere's no reason you should ever pass a user interface component around.
>>
>>Instead any data you care about should be attached to some sort of data object (or a business object) and you acan then pass THAT around more effectively.
>>
>
>I think the last paragraph defines what I should learn to do - create some kind of a business object and pass it around. Let me explain why I need to pass THIS/PAGE (and I agree and learned that it is not possible) to a method of another class. In that method of another class I need to get .Text values of the page controls. E.g. This.MyTextBox.Text and so on. These values are only "available" in the Page class of the application, however. So I guess what you are saying is I need to create an object, set values of all my control .Text to this object and then pass it around. What I don't understand is where to start <g>. This object that will be passed around, where do you create it, and how do you use (instantiate) it in the page class?
>
>If the answer to my questions are too basic and require too much of explanation on your part, feel free to ignore it. I won't take it personally, I will just have to hit the books.
>

If you don't have very many values you need to pass in you could pass them individualy. It is better to create another class in the same namespace or a business namespace that contains properties to hold all the values. Then in your button or whatever method you are using to instantiate the new page, you could instantiate an instance of the class and store all the .text values into those properties. Then pass this object over to your next page. Another idea depending on the individual situation would be to make this other class use either static or use a singleton pattern so you don't have to pass it. I have used this approach many times for things like settings classes and such. It is like Rick said rather poor practice and not really necessary to pass in a UI object.

If you need examples, I have some I would share.
Tim
Timothy Bryan
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform