VB.Net Noob needs some direction

Trying to accomplish this equation 100-150= negative 50. Instead I am getting a positive 50.

My head hurts. :(

Count = 2

First time through result = 0, myarray(0) = 100. So 100 minus 0 =100. Result =100
Second time through result =100, myarray(1) = 150. So 150 minus 100 = 50.

--------------------------------------------------------------

For count As Integer = 0 To myarray.Length - 2

result = myarray(count) - result

Next
 
Last edited:
I think I understand what you're trying to accomplish...

Take a closer look at this structure against your logic in the "Second time through" section:
Code:
arrayVariable(index) = value
 
Don't edit your original post after the fact. The benefit of the forum is that everyone is able to read the contributions of others. If you edit your posts you are depriving the contributors the benefits you derived from it, essentially taking out without contributing. Don't do that.
 
Don't edit your original post after the fact. The benefit of the forum is that everyone is able to read the contributions of others. If you edit your posts you are depriving the contributors the benefits you derived from it, essentially taking out without contributing. Don't do that.
This.
Editing previous posts also breaks the flow of the interactions between posters, such as rendering my post obscure to a new visitor to the thread.


Still a bit cloudy.
You're getting closer. I bet if you work out the flow on a whiteboard for the 3 items in the array against the "result" variable, you'll see where things are slightly jumbled.
 
I only see 2 items in the array. 100 and 150.

If it makes a difference, once I get this working, count will be a value entered by the user.

-Funk
 
How many items are being loaded into your array? How does that get evaluated/processed when you look at the bounds in your "For" clause?
 
I am loading two items (100,150) into the array at positions 0 and 1.

For count As Integer = 0 To 2 - 2?

-Funk
 
Here is the full code I am working with if it matters.

count = InputBox("Enter the amount of numbers you want to add", , "Enter")

ReDim myarray(count)
'Dim x As Integer


For count As Integer = 0 To myarray.Length - 2
myarray(count) = InputBox("Please enter a value for position " & count, "myArray")



Next

Response.Write(myarray.Length & "<br />")
Response.Write(myarray(0) & "<br />")
Response.Write(myarray(1) & "<br />")


For count As Integer = 0 To myarray.Length - 2

result = myarray(count) - result


Next
Response.Write("The sum of the values is " & result)
End Sub
 
Because when you call ReDim myarray(count), and count=2, you're telling it to allocate 3 elements.
 
There was nothing in the first 2 posts. The problem in full starts at post 3.

-Funk
 
Try this:

Code:
count = InputBox("Enter the amount of numbers you want to add", , "Enter")

ReDim myarray(count-1)
'Dim x As Integer


For count As Integer = 0 To myarray.Length - 1
myarray(count) = InputBox("Please enter a value for position " & count, "myArray")



Next

Response.Write(myarray.Length & "<br />")
Response.Write(myarray(0) & "<br />")
Response.Write(myarray(1) & "<br />")


For count As Integer = myarray.Length - 1 To 0 To Step  -1
result = myarray(count) - result


Next
Response.Write("The sum of the values is " & result)
End Sub
 
Last edited:
Why'd you give away the answer when the OP was so close?
 
Honestly he wasn't all that close but I didn't see the first posts. For something this trivial I don't think I revealed anything other than showing how to count the array properly and stepping backwards through it.

Not trying to sound superior, I've probably been doing this a LOT longer than you. Been writing code in BASIC since I was 10 and I'm 42 now. Just an old geek!;)
 
Last edited:
For something this trivial I don't think I revealed anything other than showing how to count the array properly and stepping backwards through it.
The main point was for the OP to actually read the suggestions posted, and take one more step on his/her own down the direction being pointed.

And on that same note...
Honestly he wasn't all that close but I didn't see the first posts.
 
Back
Top