Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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)
Posted

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. where total 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.
 
Share this answer
 
Comments
Nebilo 4-Nov-13 9:27am    
can you help me create a function, this is the only problem i'm facing to complete this assignment
Chris Ross 2 4-Nov-13 9:35am    
Your text books should show you how to create functions. Or, your professor can direct you towards suitable reference information.

Regrettably, if you don't know how to create a function in your chosen programming language, you have more fundamental problems to resolve than choosing the best place to declare a variable. And, although Code Project readers like to be able to help each other, we're not in the position to be teaching the fundamentals of programming - and explaining how to write a function falls squarely into that category.
Nebilo 6-Nov-13 3:51am    
Thanks chris
Declare a global variable:
VB
Public final As Double

Then declare a local static variable:
VB
Static result As Double

Then you can manipulate that static variable and after that you can assign that variable to the gobal variable:
VB
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900