选择适当的变量 [英] Selecting Proper Variables

查看:73
本文介绍了选择适当的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新的VB并且一起编程,但是我得到了它的支持。


我的问题是;


当用户输入时,我正在编写一个程序来计算平方英尺和码数

以英尺和英寸为单位的长度(单独的框),以英尺和英寸为单位的宽度

(单独的盒子),以英尺和英寸为单位的厚度(所有这些都是单独的

盒子)。我没有放弃想出分割功能,但每个都使用单个

文本框。无论如何,当我在每个盒子中输入数据时;例如。


长度2''0"

宽度2''0"

厚度0''4"


我的程序正常工作并找出正确的答案。但当我以不同的方式输入

时,就像这样;


长度2''不进入英寸,让盒子空着

Widht 2 ''不进入英寸,让箱子空了

厚度不进入脚,让箱子空,但输入4


它给我一个错误从字符串铸造 ; "键入''Integer''是无效的。

以下是我写的一些内容。

公共LFeet,WFeet,TFeet作为整数

Public LInch,WInch,TInch As Integer

Public SqrFeet,Yard As Integer


Private Sub btnCompute_Click(ByVal sender As System.Object,ByVal e作为

System.EventArgs)处理btnCompute.Click


如果Me.txtLInch.Text =" "然后TInch = 0

如果Me.txtWInch.Text =" "那么WInch = 0

如果Me.txtTFeet.Text =" "然后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


如果Me.WInch = 0而Me.LInch = 0那么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


如果Me.TFeet = 0那么Me.txtYard.Text =(Me.SqrFeet *(Me.TInch / 12)

/ 27)否则


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

结束子

我知道我正在使用错误的变量在这里,我不能解决它

虽然。


感谢任何回复的人。

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.

推荐答案

Dooglo,


在Visual Basic中转换非常简单。


文本框文本属性中的文本是一个字符串,但是要将它用作

整数,你可以(测试后它是完全数字的)确实

myvalue为整数= CInt(mytextbox.text)


当它不是完全数字的时候 - 和加号是标志,比它将b $ b抛出一个错误。


从文本回到int你可以做到这一点


mytextbox.text = Cstr(myvalue)但是这里主要使用的是

mytextbox.text = myvalue.ToString


我希望到目前为止这有用吗? />

Cor
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
我是一个新的VB并且编程在一起,但是我得到了它的支持。

我的问题是;

我正在编写一个程序来计算用户
输入的平方英尺和码数以英尺和英寸为单位的长度(单独的框),宽度在英尺和英寸
(单独的盒子),厚度以英尺为单位d英寸(所有这些都是单独的
盒子)。我还没有放弃想出分割功能,但每个都使用单个
文本框。无论如何,当我在每个盒子中输入数据时;例如。

长度2''0"
宽度2''0"
厚度0''4"

我的程序工作并计算出来正确答案。但是当我进入它时
不同,就像这样;

长度2''不进入英寸,让盒子空着
Widht 2''不进入英寸,让盒子空着
厚度没有进入脚,让箱子空了,但输入4

它给了我一个错误从字符串中铸造 "输入''整数''不是
有效。以下是我写的一些内容。

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)处理btnCompute.Click

如果Me.txtLInch.Text =" "那么TInch = 0
如果Me.txtWInch.Text =" "那么WInch = 0
如果Me.txtTFeet.Text =" "然后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

如果Me.WInch = 0而Me.LInch = 0然后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

如果Me.TFeet = 0然后Me.txtYard.Text =(Me.SqrFeet *(Me.TInch /
12)/ 27)Else

Me.txtYard.Text =(Me.SqrFeet *(我。 TFeet +(Me.TInch / 12)))/ 27
结束子

我知道我在这里使用了错误的变量,我不能放弃想通了。

感谢任何回复的人。
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.



问题在于你不是检查文本框没有数据,你是检查空格字符而不是空字符串:


如果Me.txtLInch.Text =" "那么TInch = 0

....应该......

如果Me.txtLInch.Text =""然后TInch = 0

所以,当没有数据被输入时,它没有被设置为零,当你尝试

来获取文本框值并设置它变成整数变量,你是一个空字符串并使它成为一个整数,这是错误信息报告的




其次,我认为最好将脚和英寸保持为单独的

文本框,而不是尝试使它们成为一个。


第三,我不认为你需要将你的变量声明为Public,除非你打算在另一个类中使用它们,所以Private会这样做。


接下来,您可以将没有数据的检查和值的分配结合起来

一个如果声明:


''检查为空文本框

如果txtLInch.Text =""然后

TInch = 0

否则

TInch = CType(txtTInch.Text,整数)

结束如果


接下来,使用关键字Me。当然可以,但不是必需的,并且可以

使你的陈述更清晰。而不是:


如果Me.WInch = 0而且Me.LInch = 0那么

Me.txtSqrFeet.Text = Me.LFeet * Me.WFeet

其他

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

( Me.WInch / 12))

Me.SqrFeet = Me.txtSqrFeet.Text

结束如果


您可以写:


如果WInch = 0且LInch = 0那么

txtSqrFeet.Text = LFeet * WFeet

Else

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

SqrFeet = txtSqrFeet.Text

结束如果< br $> b $ b祝你好运!


" Dooglo" <做**** @ discussions.microsoft.com>在消息中写道

news:08 ********************************** @ microsof t.com ...
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!


"Dooglo" <Do****@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
我是一个新的VB和编程一起,但是我得到了它的支持。

我的问题是; 以英尺和英寸为单位的长度(单独的盒子),以英尺为单位的宽度时,我正在编写一个程序来计算平方英尺和码数。 />(单独的盒子),以英尺和英寸为单位的厚度(所有这些都是单独的盒子)。我还没有放弃想出分割功能,但每个都使用单个
文本框。无论如何,当我在每个盒子中输入数据时;例如。

长度2''0"
宽度2''0"
厚度0''4"

我的程序工作并计算出来正确答案。但是当我进入它时
不同,就像这样;

长度2''不进入英寸,让盒子空着
Widht 2''不进入英寸,让盒子空着
厚度没有进入脚,让箱子空了,但输入4

它给了我一个错误从字符串中铸造 "输入''Integer''不是
有效。
以下是我写的一些内容。

公共LFeet,WFeet,TFeet作为整数
公开LInch,WInch,TInch作为整数
公共SqrFeet,Yard作为整数

私有子btnCompute_Click(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理btnCompute。点击

如果Me.txtLInch.Text =" "那么TInch = 0
如果Me.txtWInch.Text =" "那么WInch = 0
如果Me.txtTFeet.Text =" "然后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

如果Me.WInch = 0而Me.LInch = 0然后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

如果Me.TFeet = 0然后Me.txtYard.Text =(Me.SqrFeet *(Me.TInch /
12)
/ 27)Else

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

我知道我在这里使用了错误的变量,我可以但是,请不要把它搞清楚。

感谢任何回复的人。
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.



看起来你的空值测试s正在寻找单个空间

而不是空字符串?尝试更改:


如果Me.txtLInch.Text =" "然后TInch = 0

如果Me.txtWInch.Text =" "那么WInch = 0

如果Me.txtTFeet.Text =" "然后TInch = 0




如果Me.txtLInch.Text =""然后TInch = 0

如果Me.txtWInch.Text =""那么WInch = 0

如果Me.txtTFeet.Text =""然后TInch = 0

HTH


Wayne


" Dooglo" <做**** @ discussions.microsoft.com>在消息中写道

news:08 ********************************** @ microsof t.com ...
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

"Dooglo" <Do****@discussions.microsoft.com> wrote in message
news:08**********************************@microsof t.com...
我是一个新的VB和编程在一起,但我正在把它挂起来。

我的问题当用户
输入以英尺为单位的长度和英寸(单独的框),以英尺为单位的宽度和英寸时,我正在编写一个程序来计算平方英尺和码数/>(单独的盒子),以英尺和英寸为单位的厚度(所有这些都是单独的盒子)。我还没有放弃想出分割功能,但每个都使用单个
文本框。无论如何,当我在每个盒子中输入数据时;例如。

长度2''0"
宽度2''0"
厚度0''4"

我的程序工作并计算出来正确答案。但是当我进入它时
不同,就像这样;

长度2''不进入英寸,让盒子空着
Widht 2''不进入英寸,让盒子空着
厚度没有进入脚,让箱子空了,但输入4

它给了我一个错误从字符串中铸造 "输入''整数''不是
有效。以下是我写的一些内容。

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)处理btnCompute.Click

如果Me.txtLInch.Text =" "那么TInch = 0
如果Me.txtWInch.Text =" "那么WInch = 0
如果Me.txtTFeet.Text =" "然后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

如果Me.WInch = 0而Me.LInch = 0然后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

如果Me.TFeet = 0然后Me.txtYard.Text =(Me.SqrFeet *(Me.TInch /
12)/ 27)Else

Me.txtYard.Text =(Me.SqrFeet *(我。 TFeet +(Me.TInch / 12)))/ 27
结束子

我知道我在这里使用了错误的变量,我不能放弃想出来
尽管如此。

感谢任何回复的人。
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.



这篇关于选择适当的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆