Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am trying to create a one-dimensional array without size from text entered into a textbox. I then use the redim statement to resize the array.

 

I have tried two different approaches without success. The first was setting the textbox up as the array:

 

Dim txtScore() As TextBox

Dim iScoreCount As Integer

 

For ReDim Preserve txtScore(iScoreCount)

iScoreCount += 1

Next

 

The second was setting up the array and trying to get data from the textbox to the array:

 

Dim dScore As Decimal

Dim iScoreCount As Integer

 

For dScore(iScoreCount) = txtScore.Text 'this is a syntax error

iScoreCount += 1

Next

 

I was able to get the first entry into the textbox into the array, but could not get anything further into it. I used the following to check:

 

lblScoreCount.Text = iScoreCount

 

I would get a "1" in the label, but when I tried to enter another number into the textbox, nothing would happen.

 

Any ideas on how to get the loop to work, or am I making this much harder than it has to be?

  • *Experts*
Posted
I'm not sure if I am understanding your problem correctly. What excatly are you trying to do? Splitting the text into characters and putting them into an array, or what? I'm not making much sense out of your problem.
Posted
I'm not sure if I am understanding your problem correctly. What excatly are you trying to do? Splitting the text into characters and putting them into an array' date=' or what? I'm not making much sense out of your problem.[/quote']

 

 

Sorry, I am very new to programing and am probably in over my head.

 

The assignment is to create a one-dimensional array. The data comes from a textbox. The user enteres scores(numbers) and clicks an "Add" button. the buttonclick event adds the number to the array. The ScoreCount (number of scores in the array) are displayed on the form as well as the score total(sum) and the average of the scores.

 

I am having trouble getting the data from the textbox to the array. I can get one number into it, but nothing further.

 

Does that help?

Posted

Not sure why you're trying to use an array of TextBoxes. Here's how I would do it (one TextBox on a form):


   Private _data As New ArrayList

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       Dim iLp As Integer
       Dim total As Integer
       Dim average As Single

       _data.Add(TextBox1.Text)

       For iLp = 0 To _data.Count - 1
           total += CType(_data(iLp), Integer)
       Next iLp

       Label1.Text = total.ToString
       Label2.Text = (total / _data.Count).ToString

   End Sub

 

Or, even easier:


   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       Static count As Integer
       Static total As Integer

       count += 1

       total += CType(TextBox1.Text, Integer)

       Label1.Text = total.ToString
       Label2.Text = (total / count).ToString

   End Sub

If you're using .NET, there's no reason to use regular arrays that you have to manually resize. Let .NET handle it for you. :D :cool:

Here's what I'm up to.
Posted

The assignment states to us a one-dimensional array to store the scores. The array needs to be declared without an initial size and we have to use the redim statement to resize each time a score is added.

 

Due to this we are limited in the options we have.

Posted

Tell your teacher than using redim isn't really that good and that you should be using arraylists.

 

'globals
dim scores as arraylist = new arraylist

public sub Addscore(byval score as integer)
scores.add(score)
end sub

  • *Experts*
Posted

Since you must use, as previously pointed out, a bad programming practice, here is an example for you.

Dim scores(-1) As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       ReDim Preserve scores(scores.Length)
       scores(scores.Length - 1) = Integer.Parse(sc.Text)
End Sub

Please plead with your instructor to teach to use the tools provided with .NET properly! :)

Posted

Here is what I have gotten so far...

 

Public Sub btnAdd_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnAdd.Click

 

Dim iScoreCount As Integer

Dim dScore() As Decimal = {FormatNumber(txtScore.Text)}

Dim dScoreTotal As Decimal

Dim dScoreText As Decimal

 

For iScoreCount = 0 To dScore.GetUpperBound(0)

ReDim Preserve dScore(iScoreCount)

iScoreCount += 1

Next iScoreCount

 

For iScoreCount = 0 To dScore.GetUpperBound(0)

dScoreTotal += dScore(iScoreCount)

Next iScoreCount

 

lblScoreCount.Text = iScoreCount

lblScoreTotal.Text = dScoreTotal

 

End Sub

 

So...now I get one variable into the array, but I cannot seem to figure out how to add another variable. When I enter a new number into the txtbox, it simply over-writes the old number and lblScoreTotal.Text continues to read 1 .

 

Can anyone tell me how to get this dumb program to continue adding variables to the array until I tell it to stop?

Posted

Try this:

 


   Private _data() As Integer

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       Dim iLp As Integer
       Dim total As Integer
       
       If _data Is Nothing Then
           ReDim _data(0)
       Else
           ReDim Preserve _data(_data.GetUpperBound(0) + 1)
       End If


       _data(_data.GetUpperBound(0)) = TextBox1.Text

       For iLp = 0 To _data.GetUpperBound(0)
           total += CType(_data(iLp), Integer)
       Next iLp

       Label1.Text = total.ToString
       Label2.Text = (total / _data.GetLength(0)).ToString

   End Sub

Here's what I'm up to.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...