Question Problem With Arrays

keseykid

New member
Joined
Sep 27, 2010
Messages
3
Programming Experience
Beginner
Hello All, Thanks in advance for any help,

I am attempting to create an Array for a site I am building, the code I am including is purely to test the Array.

VB.NET:
 Partial Class _Default
    Inherits System.Web.UI.Page
    Dim strName, strNumber As String
    Dim Info(0 To 1, 0 To 1) As String

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Info(0, 0) = txtName.Text
            Info(0, 1) = txtNumber.Text
            txtName.Text = " "
            txtNumber.Text = " "
        Catch ex As Exception
            MsgBox("ERROR")
        End Try

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            Info(1, 0) = txtName.Text
            Info(1, 1) = txtNumber.Text
            txtName.Text = " "
            txtNumber.Text = " "
        Catch ex As Exception
            MsgBox("ERROR")
        End Try

    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            txtShow.Text = Info(0, 0) & Info(0, 1)
        Catch ex As Exception
            MsgBox("ERROR")
        End Try

    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        Try
            strName = Info(1, 0) & Info(1, 1)
            txtShow.Text = strName
        Catch ex As Exception
            MsgBox("ERROR")
        End Try

    End Sub
End Class


I am attempting to use Button1 to put the name and number of txtName and txtNum into Array slot 0,0 and 0,1. The Button2 does the same thing for Array slot 1,0 and 1,1. That part seems to work good but the part where I attempt to print the array slots to txtShow, nothing happens. I am assuming its something to do with the difference between vb.net and asp.net, any ideas?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,335
Location
Sydney, Australia
Programming Experience
10+
There is no diffreence between VB.NET and ASP.NET that you can measure. They are apples and oranges. One is a programming language and the other is a technology for creating web applications. When you create an ASP.NET app, it must use a programming language for server-side processing. That language might be VB.NET or C# or some other. The difference you're talking about is between Windows applications, e.g. Windows Forms, and web applications, i.e. ASP.NET.

Your problem is the stateless nature of the web. Each time the user clicks a Button they are basically starting a new instance of your application. A new instance of your _Deafult class means a new array object. The array you are displaying is not the same array object that you populated. The array object that you're populating ceases to exist as soon as that post back completes.

If you want data to survive between requests then you must store it in a session variable.
 

keseykid

New member
Joined
Sep 27, 2010
Messages
3
Programming Experience
Beginner
Thank you, it is late here so i will look into session variables tomorrow. Thanks for the Reply, I will post results.
 
Top Bottom