Join 131,728 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,344 people online right now. Registration is fast and FREE... Join Now!
I have a project that requires me to make an application that accepts numbers from the user and then get the average of the numbers entered.
A number is entered into a textbox you press the button and then enter another number in the textbox and so on.
I figured a list would be the best way to go for this but I have encountered a problem. How do i get the numbers from the textbox into the list?
The examples I have seen for lists only show how to hard code them. I need for the value in the textbox to be added to the list everytime the button is pressed.
This is what i have come up with so far. I am gonna make a counter that counts how many times the button is pushed for the division. The only problem i have is filling the list.
CODE
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim numbers As New List(Of Integer) Dim sum As Integer = 0 Dim number As Integer
numbers.Add(TextBox1.Text)
For i As Integer = 0 To numbers.Count - 1 number = numbers(i) sum += number Next
By adding to the list, do you mean having each entered value stored or literally add each value together and keep a running total?
I assume you need this list to persist between clicks, is that correct?
Currently you have defined your list to accept integers, but you are trying to store a string. How do you want the values stored, as a string or integer?
I need each value entered stored and I need the list to persist between clicks.
The value needs to be stored as an integer for mathmatical purposes but I am assuming VB will type cast the value in the textbox to integer. The only problem I am having is that I cant seem to figure out how to store each value entered into the list.
This post has been edited by popper32: 10 Oct, 2008 - 07:08 PM
VB will not type cast the Text property to an Integer type and it needs to be of type Integer because that is what you specified for the List. You need to explicitly cast it to the correct data type. These is easily done using the Convert class.
CODE
numbers.Add(Convert.ToInt32(TextBox1.Text))
Now since you need the list to persist between clicks, then you need to make it a global variable so that it will retain its value.
CODE
Public Class Form1
'global variable Private numbers As New List(Of Integer)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
I am using Visual Basic 2008 Express Edition and I think the IDE automatically type casts the data. I havent gotten any exceptions when I run the code I posted above.
Thanks again, I scoured the internet for hours trying to figure this out.
I have a project that requires me to make an application that accepts numbers from the user and then get the average of the numbers entered.
A number is entered into a textbox you press the button and then enter another number in the textbox and so on.
I figured a list would be the best way to go for this but I have encountered a problem. How do i get the numbers from the textbox into the list?
The examples I have seen for lists only show how to hard code them. I need for the value in the textbox to be added to the list everytime the button is pressed.
That's weird because no .Net IDE type casts for you, when it comes from a TextBox it is a string unless you cast it
lol, you must be using another .net to me then as this works fine
CODE
Option Explicit On
Public Class Form1 Dim test As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click TextBox1.Text = "123" test = TextBox1.Text test = test + 1
My guess is that maybe express edition does not turn Option Strict On by default? If you have Option Strict Off I think vb will act like old vb6 and cast everything if it can.