Question Digit Locator

TommyEvans

Active member
Joined
Feb 19, 2010
Messages
39
Programming Experience
Beginner
I'm currently in a VB class in school. I am required to make a "digit locater", but am unsure on how to do this. Here is a screenshot of how it should look. Could anyone tell me how to do this (Not do it for me, I'd actually like to learn.)

33aujrs.jpg
 
What does the Compute button do?

Think about how you would do it personally: If the Units Digit is pressed, print the third character in the textbox. Look up the String.SubString function.
 
To be honest, I'm not exactly sure what it does. I wasn't going to include it. :p

I mean, like an example. I can't learn from reading. I learn from examples, then doing it myself. I'm not asking for someone to do it for me. I want to learn, and earn the grade myself.
 
I'm not sure how I would give you an example without doing it for you.
So I'll ask these questions; Do you know how to:
1) obtain the text in the textbox via code?
2) get the first, second, or third character in the string obtained from the textbox? (String.Substring method)
3) assign the value from above to be display by a label?
 
Did you look-up the String.Substring method?

I'm going to have to give up on this one. You say you can't learn from reading, you want an example, but you don't want someone to do it for you. Good luck!
 
VB.NET:
    Private Sub btnHundreds_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHundreds.Click
        Dim r As String = txtNumber.Text
        r = r.Substring(1)
        Me.lblOutcome.Text = r
    End Sub

    Private Sub btnTens_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTens.Click
        Dim r As String = txtNumber.Text
        r = r.Substring(2)
        Me.lblOutcome.Text = r
    End Sub

    Private Sub btnUnits_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnits.Click
        Dim r As String = txtNumber.Text
        r = r.Substring(3)
        Me.lblOutcome.Text = r
    End Sub

That's what I used. Now, I found a problem. My number I used, when I tested was: 697

When I press the hundreds button, it outputted 97. When I pressed the tens, it outputted 7. When I pressed the units, it outputted nothing.

I done something majorly wrong. :p
 
I just read over that. It only confused the heck out of me. You can help me without being afraid of "doing it for me" now. :p I got the most of it, and am still not done yet.
 
You did get pretty close. The second line in the example on the previously linked article on MSDN should have solved your problem.

The majority of .NET collections are zero based. Zero based means the first index in the collection is 0. I say the majority are zero based because there are some collections that are 1 based; they are legacy classes in the VisualBasic namespace.

There is an overload of the Substring method that takes two parameters of type Integer. The first is the StartIndex (The zero-based starting character position of a substring in this instance), the second is the length (The number of characters in the substring).

So for the Tens Unit button, the code would be:
VB.NET:
lblOutcome.Text = txtNumber.Text.Substring(1, 1)

Since this is an assignment, it may not matter but, you should have validation and error handling to take care of instances where the user enters something that is non-numeric or not three digits.
 
I am not sure how to do the validating thing. :p I've only been studying it for about 2 weeks now, and am taking my time on it. I want to learn it very thoroughly. I'm a Senior right now in High School, and am taking this via an online course.

EDIT: How do I "failsafe" it? When you don't enter anything and press submit, it still tries to calculate. How do I stop this, so it does not give an error?
 
Last edited:
Back
Top