For example...
PUBLIC Structure System_Scoring_Structure
dim daily_evaluation_score as double
dim weekly_evaluation_score as double
dim monthly_evaluation_score as double
dim yearly_evaluation_score as double
End Structure
..after using this structure, I want to reset each variable to zero, before re-using it again for another data record to process. All I can think of is to set each variable to zero. (My actual Structure has a lot more variables.)
Redim and Erase only work on arrays.
How about just new'ing it up?
Dim scores As System_Scoring_Structure = New System_Scoring_Structure
' TODO: Set the scores members and use it somewhere...
' ...
' now zero the structure out..
scores = New System_Scoring_Structure
' it's reset and ready to be filled again.
I've just run a simplistic benchmark to see which is the quickest method.
If any of your variables are reference types, I would recommend creating a class, definitely.
Assuming that all the variables in your structure are value types, I compared the following two ways.
I created my own structure of value types with 27 variables of the Double
type and wrote a method within the structure to set each double to 0 called .Reset
I then ran this code with a couple of stopwatches to see how long this took to complete 1000000000 times, and how long it took to simply assign as a new instance of the structure.
My structure was called test
- yes capitalization an all that ...
Dim t1 As New Stopwatch
Dim s As test
Dim s1 As test
t1.Start()
For i As Integer = 1 To 1000000000
s.reset()
Next
t1.Stop()
Dim t2 As New Stopwatch
t2.Start()
For i As Integer = 1 To 1000000000
s1 = New test
Next
t2.Stop()
MessageBox.Show(t1.ElapsedMilliseconds & " " & t2.ElapsedMilliseconds)
Executing the sub 1000000000 times took about 21 seconds, while executing the New
assignment 1000000000 times took 24 seconds on my pc. So there is a noticeable difference, but of your code isn't going to reset the variable an awful lot, it's down to personal taste. Memory consumption not significantly different so that's not a consideration either.
Out of interest, I changed the structure to a class which, according to Microsoft is the recommended way of create such a thing the if data takes up more than 16 bytes of memory, and ran the benchmark again. This time is took about 19 seconds and 31 seconds respectively and the second loop used a couple of megabytes more memory.
So .. up to you really.
You can delegate this to a function or subroutine - but understand whether you want to adjust the original data (first example - ByRef), or replace the original data with a new copy (second example).
Private sub Reset_system_scoring(ByRef sss as System_Scoring_Structure)
sss.daily_evaluation_score = 0
sss.weekly_evaluation_score = 0
[...]
end sub
or
Private function Reset_system_scoring() as System_Scoring_Structure
dim sss as System_Scoring_Stucture
sss.daily_evaluation_score = 0
sss.weekly_evaluation_score = 0
[...]
return sss ' OK - this is really ugly code.
end sub
As previously suggested - re New
ing the structure could also work - this assumes that all variables inside will default to 0 unless you have a specific constructor.
Refactoring the structure into a class should also be considered. You can use it to store base data and then do internal calculations to get aggregated data. This would also allow you to code in various methods, including Reset
, which make your main code look neater.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.