Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

help with visual basic project


  • Please log in to reply

#1
ebecky

ebecky

    New Member

  • Member
  • Pip
  • 1 posts
Hello! I am taking a Visual Basic 2010 class. I guess the light bulb has not turned on in my head yet. Here is my second assignment. Assignment 2

Right Triangle
In a right triangle, the square of the length of the longest side is equal to the sum of the squares of the
lengths of the other two sides.
Write and test a Visual Basic program to
complete the following project,
determining whether a given triangle is “right” or not based on the lengths of its sides.


Code
o
Declare variables to hold the lengths of the three sides of a triangle. All should be
doubles.
o
Create an event handler for the click event in the first button. All the following activities
must take place within this event handler.
o
Reset the last bu
tton to its default appearance and text.
o
Input three values representing the lengths of three sides of a triangle in inches from
the three textboxes. Validate each of these values. If any input value is less than 0.1
inches, clear the corresponding textbox
and set the text in the label to indicate that the
offending value is too small.
o
Identify the longest side as the hypotenuse. Do no assume or require the user to identify
the longest side. Do this in code.
o
Calculate the expected length of the hypotenuse b
ased on the lengths of the 2 shorter
sides.
o
Compare the expected length of the hypotenuse to the length input by the user.
o
If the two lengths are the same, declare the triangle to be
“Right”, display the word
“Right” in the last button and set its
background color to green. Otherwise, declare the
triangle to be “Not Right”, display the words “Not Right” in the last button and set its
background color to red.
These are the instructions and I already have my buttons and labels in the right places. Here is the code that I have so far.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim A As Double
        Dim B As Double
        Dim C As Double

        A = TextBox1.Text
        B = TextBox.Text
        C = (A ^ 2 + B ^ 2)

        If Button2.Text = "Right" Then
            Button2.BackColor = Color.Green
        Else
            Button2.Text = "Not Right"
            Button2.BackColor = Color.Red
        End If

I have some errors that say: Reference to a non-shared member requires an object reference. And 'TextBox1' is not declared. It may be inaccessible due to its protection level.

I do not know how to fix these and I really don't understand what I am doing. I am just beginning, and we haven't learned about classes. Could someone look at this and give me some advice. Thanks!
  • 0

Advertisements


#2
AceInfinity

AceInfinity

    Visiting Staff

  • Visiting Consultant
  • 34 posts
  • MVP
You have implicit type casting there. How can you assign a string value (Textbox#.Text) to a variable of type Double floating point? I will explain...

Does this make sense to you?
Dim var As Double = "30.0"

Or perhaps this:
Dim var As Double = "string"

There's little difference between those examples. Those are both strings, not numeric value types. This is a numeric value type of type Double, being assigned to a variable that expects a type of Double:
Dim var As Double = 10.0

As for the reference error, it is as it says, it's not declared. You do not have a TextBox on your form called TextBox1; it is non-existant.

Below that, you have the TextBox class and you're trying to grab the Text property from it instead of an instance of the TextBox class:
B = TextBox.Text

And after the sum of the squares for the Pythagorean calculations, how do you expect Button2.Text to be "Right" or "Not Right" when you aren't setting it to a value? Also, what that code does, is if it is not "Right", then it changes the Text of the button to "Not Right". You aren't checking for "Not Right". And if it is already "Not Right", this would only re-assign the Text to the same value.

As for the main branch of this logic, you don't have a boolean anywhere in here, so you can't possibly expect to return a value as to whether the triangle is a right triangle or not.

Edited by AceInfinity, 20 March 2013 - 08:24 PM.

  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP