Wrox Programmer Forums
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 5th, 2003, 11:23 AM
Authorized User
 
Join Date: Sep 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default TextLength? Numeric

What am I doing wrong?...I want to check if the user enters a number greater than 7...if so....messagebox "Please enter a smaller number"


Private Function DoubleNumber(ByVal dblnumber As Single) As Single
        Return (dblnumber * 2)
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                              Handles Button1.Click

        If TextBox1.TextLength > 7 Then
            lblResult.Text = "Your number doubled is " & _
            DoubleNumber(CSng(TextBox1.Text))
        Else
            MsgBox("Please use a smaller number")
        End If
    End Sub

Still learning...Thanks in advance!!! Kevin

 
Old October 5th, 2003, 12:59 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Kevin,

You are not actually checking whether the number is greater than 7, but if its length is greater than 7. So, a number like 84632854 would match your criteria.

Instead, you'll need to convert the text from the text box to a number, and then check if the results of the conversion are greater than 7:
Code:
If IsNumeric(TextBox1.Text) Then
  If CSng(TextBox1.Text) > 7 Then
    lblResult.Text = "Your number doubled is " & _
      DoubleNumber(CSng(TextBox1.Text))
  Else
    MsgBox("Please use a smaller number")
  End If
Else
  MessageBox.Show("This is not a valid number")
End If

You'll also need to change > 7 to < 7 because, if I understand the situation correctly, you want your users to type in a number less than or equal to 7.

One more thing: MsgBox is "classic Visual Basic" (just as the IsNumeric method I used in my example, BTW). You may decide to use MessageBox.Show instead. It does the same, but it's easier to port your code to C# or another .NET language in the future, should that ever be necessary.

HtH,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old October 5th, 2003, 10:10 PM
Authorized User
 
Join Date: Sep 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank You Imar!...I tried IsNumeric but not the way you placed it in the code...still learning a lot!

Can this be written with less code?

Thanks...Kevin

 
Old October 6th, 2003, 03:38 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Well, less code is not always better. I am quite a "verbose coder" as I believe that is usually easier to understand for others maintaining my code.

In your case, there is not much to shorten. You'll need to check the type of the value entered, before you can check if its value is greater than 7.

You may want to look into Validation, which is build-in in Visual Basic. This will allow you to validate multiple controls with different kinds of values. However, it may not be the first beginner's topic you should get into.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old October 7th, 2003, 11:21 PM
Authorized User
 
Join Date: Sep 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar....I am having trouble really understanding how to iterate through loops...or use them in a practical application...I guess I'm not seeing the "Big" picture.

 ' start looping...
        Dim n As Integer = 0
        Do While n < 10

            ' add n to the list...
            lstData.Items.Add(n)
            ' add one to n...
            n = n + 1

            ' do we need to quit?
            If n = 3 Then
                Exit Do
            End If
        Loop

Any advice or insight(in plain english please)sorry...having trouble grasping this...maybe a practical example where I would need to iterate through a loop? I have looked for some links on this subject...but came up empty. thanks Imar...

sincerely

 
Old October 8th, 2003, 04:02 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Loops are useful for all kinds of purposes. For example, you can loop through a collection object (a bunch of objects tied together for easy access), retrieving a Name or some other property for each item in the collection.

Loops are also useful for repetitive tasks: If you need to add 10 labels to a form, you can add the label 10 times by coding it 10 times. But with a loop, you create the code for one Label, and then repeat that loop 10 times. Saves you from a lot of typing.

You can loop through a recordset, outputting values you retrieved from a database, loop through a string outputting each individual character, loop through an array and displaying each item inside the array.

The possibilities (and needs) for a loop are endless.

The problem you may run into while learning, is that most examples explaining loop concepts don't have much real world value, so it's hard to imagine what they can do and are used for.

The code you posted shows another good use of a loop: Adding multiple items to List Box (I assume lstData is a Listbox). This way, you can easily add 10 items with a few lines of code, without adding all these items yourself.

The Exit Do may not be a good idea. If you only want to add three items, make the loop loop three times instead of 10. Using Exit Do usually makes your code harder to read as it jumps from one point (the loop) to another point (at the end of the loop. Sometimes, however, Exit Do is the right way to exit a loop, but usually I try to code around it.


Cheers,

Imar



---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old October 8th, 2003, 08:16 AM
Authorized User
 
Join Date: Sep 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you again Imar! Looping has been one of my weak points...but I'm learning!

Kevin :)

 
Old October 8th, 2003, 08:33 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Kevin,

For i As Integer = 1 To 10
    MessageBox.Show("You're welcome " & i.ToString())
Next

:)

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old October 8th, 2003, 08:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jlick
Default

Kevin,

You are currently working on learning the language. This is am important thing, but one of the things that I have found with many books that "teach" programming, is that they focus only on the syntax of the programming language, and not enough on conceptual things. This is part of your frustration.

Think of it this way. Let's say you are writing a program that will balance your checkbook. In the program the user can enter checks, but the programmer doesn't know at the time of writing code how many checks there are, or what their values are. You can loop through all the checks and all the deposits to see what the current balance is. Or you can loop through all the checks to find out how many were written to the power company, or even how much the total value of all the checks written to the power company is. This is powerful, because you just CAN NOT write code that will do this without loops.

Hope this helps.



John R Lick
[email protected]





Similar Threads
Thread Thread Starter Forum Replies Last Post
From numeric to alfa numeric ebekir XSLT 1 August 10th, 2007 06:13 AM
How can i know it's NUMERIC deco Beginning VB 6 1 April 23rd, 2007 06:19 AM
HELP!! Numeric Textbox value quentinuys HTML Code Clinic 3 December 18th, 2006 08:04 AM
Determine if numeric Scootterp Access VBA 4 March 2nd, 2006 08:44 AM
Numeric Up Down Kunkel General .NET 0 April 16th, 2004 10:07 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.