Hammy Posted November 11, 2004 Posted November 11, 2004 I am looking at the following code, and wondering about the variable declarations before the Page_Load event. Specifically, I am wondering why they would declare them as private. I understand that private means that the code is only available to the scope it is declared in, but I am wondering what declaring a gobal page variable as private acomplishes. <script runat="server"> Private i as Integer Private strOutPut as String = "" Sub Page_Load(obj as Object, e as EventArgs) dim xmldoc as new XMLDocument() try xmldoc.Load(Server.MapPath(("books.xml")) ShowTree(xmlDoc.DocumentElement) catch ex as Exception strOutput = "Error accessing XML file" end try output.Text = strOutput End Sub </script> Quote
Administrators PlausiblyDamp Posted November 11, 2004 Administrators Posted November 11, 2004 (edited) It means the variable is available to all functions in the current page, although in the code posted above is totally redundant and strOutPut could have been declared within the Page_Load anyway... Edited April 2, 2007 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Hammy Posted November 12, 2004 Author Posted November 12, 2004 It means the variable is available to all functions in the current page' date=' although in the code posted above i is totally redundant and strOutPut could have been declared within the Page_Load anyway...[/quote'] Sorry, there was actually another function that makes use of i and strOutPut that I didn't post. OK, do I guess what is confusing me is why bother with the private? I mean if the variables are available to all functions in the current page, what's the difference with a simple dim? Here's another one I am looking at...whay make i,j & strOutput private and xmlDoc public? Sorry to be a pain, but I am really trying to figure out why one would choose to make these variables private, public, or just a simple dim. <%@ Page Language="VB" %> <%@ Import Namespace="System.Xml" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <script runat=server> private i, j as integer private strOutput as string = "" public xmldoc as new XMLDataDocument() sub Page_Load(obj as object, e as eventargs) if not Page.IsPostBack then GetData() BindControl() end if end sub sub UpdateBtn_Click(obj as object, e as eventargs) dim Title as TextBox dim Genre as TextBox dim Style as TextBox dim Price as TextBox GetData() 'update data For i = 0 To DataGrid1.Items.Count-1 Title = DataGrid1.Items(i).FindControl("Title") Genre = DataGrid1.Items(i).FindControl("Genre") Style = DataGrid1.Items(i).FindControl("Style") Price = DataGrid1.Items(i).FindControl("Price") xmldoc.DataSet.Tables(0).Rows(i)("title") = _ Title.Text xmldoc.DataSet.Tables(0).Rows(i)("genre") = _ Genre.Text xmldoc.DataSet.Tables(0).Rows(i)("style") = _ Style.Text xmldoc.DataSet.Tables(0).Rows(i)("price") = _ Price.Text Next try xmldoc.Save(Server.MapPath("books.xml")) catch output.Text = "Error updating data" end try BindControl() end sub sub GetData() try xmldoc.DataSet.ReadXml(Server.MapPath("books.xml")) catch ex as Exception output.Text = "Error accessing XML file" end try end sub sub BindControl() DataGrid1.DataSource = xmldoc.DataSet DataGrid1.DataMember = xmldoc.DataSet.Tables(0). _ TableName DataGrid1.DataBind() end sub </script> Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.