Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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>

Posted
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>

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...