Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 7th, 2004, 10:57 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default Local reference in Global function

I have a module containing a global function, and I want it to have access to the calling page's controls.
Code:
Public Sub(parameters)
    If condition is true Then
        myControl.Visible = True
    End If
End Sub
myControl shows as 'Not Declared'. I understand this, but how do I reference it properly?

Thanks in advance,
Colonel

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old July 7th, 2004, 11:10 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

What is parameters in this case? A control, or the entire page?
Do you want the Sub routine to change a (random) control in the page, or a specific control?

In the first scenario, pass the control in the page:

MySub (Button1)

Then have the MySub sub accept a Control as a parameter.

Otherwise, pass the entire form:

MySub (Me)

and have the MySub accept a Page object. You can use the Controls collection of the Page instance to find your control.

If you elaborate a bit about what you're trying to accomplish, I may be able to come up with a better or more detailed solution.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Wandering Star by Portishead (Track 5 from the album: Dummy) What's This?
 
Old July 7th, 2004, 11:19 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The parameter in this case is a string. It's used in a db lookup; if the lookup returns zero rows, then I want myControl (a label) to be visible.

I guess I could pass the page, but it seems like overkill for just switching Visible on a label?

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old July 7th, 2004, 11:27 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Since you're passing a reference, not a copy of the page, there is not that much overhead. But if you know it's going to be a label, pass a label:

(ASPX Page)
Dim MyLabel1 As Label
DetermineVisibility(MyLabel1, true)

(Sub Routine)
Public Sub DetermineVisibility(ByRef TheLabel As Label, condition As Boolean)
  If condition Then
    TheLabel .Visible = True
  Else
    TheLabel .Visible = False
  End If
End Sub

This way, you have strongly typed reference to a Label instance, so you could set other Label specific properties / methods as well.
If you want a generic method that can handle Textboxes, buttons and so on as well, change the type of the TheLabel variable to Control.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Missed by P.J. Harvey (Track 2 from the album: Rid Of Me) What's This?
 
Old July 7th, 2004, 11:42 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Or why not simplify this whole situation and do this...

(ASPX Page)
Dim MyLabel1 As Label
MyLabel1.Visible = DetermineCondition(<parameters>)

(Sub Routine)
Public Function DetermineCondition(<parameters>) As Boolean
If <condition> Then
    Return True
Else
    Return False
End If
End Function

This would help separate your business logic (determining some condition) from your UI logic (setting visibility) while also removing the requirement of passing UI control(s) to the helper method.
 
Old July 7th, 2004, 12:08 PM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually, Peter I like that solution better for the "cleanliness" of not having to pass an object, and better separation of logic and UI.

Thanks guys!

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.





Similar Threads
Thread Thread Starter Forum Replies Last Post
comapring global variable value to local variable amhicraig XSLT 6 December 5th, 2007 12:16 PM
COUNTIF Function can't reference other workbooks kuznickic Excel VBA 1 October 5th, 2007 04:35 AM
hazy on local function scope Brad C BOOK Beginning Lua Programming ISBN: 978-0-470-06917-2 2 March 31st, 2007 11:49 AM
Copy Local, Reference property ps2goat .NET Framework 1.x 1 October 24th, 2005 03:13 PM
Add reference to Global Assemblies happygv ASP.NET 1.0 and 1.1 Basics 10 May 27th, 2004 02:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.