数据类型定义的问题 [英] problem with data type definition
问题描述
伙计们,
我对申报某个变量感到有点困惑。
我在一个按钮下定义了它。这是在一个do-while循环中,它存储计算值,而其他正在执行。
我想用另一个按钮提取该值。我知道我可以使用函数,但是有没有办法可以使用某种数据类型来定义变量,该数据类型存储该变量的当前值?
以下是代码: total是变量的名称。
Hi guys,
I'm a little bit confused to declare a certain variable.
I defined it under a button.It's in a do-while loop and it stores calculated values while other this are being executed.
And i wanted to extract that value using another button.I know i can use a function but is there a way that i can define the variable using a certain data type that stores the current value of that variable?
Here's the code anyway:"total" is the name of the variable.
cmd = New OleDbCommand("SELECT [sub_pay_item_quantity].[quantity],[sub_pay_item_unit_rate].[rate] FROM " &
"[sub_pay_item_quantity],[sub_pay_item_unit_rate] WHERE [sub_pay_item_quantity].[sub item]=[sub_pay_item_unit_rate].[sub item] AND " &
"[sub_pay_item_quantity].[sub item]='" & subItem & "' AND [sub_pay_item_quantity].[bridge type]='" & bridgeType & "' " &
"AND [sub_pay_item_quantity].[span]='" & span & "'", conn)
cmd1 = New OleDbCommand("SELECT * FROM sub_pay_item_quantity WHERE [sub item]='" & subItem & "' AND [bridge type]='" & bridgeType & "'" & _
"AND [span]='" & span & "'", conn)
data_reader = cmd.ExecuteReader()
data_reader1 = cmd1.ExecuteReader()
If data_reader.HasRows = True Then
Do While data_reader.Read()
quantity = CDbl(data_reader.Item("quantity"))
rate = CDbl(data_reader.Item("rate"))
amount = (quantity * rate)
result += amount
Loop
total = result
Else
MsgBox("Unit rate does not exist", vbCritical, "Bridge Construction Cost Estimate")
Exit Sub
End If
If data_reader1.HasRows = True Then
Do While data_reader1.Read()
payItem = CDbl(data_reader1.Item("pay item"))
subpayItem = CDbl(data_reader1.Item("sub pay item"))
unit = data_reader1.Item("unit")
Loop
End If
RichTextBox1.SelectionAlignment = HorizontalAlignment.Left
RichTextBox1.AppendText(payItem & vbTab & vbTab & subpayItem & vbTab & vbTab & subItem & vbTab & vbTab &
unit & vbTab & bridgeType & vbTab & vbTab & span & vbTab &
vbTab & quantity & vbTab & vbTab & rate & vbTab & vbTab & amount & vbNewLine)
推荐答案
变量的性质是,根据定义,它存储其当前值。此外,变量的数据类型(int,string,float,某些类类型等)不会影响其当前值的可访问性......只是变量存储的值的类型。
我怀疑你试图解决的问题是范围之一 - 定义的变量在哪里,以及程序的其他部分可以访问它。
虽然你没有显示方法的开头(即定义了总计
)我怀疑你已经在上面显示的代码的方法中将它定义为局部变量。
为了能够从其他方法访问该变量,你必须将其移动到两种方法共有的一些范围。最明显的地方是使它成为类的实例成员(字段),它还包含需要使用变量的方法。
The nature of a variable is that, by definition, it stores its current value. Also, the data type of the variable (int, string, float, some class type, etc.) doesn't affect the accessibility of its current value ... just the kind of value the variable stores.
I suspect that the issue you are trying to resolve is one of scope - where is the variable defined, and what other parts of the program have access to it.
Although you don't show the start of the method (i.e. wheretotal
is defined) I suspect that you've defined it as a local variable in the method whose code you show above.
To be able to access that variable from some other method you must move it to some scope that is common to both methods. The most obvious place is to make it an instance member (a field) of the class that also contains the methods that need to use the variable.
声明一个全局变量:
Declare a global variable:
Public final As Double
然后声明一个本地静态变量:
Then declare a local static variable:
Static result As Double
然后你可以操作那个静态变量,然后你可以将该变量赋给gobal变量:
Then you can manipulate that static variable and after that you can assign that variable to the gobal variable:
data_reader = cmd.ExecuteReader()
If data_reader.HasRows = True Then
Do While data_reader.Read()
quantity = CDbl(data_reader.Item("quantity"))
rate = CDbl(data_reader.Item("rate"))
amount = (quantity * rate)
result = result + amount
Loop
final = result
Else
MsgBox("Unit rate does not exist", vbCritical, "Bridge Construction Cost Estimate")
Exit Sub
End If
最后你可以在任何地方访问全局变量一个代码。因此它显示了该静态变量的最后一个值。
And at last you access the global variable anywhere inside a code.Thus it displays the last value of that static variable.
这篇关于数据类型定义的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!