Global variables

DColin
DColin used Ask the Experts™
on
Hi Experts,

I am using a web service API to retrieve holiday information, please consider the following pseudo code:

Sub ButtonOnClick_ApiGetCountryList

     countryList = webService.GetCountryList

End Sub

Sub ButtonOnClick_ApiGetHotelList

     hotelList = webService.GetHotelList(Country)

End Sub

Sub ButtonOnClick_ApiGetAirportList

     airportlList = webService.GetAirportlList(Country)

End Sub

Sub ButtonOnClick_ApiGetBeachlList

     BeachList = webService.GetBeachlList(Country)

End Sub

Once the user has clicked the get country button and the web service has delivered a list of countries I need a country from that list to be available to the other three subs. How do I do this without using a global variable or is it OK to use a global variable?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
Questions: How often does the webService information change regarding the lifetime of your app?
Is it a asp.net/winform project?
Are these lists used elsewhere in your app, appart  from the form/page you are displaying?

I will put countryList as global variable, but the others as cached form variables (say 5-10 minutes).
IT Consultant
Top Expert 2010
Commented:
I would avoid a global variable (it can make for some confusion and disorganization).  If all the subs that need access to the information are in the same class (i.e. on the same form) then you can use a class-level private variable - or use a separate class with "Shared" members.

Public Class Form1
	Private countryList As List(Of String)
	Private hotelList As List(Of String)
	Private airportList As List(Of String)
	Private beachList As List(Of String)

	Private Sub GetCountryListButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetCountryListButton.Click
		countryList = webService.GetCountryList()
	End Sub

	Private Sub GetHotelListButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetHotelListButton.Click
		hotelList = webService.GetHotels(countryList)
	End Sub

	Private Sub GetAirportListButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetAirportListButton.Click
		airportList = webService.GetAirports(countryList)
	End Sub

	Private Sub GetBeachsListButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetBeachsListButton.Click
		beachList = webService.GetBeachs(countryList)
	End Sub
End Class

Open in new window

I would set it up as a class library, using stored procedure to pull your country information. Then you simply access it with one line of code in each section. Something like classLibraryName.className.attribute. In your example it a possibility would be clsLibrary.Country.countryName or clsLibrary.Country.countryID.


http://www.csunit.org/tutorials/tutorial6/
http://www.codeproject.com/KB/vb/OOPS_In_VBNET.aspx
Hi, you have to follow the these queries.

Into the webService.GetCountryList() query you have to write one more filter that the id of country is matched with the id of countryid which is into the hotellist table and the id of country is matched with the id of countryid which is into the airportlist table and the id of country is matched with the id of countryid which is into the beachlist table. You must have to one id field for storing the id of country into each table for relationships between these four tables.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial