Selecting Proper Variables

G

Guest

I'm new VB and programming all together, but I'm getting he hang of it.

My question is;

I'm writting a program to figure square feet and yards when the user inputs
"Length in feet and inch ( seperate boxes), Width in feet and inches
(seperate boxes), and thickness in feet and inches (all so in seperate
boxes). I haven't quit figured out the split function yet to use a single
text box for each. Anyways, when I enter data in each box; eg.

Length 2' 0"
Width 2' 0"
Thickness 0' 4"

my program works and figures out the correct answer. But when I enter it
differently, like this;

Lenght 2' not entering inches leaving box empty
Widht 2' not entering inches leaving box empty
Thickness not entering feet leaving box empty but entering 4"

it gives me an error "Cast from string " " to type 'Integer' is not valid."
Here is some of what Ive written.


Public LFeet, WFeet, TFeet As Integer
Public LInch, WInch, TInch As Integer
Public SqrFeet, Yard As Integer

Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCompute.Click

If Me.txtLInch.Text = " " Then TInch = 0
If Me.txtWInch.Text = " " Then WInch = 0
If Me.txtTFeet.Text = " " Then TInch = 0

Me.LFeet = Me.txtLFeet.Text
Me.LInch = Me.txtLInch.Text
Me.WFeet = Me.txtWFeet.Text
Me.WInch = Me.txtWInch.Text
Me.TFeet = Me.txtTFeet.Text
Me.TInch = Me.txtTInch.Text

If Me.WInch = 0 And Me.LInch = 0 Then Me.txtSqrFeet.Text = Me.LFeet
* Me.WFeet Else

Me.txtSqrFeet.Text = (Me.LFeet + (Me.LInch / 12)) * (Me.WFeet +
(Me.WInch / 12))

Me.SqrFeet = Me.txtSqrFeet.Text

If Me.TFeet = 0 Then Me.txtYard.Text = (Me.SqrFeet * (Me.TInch / 12)
/ 27) Else

Me.txtYard.Text = (Me.SqrFeet * (Me.TFeet + (Me.TInch / 12))) / 27

End Sub


I know that I'm using the wrong variables here, I can't quit figure it out
though.

Thanks to Anyone Who Replies.
 
C

Cor Ligthert

Dooglo,

Converting in Visual basic is very easy.

A text in the textbox text property is a String, however to use it as an
Integer you can (after testing that it is completly numeric) do
myvalue as integer = CInt(mytextbox.text)

When it is not completly numeric where - and plus are signs, than it will
throw an error.

Doing this back from text to int you can do

mytextbox.text = Cstr(myvalue) however mostly is used here
mytextbox.text = myvalue.ToString

I hope this helps so far?

Cor
 
S

Scott M.

The problem is that you aren't checking the textboxes for no data, you are
checking for a space character, rather than and empty string:

If Me.txtLInch.Text = " " Then TInch = 0
....should be...
If Me.txtLInch.Text = "" Then TInch = 0

So, when no data is entereed, it is not getting set to zero and when you try
to take the textbox value and set it into the integer varaible, you are
tring to take an empty string and make it become an integer which is what
the error message is reporting.

Second, I think it is better to keep the feet and inches as separate
textboxes, rather that try to make them one.

Third, I don't think you'll need your variable declared as Public unless you
intend to use them in another class so Private will do.

Next, you can combine the check for no data and the assignment of a value to
one If statement:

'Check for empty textboxes
If txtLInch.Text = "" Then
TInch = 0
Else
TInch = CType(txtTInch.Text, Integer)
End If

Next, the use of the keyword "Me" is certainly ok, but not required and can
make your statements cleaner to write. Instead of:

If Me.WInch = 0 And Me.LInch = 0 Then
Me.txtSqrFeet.Text = Me.LFeet * Me.WFeet
Else
Me.txtSqrFeet.Text = (Me.LFeet + (Me.LInch / 12)) * (Me.WFeet +
(Me.WInch / 12))
Me.SqrFeet = Me.txtSqrFeet.Text
End If

You can write:

If WInch = 0 And LInch = 0 Then
txtSqrFeet.Text = LFeet * WFeet
Else
txtSqrFeet.Text = (LFeet + (LInch / 12)) * (WFeet + (WInch / 12))
SqrFeet = txtSqrFeet.Text
End If


Good Luck!
 
W

Wayne Wengert

It looks like your test for empty values is looking for a single space
rather than an empty string? Try changing:

If Me.txtLInch.Text = " " Then TInch = 0
If Me.txtWInch.Text = " " Then WInch = 0
If Me.txtTFeet.Text = " " Then TInch = 0


To

If Me.txtLInch.Text = "" Then TInch = 0
If Me.txtWInch.Text = "" Then WInch = 0
If Me.txtTFeet.Text = "" Then TInch = 0


HTH



Wayne
 
S

SStory

Dooglo,

hmmm.

Well,

Split function is easy
just like
dim SomeStrings() as string=WhateverString.Split(";")
where ; is the separator--this would build a string array "SomeString" where
; was the separator to determine separate strings.

Now in making text boxes for this... I have 2 suggestions.
One if you are going to use text boxes, don't include the units.... the
double quote is a special char anyway and might cause you some
problems...besides you only need the numbers
So Idea #1

[Use the Up/Down Numeric Control] <labelcontrol here that has 'feet'> [Use
the Up/Down Numeric Control] <labelcontrol here that has the word 'inches'
or if you prefer the "

This avoids the issues of having any chars--just whole numbers.

Idea #2
If you have a very limited number of expected lengths and widths you might
use one combo box for each and fill it will all possible lengths....like
1foot, 1 inch, etc or 1' 1"

and of course each combo item would be from a class that has a property for
feet and one for inches and overrides tostring to show them together(that's
what the combobox uses to display)

I'm not sure which way you would prefer--if there will be too many
combinations, Idea 1 is better... and users may prefer idea #1

don't use "" for empty string, use string.empty----(doing so is more
efficient)

using the numeric control(again don't remember it's name and not in the IDE
right now, but it is an updown control that only allows numbers) instead of
a text box
you could say
TInch=CStr(YourNumericControlInches.Text) (or maybe .Value instead of
..Text; don't remember)

That way it will always have some value and you shouldn't get the error...
they will have to at least have 0 in the numeric control.

Hope this helps some....

Shane
 
H

Herfried K. Wagner [MVP]

* =?Utf-8?B?RG9vZ2xv?= said:
I'm writting a program to figure square feet and yards when the user inputs
"Length in feet and inch ( seperate boxes), Width in feet and inches
(seperate boxes), and thickness in feet and inches (all so in seperate
boxes). I haven't quit figured out the split function yet to use a single
text box for each. Anyways, when I enter data in each box; eg.

Turn on 'Option Strict On' and take a look at the 'C*' functions
('CInt', ...).
 
S

SStory

oops... I meant use CInt()
instead of CStr()

SStory said:
Dooglo,

hmmm.

Well,

Split function is easy
just like
dim SomeStrings() as string=WhateverString.Split(";")
where ; is the separator--this would build a string array "SomeString" where
; was the separator to determine separate strings.

Now in making text boxes for this... I have 2 suggestions.
One if you are going to use text boxes, don't include the units.... the
double quote is a special char anyway and might cause you some
problems...besides you only need the numbers
So Idea #1

[Use the Up/Down Numeric Control] <labelcontrol here that has 'feet'> [Use
the Up/Down Numeric Control] <labelcontrol here that has the word 'inches'
or if you prefer the "

This avoids the issues of having any chars--just whole numbers.

Idea #2
If you have a very limited number of expected lengths and widths you might
use one combo box for each and fill it will all possible lengths....like
1foot, 1 inch, etc or 1' 1"

and of course each combo item would be from a class that has a property for
feet and one for inches and overrides tostring to show them together(that's
what the combobox uses to display)

I'm not sure which way you would prefer--if there will be too many
combinations, Idea 1 is better... and users may prefer idea #1

don't use "" for empty string, use string.empty----(doing so is more
efficient)

using the numeric control(again don't remember it's name and not in the IDE
right now, but it is an updown control that only allows numbers) instead of
a text box
you could say
TInch=CStr(YourNumericControlInches.Text) (or maybe .Value instead of
.Text; don't remember)

That way it will always have some value and you shouldn't get the error...
they will have to at least have 0 in the numeric control.

Hope this helps some....

Shane


Dooglo said:
I'm new VB and programming all together, but I'm getting he hang of it.

My question is;

my program works and figures out the correct answer. But when I enter it
differently, like this;

Lenght 2' not entering inches leaving box empty
Widht 2' not entering inches leaving box empty
Thickness not entering feet leaving box empty but entering 4"

it gives me an error "Cast from string " " to type 'Integer' is not valid."
Here is some of what Ive written.


Public LFeet, WFeet, TFeet As Integer
Public LInch, WInch, TInch As Integer
Public SqrFeet, Yard As Integer

Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCompute.Click

If Me.txtLInch.Text = " " Then TInch = 0
If Me.txtWInch.Text = " " Then WInch = 0
If Me.txtTFeet.Text = " " Then TInch = 0

Me.LFeet = Me.txtLFeet.Text
Me.LInch = Me.txtLInch.Text
Me.WFeet = Me.txtWFeet.Text
Me.WInch = Me.txtWInch.Text
Me.TFeet = Me.txtTFeet.Text
Me.TInch = Me.txtTInch.Text

If Me.WInch = 0 And Me.LInch = 0 Then Me.txtSqrFeet.Text = Me.LFeet
* Me.WFeet Else

Me.txtSqrFeet.Text = (Me.LFeet + (Me.LInch / 12)) * (Me.WFeet +
(Me.WInch / 12))

Me.SqrFeet = Me.txtSqrFeet.Text

If Me.TFeet = 0 Then Me.txtYard.Text = (Me.SqrFeet * (Me.TInch / 12)
/ 27) Else

Me.txtYard.Text = (Me.SqrFeet * (Me.TFeet + (Me.TInch / 12))) / 27

End Sub


I know that I'm using the wrong variables here, I can't quit figure it out
though.

Thanks to Anyone Who Replies.
 
S

SStory

But again,

Better to use string.empty instead.

using "" means vb creates a new string for each ""

using string.empty means there is only one instance of "" and it is
string.empty and so it is more efficient.

Shane
 
W

Wayne Wengert

Thanks - never even realized string.empty was there. I'll be sure to use
that in the future.

Wayne
 
H

Hal Rosser

you should check the contents of the text boxes first to see if they can be
converted to numbers. - (use the isnumeric function)
then you should convert to and assign to -> variables of a numeric data
type - like double.
then use the numeric variables to do your calculations.
by the way - at the top - you should type 'option strict on' and 'option
explicit on'
vb 6.0 was much easier to learn for newbies.
 
S

Scott M.

IsNumeric doesn't always give you an accurate answer though.

124EZ would be considered numeric, but would show up as 12
EZ12 would not be considered numeric

Using regular expressions would be better.
 
H

Hal Rosser

Regular expressions - as you say is the best approach - but the OP is new to
programming
Regular expressions to a newbie is pretty intimidating. But is worth
learning - for any language.
 
S

SStory

I know brother... there's too much for any of us to know it all.
Thank goodness we have this group. :)

Shane
 
S

SStory

Why bother with either when there is already an up down control that only
takes numbers???

Seems to me the more logical choice in this circumstance... But if I were
going to use a textbox,
I'd trap the keypress event and only allow ASCII values for digits or "." or
Backspace

And of course check to allow only one "."

Seems easier to just use the numeric control.

My 2 cents....

Shane
 
S

Scott M.

Why bother with either when there is already an up down control that only
takes numbers???

If this is an ASP.NET Web Application, then the control you are talking
about doesn't exist (however there are validation controls to take care of
inputting only numeric values). But that wasn't what the question was.
Seems to me the more logical choice in this circumstance... But if I were
going to use a textbox,
I'd trap the keypress event and only allow ASCII values for digits or "."
or
Backspace

For a web application, this would have to be done client-side with
JavaScript (pain) and even for a Windows application it is a pretty
inefficient approach.
 
H

Hal Rosser

Good plan - I do it that way too -
But- You also have to watch out for the user doing a 'Paste' into the text
box
 
C

Cor Ligthert

Shane,
using "" means vb creates a new string for each ""

using string.empty means there is only one instance of "" and it is
string.empty and so it is more efficient.
MyString.empty
myString = Nothing
myString = ""
Are all the same

String Is Nothing catches even an not instanced string.

In this newsgroup is always told (not particulary by me) that the prefered
way was myString = "" because that shows exactly the situation. There is a
string however it holds no characters, the other ones can be confusing.

Cor
 
C

Cor Ligthert

Scott

When something is a numeric expression, what is than false with that, when
it is told that it is, in my opinion only when you have extra conditions,
than can your statement be true.

Cor
 
C

Cor Ligthert

Hal,

Probably you know, however to make this thread more complete do I add this
after your name.

Therefore you should use it in my opinion in the way it is done here in the
"validating" event.

Cor
 
G

Guest

Thanks so much to all of you for replying to my post.
Sorry that I didn't reply back sooner.

Thanks again for the help.
 
Top