Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

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!




Help with Visual Basic 2008

 
Reply to this topicStart new topic

Help with Visual Basic 2008, Working with a List

popper32
post 10 Oct, 2008 - 12:33 PM
Post #1


New D.I.C Head

*
Joined: 10 Oct, 2008
Posts: 4

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.

User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 10 Oct, 2008 - 12:45 PM
Post #2


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,909



Thanked 116 times

Dream Kudos: 8450

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


Show us what you've written to do this assignment and we'll help you as much as you can.

VB 2008 is VB.NET so I'm moving this to the VB.NET forum smile.gif
User is offlineProfile CardPM

Go to the top of the page

popper32
post 10 Oct, 2008 - 02:16 PM
Post #3


New D.I.C Head

*
Joined: 10 Oct, 2008
Posts: 4

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

        Label1.Text = sum  ' divide by counter


    End Sub
End Class



User is offlineProfile CardPM

Go to the top of the page

Jayman
post 10 Oct, 2008 - 05:15 PM
Post #4


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,827



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


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?
User is online!Profile CardPM

Go to the top of the page

popper32
post 10 Oct, 2008 - 07:07 PM
Post #5


New D.I.C Head

*
Joined: 10 Oct, 2008
Posts: 4

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
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 10 Oct, 2008 - 07:33 PM
Post #6


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,909



Thanked 116 times

Dream Kudos: 8450

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


FYI VB.NET will not type case, you have to convert it to an integer yourself. Anything entered into a TextBox is considered a string
User is offlineProfile CardPM

Go to the top of the page

Jayman
post 10 Oct, 2008 - 07:36 PM
Post #7


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,827



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


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



Hope that helps explain it for you.
User is online!Profile CardPM

Go to the top of the page

popper32
post 11 Oct, 2008 - 04:49 AM
Post #8


New D.I.C Head

*
Joined: 10 Oct, 2008
Posts: 4

That did the trick, thank you very much.

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.

User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 11 Oct, 2008 - 08:17 AM
Post #9


using DIC.Core;

Group Icon
Joined: 26 Jul, 2007
Posts: 8,909



Thanked 116 times

Dream Kudos: 8450

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

absynthe
post 11 Oct, 2008 - 09:44 AM
Post #10


D.I.C Addict

****
Joined: 20 Sep, 2008
Posts: 757



Thanked 4 times
My Contributions


QUOTE(popper32 @ 10 Oct, 2008 - 01:33 PM) *

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.

Ive not used 08 yet! How is it?
User is offlineProfile CardPM

Go to the top of the page

jg007
post 11 Oct, 2008 - 10:29 AM
Post #11


New D.I.C Head

*
Joined: 23 Mar, 2008
Posts: 26



Thanked 1 times
My Contributions


QUOTE(PsychoCoder @ 11 Oct, 2008 - 09:17 AM) *

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

        MsgBox(test)

    End Sub
End Class

User is offlineProfile CardPM

Go to the top of the page

magicmonkey
post 11 Oct, 2008 - 11:28 AM
Post #12


D.I.C Regular

***
Joined: 12 Sep, 2008
Posts: 374



Thanked 64 times
My Contributions


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.
User is online!Profile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 10:27AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month