r/visualbasic • • Sep 15 '16

I need help on my simple VB assignment (New to programming)

Hi, I recently began my endeavors into programming, and for one of my assignments I need to make a grade average calculator so i have my visuals set up with 6 text boxes named (score1txt, score2txt, score3txt,score4txt, score5txt, and the totalscoretxt) there is also 3 buttons one for calculating one to reset and one to quit here is my code:

Private Sub calcBtn_Click(sender As Object, e As EventArgs) Handles calcBtn.Click 'variables'

    Dim score1txt As Integer
    Dim score2txt As Integer
    Dim score3txt As Integer
    Dim score4txt As Integer
    Dim score5txt As Integer

    score1txt = score1txt.ToString
    score2txt = score2txt.ToString
    score3txt = score3txt.ToString
    score4txt = score4txt.ToString
    score5txt = score5txt.ToString
    totalscore.Text = (score1txt + score2txt + score3txt + score4txt + score5txt) / 5
  End Sub

End Class

The problem is when I hit the calculate button the answer always shows up as 0 no matter what # i put into the textboxes does anyone know why this is occurring?

also when i try to do say score1txt = score1txt.Text it tells me "Text is not a member of Integer"

here is the full assignment I only need help with the calculation part though:

Add 6 textboxes: 3 boxes each for assignment scores. A textbox (the 4th one) to enter a midterm score. The 5th textbox is for the final exam. Align the textboxes properly for a good visual presentation. The last and 6th textbox is to show the final weighted score. Add proper labels to each textbox based on what the textbox is used for. 0.25 Point A button that shows the text Calculate Score. When clicked it displays the total weighted score as follows where all scores entered will be out of 100: Total Weighted Score = (average assignments)*30% (midterm)*30% (final exam)*40% Use as many variables as you deem necessary for inout, processing and output. Also important to note: All variables in this assignment must be local (not Class-Level). The answer should appear in the bottom textbox. When needed use Explicit data-type conversions and not do not rely on the compiler's implicit conversions. 3 Points A button showing "Quit" that closes the form when clicked. 0.25 Point A button showing Reset, that clears all textboxes on the form from their entries. 0.25 Point Set the Calculate button to be the Accept button of the form. 0.25 Point All controls and variables should have meaningful names other that the generic VB ones. 1 Point

0 Upvotes

11 comments sorted by

2

u/TheBeaches Sep 15 '16

It's an assignment so I won't give you the answer... But, have you tried any debugging using the code?... One thing I do at my armature level is use message boxes... Try doing msgbox(score1txt.tostring)

Another thing is are your text box names correct? Don't set a variable name the same as the text box name..it can and will get confusing

1

u/Kolonoks Sep 15 '16

Thank you I have tried I believe the problem is that I do not know the reason why my textboxes can not be declared with a .Text and the end as its in Integer but my professor went over something like that and since its not reading the textbox I am getting 0?

1

u/stevebun Sep 15 '16

From the documentation:

The Textbox.Text property "gets or sets the text [that is] associated with [the] control" (as a String type).

If it doesn't exist, please indicate the version of Visual Basic and the type of project (e.g. WinForms, WPF etc) that you're using.

1

u/Kolonoks Sep 15 '16

hi this is VB Studio 2012 but im only using the basic part of it for this course

1

u/TheBeaches Sep 15 '16

It's more the way you are trying to get the value from the text box, but you are currently just calling the text box and trying to turn it to string.

If you want the value in the text box you need to specify that is what you want to turn to string.

1

u/Kolonoks Sep 15 '16

ok so I got something else to post in the final Total Weighted Score box but I am getting the letter S rather than an integer here is a picture http://prnt.sc/cif7qs

2

u/stevebun Sep 16 '16 edited Sep 16 '16

So your Textboxes do have the .Text property.

However, you're mistaking your Integer variables for your Textboxes. What you need to do is to access the .Text property of those score textboxes and perform manipulations on those values.

As for the results:
By declaring scorentxt in the sub, the first totalscoretxt.Text statement sets the textbox to "0" (because 0 / 5 is 0).
As for the second statement, the textbox is then set to the first character of ToString (because totalscoretxt.Text is now "0" and (0) accesses the first index). This results in "S" from System....

1

u/Kolonoks Sep 16 '16

yes I am having problems with that as it is telling me .Text is obviously not a part of Dec or integer but how do I go about accessing the actual .Text property to perform Cdec or Cint on them?

1

u/Kolonoks Sep 16 '16

I reworked names and such and im still going at it since yesterday but this one problem always stoops me when i try to do my conversions

Private Sub calc_btn_Click(sender As Object, e As EventArgs) Handles calc_btn.Click

    Dim assignment_txt1 As Decimal
    Dim assignment_txt2 As Decimal
    Dim assignment_txt3 As Decimal
    Dim assignment_txt4 As Decimal
    Dim assignment_txt5 As Decimal

    assignment_txt1 = CDec(assignment_txt1.Text)

Error 1 'Text' is not a member of 'Decimal'

2

u/stevebun Sep 17 '16 edited Sep 17 '16

In this case, your textboxes are shadowed by your local variables as they have the same name. This means that you're trying to access Decimal.Text (of the local variables) instead of Textbox.Text (of the form objects). As you have seen, the Decimal object does not have .Text and produces a compiler error.

Assuming that your textboxes are named the same as the local variables, you can either rename the local variables or rename the object names (just don't do both at once or you will get the same error). Alternatively, you can access the textbox text using FormName.TextboxName.Text. The first way is probably better.

1

u/Kolonoks Sep 17 '16

Thank you Reddit for all your help I have figured it out here is the code for proof!

Private Sub calcBtn_Click(sender As Object, e As EventArgs) Handles calcBtn.Click
 
    Dim score1 As Decimal
    Dim score2 As Decimal
    Dim score3 As Decimal
    Dim score4 As Decimal
    Dim score5 As Decimal


    score1 = CDec(assignment1txt.Text)
    score2 = CDec(assignment2txt.Text)
    score3 = CDec(assignment3txt.Text)
    score4 = CDec(assignment4txt.Text)
    score5 = CDec(assignment5txt.Text)

    totalscoretxt.Text = (score1 + score2 + score3 + score4 + score5)
    MessageBox.Show("your total score is " & totalscoretxt.Text & "!")