|
-
Mar 7th, 2010, 02:13 PM
#1
Thread Starter
New Member
[RESOLVED] Quick Question from a n00b
I am just learning and I found that I should be Declaring my variables as strings or integers from the start. I learned that I can use an * number to determin length.
Example: Dim yourName as String * 10
But if I try and do this to an integer it doesn't like it (Dim yourAge as Integer * 2)
So, does this mean I have to declare it as a string then reference it later using the val() or is there a different way to define allowed integer length?
-
Mar 7th, 2010, 03:01 PM
#2
Re: Quick Question from a n00b
 Originally Posted by SakuyasLove
Example: Dim yourName as String * 10
You can only define fixed length for string type variables. That should answer your question about attempting to create fixed length integers.
-
Mar 7th, 2010, 03:44 PM
#3
Re: Quick Question from a n00b
Numeric data types like Integer do not have a length, what they have is a minimum and maximum value based on the data type.
For example, Integer can store whole numbers from -32,768 to 32,767
Long (long integer) can store whole numbers from -2,147,483,648 to 2,147,483,647
There are various options for storing numbers with decimal places, such as Single, Double, and Currency.
You can find info about all of this in VB's help, particularly if you search (by title) for "Data Type Summary".
-
Mar 7th, 2010, 10:10 PM
#4
Thread Starter
New Member
Re: Quick Question from a n00b
So my thought was correct, if I want to limit the user on how many numbers to put in I need to collect them as a string, that has a length of 2 and then call it as a val() later.
-
Mar 8th, 2010, 12:16 AM
#5
Re: Quick Question from a n00b
There are many ways to limit what a user inputs. You can write conditions using If - Then blocks if you wish. For TextBoxes you can set its MaxLength property. The TextBox won't accept chrs exceeding MaxLength.
Code:
Text1.MaxLength = 8 ' TextBox limit is 8 chars, including spaces
I could be wrong but I think you're not quite grasping what Dim means. It will not restrict what a user tries to input. Instead, you will throw an error if a data is entered that does not agree with its declaration type.
<--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
If topic has been resolved, please pull down the Thread Tools & mark it Resolved.
Is VB consuming your life, and is that a bad thing?? 
-
Mar 8th, 2010, 05:41 AM
#6
Re: Quick Question from a n00b
Here's another example: ...
Use a Textbox and a Commandbutton for testing....
Code:
Private Sub Command1_Click() '~~~> On clicking the commandbutton named 'Command1'
If Val(Text1.Text) <= 0 Or Val(Text1.Text) >= 10 Then '~~~> Check the user's entry in textbox named 'Text1'
MsgBox "No...! You have to enter number within the range of 1 to 9" '~~~> If it is not in the required range, display a message
Exit Sub '~~~> Prevent the rest of the codes from being executed
End If
'~~~ If the checking is a success, then the rest of the codes will be executed
MsgBox "Good..! You have entered a value in the range of 1 to 9"
End Sub
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Mar 8th, 2010, 10:28 AM
#7
Thread Starter
New Member
Re: Quick Question from a n00b
Thanks, that makes more since now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|