Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works

# Visual Basic Help

### #1 Crmathe4654 Posted 04 November 2011 - 08:42 AM

Crmathe4654

New Member

• Member
• 1 posts
Just a quick question.

I am in a visual basic class online and shot myself in the foot early on in the semester. I joined a group that had someone that was very well versed in computer programming and she offered to take the bulk of our projects leaving myself and another girl with minimal work. Well that worked fine until my luck decided to shine through and she dropped the class. Now I am at a loss when it comes to the coding language and I am playing catch up big time. The book they provide is pretty useless and I was wondering where I could go for better help with the coding for Visual Basic 2010 express. I can create the design and layout of my program and even some basic button functions but as far as the rest is concerned I'm at a loss. The equation I am struggling with is as follows: H+(V*T)-(16.087*T*T) Variable H= an input into a text box labeled TbxInitialHeight, Variable V= an input into a text box labeled TbxInitalVelocity and variable T = V/32.174. This all needs to happen when button BtnMaxHeight is pressed. I thought I had it figured out but when I ran the program it kept telling me that the data in the text boxes couldn't be either converted or ran as a single.
Also for another button function, with these same variables and equation, I need it to keep repeating the equation given while increasing T each time by .001 until the answer of that equation is equal to zero. How do write the code to keep repeating until I get my desired result.

Any and all help that can be provided will be greatly appreciated!
• 0

### #2 tom982 Posted 24 January 2012 - 04:16 AM

tom982

Member 1K

• Member
• 1,183 posts
Hello Crmathe4654 and welcome to G2G.

I know it's been a very long time, but I might as well ask, do you still need help?

I don't know much VB.NET, but I think that's well within my capability - well the first part anyway. The VB.NET that I do know, was learnt from this website and I would highly recommend it! I'm currently teaching myself C# on there.

Here's what I've written for the first part:

```Public Class Form1
Private Sub btnMaxHeight_Click(sender As System.Object, e As System.EventArgs) Handles btnMaxHeight.Click
Dim varH As Double = tbInitialHeight.Text
Dim varV As Double = tbInitialVelocity.Text
Dim varT As Double = varV / 32.174
Dim maxHeight As Double
maxHeight = varH + (varV * varT) - (16.087 * varT * varT)
MessageBox.Show("The max height reached is: " + maxHeight.ToString())
End Sub
End Class```

Here's how far I've got with the second part:

```Public Class Form1
Dim varH As Double
Dim varV As Double
Dim varT As Double
Dim maxHeight As Double
Private Sub btnMaxHeight_Click(sender As System.Object, e As System.EventArgs) Handles btnMaxHeight.Click
Dim varH As Double = tbInitialHeight.Text
Dim varV As Double = tbInitialVelocity.Text
Dim varT As Double = varV / 32.174
Dim maxHeight As Double
maxHeight = varH + (varV * varT) - (16.087 * varT * varT)
MessageBox.Show("The max height reached is: " + maxHeight.ToString())
End Sub

Private Sub btnIncrease_Click(sender As System.Object, e As System.EventArgs) Handles btnIncrease.Click
Dim varH As Double = tbInitialHeight.Text
Dim varV As Double = tbInitialVelocity.Text
Dim varT As Double = varV / 32.174
Dim maxHeight As Double
maxHeight = varH + (varV * varT) - (16.087 * varT * varT)
Do Until maxHeight = 0
varT = varT + 0.01
maxHeight = varH + (varV * varT) - (16.087 * varT * varT)
Loop
MessageBox.Show("Variable T = " + varT.ToString())
End Sub
End Class```

Ignore the messy code I don't know what values to plug into it that would generate a value of T that makes maxHeight=0, so that code is currently eating 50% of my CPU trying to calculate a value. I'll leave it half an hour or so before having another go at the code, it might find one.

This has next to no error-handling by the way, so don't execute the code with empty textboxes or it'll crash. I don't know if you wanted it to work with negative values or not, so I've let it.

Tom

Edited by tom982, 24 January 2012 - 04:45 AM.

• 0

### #3 AceInfinity Posted 27 January 2012 - 03:08 AM

AceInfinity

Visiting Staff

• Visiting Consultant
• 34 posts
@tom982 - In your code you're not showing best habits. Firstly, you should explicitly define your types, luckily VB does that for you, but a textbox string is not a type double value.

Why are you defining those variables outside of a subroutine though if you never have need for them?

I would also recommend using a While loop in this case over the Do Until loop. Much easier.

`varT = varT + 0.01`

That can be shortened to this:
`varT += 0.01`

Something like this is more optimized code...
```Option Strict On

Public Class Form1

Private varH As Double
Private varV As Double
Private varT As Double
Private maxHeight As Double

Private Sub btnMaxHeight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaxHeight.Click
If tbInitialHeight.Text = String.Empty Or tbInitialVelocity.Text = String.Empty Then
MessageBox.Show("Please make sure all inputs have values", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
Else
Try
varH = CType(tbInitialHeight.Text, Double)
varV = CType(tbInitialVelocity.Text, Double)
varT = varV / 32.174
Catch ex As InvalidCastException
MessageBox.Show("There was an unknown error with your input values, please make sure they are valid", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
End If

maxHeight = varH + (varV * varT) - (16.087 * varT ^ 2)
MessageBox.Show("The max height reached is: " + maxHeight.ToString(), "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End Sub

Private Sub btnIncrease_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIncrease.Click
If tbInitialHeight.Text = String.Empty Or tbInitialVelocity.Text = String.Empty Then
MessageBox.Show("Please make sure all inputs have values", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
Else
Try
varH = CType(tbInitialHeight.Text, Double)
varV = CType(tbInitialVelocity.Text, Double)
varT = varV / 32.174
Catch ex As InvalidCastException
MessageBox.Show("There was an unknown error with your input values, please make sure they are valid", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
End If

maxHeight = varH + (varV * varT) - (16.087 * varT ^ 2)
While maxHeight < 0
varT += 0.01
maxHeight = varH + (varV * varT) - (16.087 * varT ^ 2)
End While
MessageBox.Show("Variable T = " + varT.ToString(), "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End Sub

End Class```

Edited by AceInfinity, 27 January 2012 - 03:39 AM.

• 0

### #4 tom982 Posted 27 January 2012 - 06:14 PM

tom982

Member 1K

• Member
• 1,183 posts
As you've probably realised, I'm not all that good with VB. I just wanted to give it a shot as it was an unanswered thread, even my code is better than nothing!

Thanks for the tips I've dropped VB and I'm learning C# now, but I'm sure they'll still be useful.

Tom
• 0

### #5 AceInfinity Posted 27 January 2012 - 06:25 PM

AceInfinity

Visiting Staff

• Visiting Consultant
• 34 posts

As you've probably realised, I'm not all that good with VB. I just wanted to give it a shot as it was an unanswered thread, even my code is better than nothing!

Thanks for the tips I've dropped VB and I'm learning C# now, but I'm sure they'll still be useful.

Tom

C# will give you a good stepping stone for C++ in the future when you venture off into such a language, or something such as C or Java even.

I would agree though, and it was generous of you to take time to post to such an old thread just to help the person out as well!

Cheers!
~Ace
• 0

### #6 tom982 Posted 28 January 2012 - 10:46 AM

tom982

Member 1K

• Member
• 1,183 posts

As you've probably realised, I'm not all that good with VB. I just wanted to give it a shot as it was an unanswered thread, even my code is better than nothing!

Thanks for the tips I've dropped VB and I'm learning C# now, but I'm sure they'll still be useful.

Tom

C# will give you a good stepping stone for C++ in the future when you venture off into such a language, or something such as C or Java even.

I would agree though, and it was generous of you to take time to post to such an old thread just to help the person out as well!

Cheers!
~Ace

Yeah, I was told that it's a good one to get started with. Especially because it's part of .NET as well. Hopefully I'll get further with C# than I did with VB - it shouldn't be too hard

Thanks again for the tips, I really appreciate you taking the time to point me in the right direction, and I'm sure that the OP will appreciate it even more than I do!

Tom
• 0

### Similar Topics

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users