One dimensional arrays and classes

accumulating value in array

Page 1 of 1

9 Replies - 3117 Views - Last Post: 12 December 2010 - 03:45 PM Rate Topic: -----

#1 kchenry2003  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 11
  • Joined: 10-October 09

One dimensional arrays and classes

Posted 12 December 2010 - 11:34 AM

I have written a class for my one dimensional array
The idea is to collect user information, put the info into an array and accumulate the value of each item of the array so there is a running total. The problem is that the code is not totalling the array when my btnSum is clicked or maybe at all.
I am getting no errors, but I am also getting no total.

Following is my class
Public Class mySum
    Public _quan As Integer
    Sub New(ByVal q As Integer)
        _quan = q
    End Sub
    Property Quantity() As Integer
        Get
            Return _quan
        End Get
        Set(ByVal value As Integer)
            If value > 0 Then
                _quan = value
            Else
                _quan = 0
            End If
        End Set
    End Property
    Public Sub New()
        _quan = 0
    End Sub
    Public Function CalculateSums() As Integer
        Return _quan + _quan
    End Function

    Overrides Function ToString() As String
        Return String.Format("{0,10}", _quan)
    End Function
End Class


Here is my btnSum code
 Private Sub btnSum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSum.Click
        ' Display search items from description in lbitems
        Dim myNewSum As New mySum
        Dim sum As Integer
        Integer.TryParse(tbQuant.Text, myNewSum.Quantity)
        sum = myNewSum.CalculateSums()
        lblTotal.Text = sum.ToString
    End Sub

Any help will be greatly appreciated.

This post has been edited by kchenry2003: 12 December 2010 - 11:38 AM


Is This A Good Question/Topic? 0
  • +

Replies To: One dimensional arrays and classes

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,176
  • Joined: 02-July 08

Re: One dimensional arrays and classes

Posted 12 December 2010 - 11:49 AM

You don't have an array, just a property that holds 1 value. You did not set the value of quantity so it is 0. You made a Sub New where you could pass the value to it in the constructor. I think it would look more like this:

Public Class mySum
  Public intList As New List(Of Integer)
  Public Function SumAllNumbers() As Integer
    Return (From i As Integer In intList Select i).Sum
  End Function
End Class

'usage
Dim ms As New mySum
ms.intList.AddRange(New Integer() {1, 2, 3, 4, 5})
MessageBox.Show(ms.SumAllNumbers.ToString)


Was This Post Helpful? -1
  • +
  • -

#3 kchenry2003  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 11
  • Joined: 10-October 09

Re: One dimensional arrays and classes

Posted 12 December 2010 - 12:24 PM

I need to take the integer values accumulated from a textbox and out put the totals to a label. I have been working on this for days and cannot find a clear example of how to do this very thing. I have looked through code snippets, books, tutorials, this forum and others. Can someone give me the secrete code for adding the values in a one dimensional arrays?

This is what I think it should be
dim myIntegers as integers
dim myRow as integers
dim mySum as integers
myIntegers = myText.box

myRow = myInteger
mySum = myRow + myInteger
lblTotal = mySum
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,298
  • Joined: 12-June 08

Re: One dimensional arrays and classes

Posted 12 December 2010 - 12:32 PM

Well arrays are static in length.. maybe an array list?

http://msdn.microsof....arraylist.aspx

But in your example of totaling up a text box you really don't need an array.. just an integer (or heck the label itself) would suffice.

textbox has a value
event fires (button click? textbox value change?) saying to tally that total.
verify textbox value is a number
integer for the sum += textbox value
label = integer for the sum (cast as string)

repeat.
Was This Post Helpful? -1
  • +
  • -

#5 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,176
  • Joined: 02-July 08

Re: One dimensional arrays and classes

Posted 12 December 2010 - 12:41 PM

I just did! a List(Of Integer) is a 1 dimensional array of Integers. In my example you would use the .Add method to add 1 at a time. Then just call that function I gave you to get the total in the array.

Why make 2 topics, just ask for an explanation if you don't understand it.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,298
  • Joined: 12-June 08

Re: One dimensional arrays and classes

Posted 12 December 2010 - 12:44 PM

Sorry Hawk.. I thought he was talking about an array proper..

I'll merge the topics.. I didn't see the other.

This post has been edited by modi123_1: 12 December 2010 - 12:47 PM

Was This Post Helpful? -1
  • +
  • -

#7 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,176
  • Joined: 02-July 08

Re: One dimensional arrays and classes

Posted 12 December 2010 - 01:00 PM

Since TryParse is a boolean function(use your textbox input where I have the "10":
Dim i As Integer
Dim ms As New mySum
ms.intList.AddRange(New Integer() {1, 2, 3, 4, 5})
If Integer.TryParse("10", i) Then
  ms.intList.Add(i)
End If
MessageBox.Show(ms.SumAllNumbers.ToString)


This may make more sense:

Public Class mySum
  Public intList As New List(Of Integer)
  'simpler example 
  Public Function SumAllNumbers() As Integer
   Dim int As Integer 
   For Each i As Integer in intList
     int += i
   Next
   Return int
  End Function
End Class


This post has been edited by hawkvalley1: 12 December 2010 - 01:07 PM

Was This Post Helpful? 0
  • +
  • -

#8 kchenry2003  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 11
  • Joined: 10-October 09

Re: One dimensional arrays and classes

Posted 12 December 2010 - 01:24 PM

View Posthawkvalley1, on 12 December 2010 - 12:00 PM, said:

Since TryParse is a boolean function(use your textbox input where I have the "10":
Dim i As Integer
Dim ms As New mySum
ms.intList.AddRange(New Integer() {1, 2, 3, 4, 5})
If Integer.TryParse("10", i) Then
  ms.intList.Add(i)
End If
MessageBox.Show(ms.SumAllNumbers.ToString)


This may make more sense:

Public Class mySum
  Public intList As New List(Of Integer)
  'simpler example 
  Public Function SumAllNumbers() As Integer
   Dim int As Integer 
   For Each i As Integer in intList
     int += i
   Next
   Return int
  End Function
End Class



How do code the values for newInteger() when they are from user input and will be different everytime?
Was This Post Helpful? 0
  • +
  • -

#9 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,176
  • Joined: 02-July 08

Re: One dimensional arrays and classes

Posted 12 December 2010 - 01:28 PM

You make a Sub where you do the Integer.TryParse and add the number when it passes the check. Most likely a button click event???

This post has been edited by hawkvalley1: 12 December 2010 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

#10 kchenry2003  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 11
  • Joined: 10-October 09

Re: One dimensional arrays and classes

Posted 12 December 2010 - 03:45 PM

View Postkchenry2003, on 12 December 2010 - 12:24 PM, said:

View Posthawkvalley1, on 12 December 2010 - 12:00 PM, said:

Since TryParse is a boolean function(use your textbox input where I have the "10":
Dim i As Integer
Dim ms As New mySum
ms.intList.AddRange(New Integer() {1, 2, 3, 4, 5})
If Integer.TryParse("10", i) Then
  ms.intList.Add(i)
End If
MessageBox.Show(ms.SumAllNumbers.ToString)


This may make more sense:

Public Class mySum
  Public intList As New List(Of Integer)
  'simpler example 
  Public Function SumAllNumbers() As Integer
   Dim int As Integer 
   For Each i As Integer in intList
     int += i
   Next
   Return int
  End Function
End Class



How do code the values for newInteger() when they are from user input and will be different everytime?

Was This Post Helpful? -2
  • +
  • -

Page 1 of 1