Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2492
  • Last Modified:

Print array list to label C#

This is a follow up question to one I posted earlier. I am trying to do the same thing I did in VB in C#. The problem is the solution was to use viewstate While I am totally comfortable with the answer I got I now have to do it in C#. I don't know the first thing about viewstate in C#. The question was as follows:

So this is so simple its embarassing. I cannot figure out what the problem is that is causing this. I have a simple calculator that prints the results in a textbox. I then have a button that is supposed to write the result to an arraylist. Then another button that prints the results to a label control. I'm not sure if the arraylist is being populated or not. I try to manually add an item:
resultslist.add("8") but this does not even add to the array as far as I can tell. Please see the code snippet below. Thank You.

The code that worked in VB is:


<html>
<head runat="server">
    <title></title>
<script runat = "server" language = "VB">
   
    Dim resultslist As New ArrayList
   
   
   
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack Then
            If Not ViewState.Item("resultslist") Is Nothing Then
                resultslist = ViewState.Item("resultslist")
            End If
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        Label1.Text = number1 + number2
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        Label1.Text = number1 - number2
    End Sub


    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        Label1.Text = number1 * number2
    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
     
        Dim number1 As Double = TextBox1.Text
        Dim number2 As Double = TextBox2.Text
        If TextBox2.Text = "0" Then
            MsgBox("Cannot Divide By Zero")

        Else
            Label1.Text = number1 / number2
        End If
    End Sub

   
    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Label1.Text = "Result"
    End Sub

    Protected Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        Label1.Text = "Result"
    End Sub

   
    Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        resultslist.Add(Label1.Text.ToString)
        ViewState.Item("resultslist") = resultslist
       
    End Sub

    Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            For Each item In resultslist
            Label2.Text = Label2.Text + item & " / "
            Next
       
    End Sub
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>ENTD464 Calculator</h1>
    </div>
    <asp:TextBox ID="TextBox1" runat="server" Width="70px"></asp:TextBox>
    <br />
    <asp:Button ID="Button1" runat="server" Text="+" Width="16px" />
    <asp:Button ID="Button2" runat="server" Text="-" />
    <asp:Button ID="Button3" runat="server" Text="*" />
    <asp:Button ID="Button4" runat="server" Text="/" />
    <br />
    <asp:TextBox ID="TextBox2" runat="server" Width="70px"></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label1" runat="server" Enabled="False" Text="Result"></asp:Label>
    <br />
    <br />
    <asp:Button ID="Button5" runat="server" Text="Store" onclick="Button5_Click"
        Width="101px" />
    <br />
    <br />
    <asp:Button ID="Button6" runat="server" onclick="Button6_Click"
        Text="Show Array" />
    <br />
    <br />
    <asp:Label ID="Label2" runat="server"></asp:Label>
    </form>
</body>
</html>



This is what I have in C# now, but of course it is not working:

<html>
<head>
    <title></title>
  <script runat = "server" language = "C#">
  ArrayList resultslist = new ArrayList();
         
    protected void Button1_Click(object sender, EventArgs e)
    {
        double number1 = Convert.ToDouble(TextBox1.Text);
        double number2 = Convert.ToDouble(TextBox2.Text);
        string result = Convert.ToString(number1 + number2);
        Label1.Text = result;
 
    }
    protected void Button2_Click(object sender, EventArgs e)
    {

        double number1 = Convert.ToDouble(TextBox1.Text);
        double number2 = Convert.ToDouble(TextBox2.Text);
        string result = Convert.ToString(number1 - number2);
        Label1.Text = result;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        double number1 = Convert.ToDouble(TextBox1.Text);
        double number2 = Convert.ToDouble(TextBox2.Text);
        string result = Convert.ToString(number1 * number2);
        Label1.Text = result;
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        double number1 = Convert.ToDouble(TextBox1.Text);
        double number2 = Convert.ToDouble(TextBox2.Text);
        if (TextBox2.Text == "0")
        {
            Label1.Text = "Cannot divide by zero";
        }
        else
        {

            string result = Convert.ToString(number1 / number2);
            Label1.Text = result;
        }
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
    resultslist.Add(Label1.Text);
    }

    protected void Button6_Click(object sender, EventArgs e)
    {
        foreach  (string item in resultslist)
        {
            Label2.Text = Label2.Text + item;
        }
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>ENTD Calculator</h1>
    </div>
    <asp:TextBox ID="TextBox1" runat="server"  
        Width="70px"></asp:TextBox>
    <br />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
        style="width: 22px" Text="+" />
    <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="-" />
    <asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="*" />
    <asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="/" />
    <br />
    <asp:TextBox ID="TextBox2" runat="server"  
        Width="70px"></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label1" runat="server" Text="Result" Enabled="False"></asp:Label>
    <br />
    <br />
    <asp:Button ID="Button5" runat="server" Text="Store" onclick="Button5_Click" />
    <br />
    <br />
    <asp:Button ID="Button6" runat="server" Text="Button" onclick="Button6_Click" />
    <br />
    <br />
    <asp:Label ID="Label2" runat="server" Enabled="False"></asp:Label>
    </form>
</body>
</html>
0
psychokraft
Asked:
psychokraft
2 Solutions
 
ddayx10Commented:
OK it's was a little confusing to figure out, but I think this is what you are looking for<see code>:
ArrayList resultslist = new ArrayList();


    protected void Page_Load(object sender, EventArgs e)
    {
        //If IsPostBack Then
        //    If Not ViewState.Item("resultslist") Is Nothing Then
        //        resultslist = ViewState.Item("resultslist")
        //    End If
        //End If

        if (IsPostBack)
        {
            if (ViewState["resultslist"] != null)
            {
                resultslist = (ArrayList)ViewState["resultslist"];
            }
        }

    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        double number1 = Convert.ToDouble(TextBox1.Text);
        double number2 = Convert.ToDouble(TextBox2.Text);
        string result = Convert.ToString(number1 + number2);
        Label1.Text = result;

    }
    protected void Button2_Click(object sender, EventArgs e)
    {

        double number1 = Convert.ToDouble(TextBox1.Text);
        double number2 = Convert.ToDouble(TextBox2.Text);
        string result = Convert.ToString(number1 - number2);
        Label1.Text = result;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        double number1 = Convert.ToDouble(TextBox1.Text);
        double number2 = Convert.ToDouble(TextBox2.Text);
        string result = Convert.ToString(number1 * number2);
        Label1.Text = result;
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        double number1 = Convert.ToDouble(TextBox1.Text);
        double number2 = Convert.ToDouble(TextBox2.Text);
        if (TextBox2.Text == "0")
        {
            Label1.Text = "Cannot divide by zero";
        }
        else
        {

            string result = Convert.ToString(number1 / number2);
            Label1.Text = result;
        }
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        resultslist.Add(Label1.Text);
        ViewState["resultslist"] = resultslist;

    }

    protected void Button6_Click(object sender, EventArgs e)
    {
        foreach (string item in resultslist)
        {
            Label2.Text = Label2.Text + item;
        }
    }

Open in new window

0
 
sonawanekiranCommented:
here is a link to convert vb to c# and viceversa
converter.telerik.com/
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now