1

I am using the following structure many times throughout my code to pass a set of data to a function:

Public Structure MyStruct
    Dim ResourceID As String
    Dim RSRCName As String
    Dim CommercialType As String
    Dim ParticipantName As String
    Dim ASReserveZone As String
    Dim SCADAStatusQuality As String
    Dim SCADAStatus As String
    Dim SCADAMWQuality As String
    Dim ManualDispatchReason As String

    Dim RegUpQual As Boolean
    Dim RegDownQual As Boolean
    Dim SuppQual As Boolean
    Dim SpinQual As Boolean
    Dim UseEmergencyLimits As Boolean
    Dim FollowLastDispatch As Boolean

    Dim ASOfferCurveDict As HybridDictionary

    Dim XIC As Integer
    Dim PriceBased As Integer
    Dim PNodeID As Integer
    Dim Committed As Integer
    Dim CommitmentStatus As Integer
    Dim ManualDispatch As Integer
    Dim LastApprdNumOfIntervalsToMax As Integer

    Dim MWCurve() As Double
    Dim EnergyOfferCurve() As Double
    Dim PlannedMW As Double
    Dim SCADAMW As Double
    Dim InitialOnHours As Double
    Dim LastApprdDispatchMW As Double
    Dim TotalRampedCRDeployMW As Double
End Structure

Dim Struct as MyStruct

I can use Struct = Nothing to reset the data, but I would rather iterate through the structure and set all the numeric values to 999999999. Any idea how to do this?

1
  • 5
    Wow, that structure is huge. You should try to reduce that by separating concerns and observing the single responsibility principle. Commented Feb 15, 2013 at 15:16

1 Answer 1

3

You could do it with reflection, but I wouldn't necessarily recommend it. I'd sooner just add a Clear or Reset method to the structure which sets all the values to their default values. Another option would be to set the default values when the fields are declared:

Public Structure MyStruct
    ' ...
    Dim XIC As Integer = 999999999
    Dim PriceBased As Integer = 999999999
    Dim PNodeID As Integer = 999999999
    ' ...
End Structure

Then, to reset a variable to all the default values, just do this:

myVariable = New MyStruct()

I should also mention, unless it really needs to be a structure for some reason, you should change this to a class. It's quite a lot of data to be passing around on the stack all the time.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks Steven. I am an Electrical Engineer that did VBA for several years before upgrading to .net. So, I'm having to teach myself how to get away from the legacy coding practices! This is going to sound horrible, but with this tool I'm building, I'm actually learning to use classes for the first time.
:) Don't worry. It's nothing to be ashamed of. All of us remember the first time we used a class. Everyone was a beginner once. For instance, I know nothing about electrical engineering :)
I took your advice and got rid of the structure and moved everything into the class. It is a lot easier to manage now.
It's impossible to say, without more details, whether or not moving everything into another class makes sense. I'll take your word for it. But that's not what I meant. What I meant was simply changing the line Public Structure MyStruct to Public Class MyStruct. Classes and structures are largely the same in practice. The only difference between them is that classes are reference types and structures are value types. That has some implications to how you use variables of those types, but it also affects how much memory on the stack is used.
With structures, all of the data is stored directly in the stack (which is a limited space). With classes, only a reference to the actual data is stored on the stack, so it uses much less stack space. If you put too much data on the stack, it can become inefficient and you run the risk of an out-of-stack-space exception. As such, structures are usually only recommended when the data is relatively small.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.