PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Public arrays across multiple forms-VBForums
Results 1 to 16 of 16

Thread: Public arrays across multiple forms

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2018
    Location
    Brigham City Utah
    Posts
    16

    Public arrays across multiple forms

    I am having a problem, or several problems defining arrays in a root module and using the data from those arrays in other forms within the application. For example, Public XYZ(2) as Single is defined in module SageData. In some forms I have been able to communicate the parameters by using a ReDim Preserve XYZ(2) statement in the routine using the data. The XYZ values are as declared. In the form of interest (frmReport) the values for XYZ are all zero independent of the ReDim statement, a direct link to XYZ, or even linked through SageData.XYZ. There must be something I am missing, even though it seems logical that using the public XYZ directly appears to be sufficient. Any clarification would be appreciated. Thanks.
    CostSage

  2. #2
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    524

    Re: Public arrays across multiple forms

    I would guess on your frmReport you have a form level (or procedure level in the affected procedure(s)) array declared that also has the name XYZ.

    If that isn't the case, you'll probably need to post some relevant code from your module and form(s) and clarify what, exactly, the problem is.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,945

    Re: Public arrays across multiple forms

    We need to see your code...

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2018
    Location
    Brigham City Utah
    Posts
    16

    Re: Public arrays across multiple forms

    I declared some working arrays in the SageData Module that are used throughout the application as follows:
    Code:
        Public pACAP(0 To 2) As Single            ' Working personnel characteristic vectors
        Public pAEXP(0 To 2) As Single
        Public pAEXPType(0 To 2) As Single
        Public pAEXPYrs(0 To 2) As Single
        Public pDEXP(0 To 2) As Single
        Public pDEXPType(0 To 2) As Single
        Public pDEXPYrs(0 To 2) As Single
        Public pLEXP(0 To 2) As Single
    
    In the SageCode Module I was forced to ReDim the variables as follows to perform a simple technology calculation below:
    
            ReDim Preserve pACAP(2)
            ReDim Preserve pAEXP(2)
            ReDim Preserve pPCAP(2)
            ReDim Preserve suMODP(2)
            ReDim Preserve suTOOL(2)
            ReDim Preserve eRESP(2)
            ReDim Preserve eTURN(2)
    
            ' Calc ulate Ctb components
            For indx = 0 To 2
                X = pPCAP(indx)
                X = pACAP(indx) * pAEXP(indx) * suMODP(indx)
                X = X * pPCAP(indx) * suTOOL(indx) * eRESP(indx)
                t = -3.70945 * Log(X / 4.11)
                A = t / (5.0# * eTURN(indx))
                Ctb(indx) = 2000.0# * Exp(A)
            Next indx
            CtbEff = (Ctb(0) + 4.0# * Ctb(1) + Ctb(2)) / 6.0#
            CtbStd = (Ctb(2) - Ctb(0)) / 6.0#
            Exit Sub
    
        End Sub
    In a report form (frmReport) I need to use the same variables in a in a formatted summary. In this report the values for each of the variables is 0.0. The code in the report is as follows:
    Code:
    gfx.DrawString("  Analyst Capability", RFont, RBrush, X, Y)
            gfx.DrawString(Format(CStr(pACAP(0)), "##0.00 "), RFont, RBrush, X + 160, Y)
            gfx.DrawString(Format(CStr(pACAP(1)), "##0.00 "), RFont, RBrush, X + 219, Y)
            gfx.DrawString(Format(CStr(pACAP(2)), "##0.00 "), RFont, RBrush, X + 277, Y)
            ACAPMean = CSng((pACAP(0) + 4.0# * pACAP(1) + pACAP(2)) / 6.0#)
            ACAPStd = CSng((pACAP(2) - pACAP(0)) / 6.0#)
            gfx.DrawString(Format(CStr(ACAPMean), "##0.00"), RFont, RBrush, X + 336, Y)
            gfx.DrawString(Format(CStr(ACAPStd), "##0.00"), RFont, RBrush, X + 395, Y)
            gfx.DrawString("  ACAP", RFont, RBrush, X + 453, Y)
            Y = CSng(Y + 1.5 * delta)
    ……
    I assumed initially that the Public declaration in SageData would satisfy the declaration in this case. No so. I also tried to use the ReDim declarations that I was forced to use in the SageCode module. No so again.The parameter values were again 0.0.
    I could not find any Dim statements that would have created a new zero valued variable. I must be missing something obvious. I hope this helps.
    Thanks for you help.

    costSage
    Last edited by Shaggy Hiker; Yesterday at 04:20 PM. Reason: Added CODE tags.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,945

    Re: Public arrays across multiple forms

    There's some local or form level variables hiding your module variables. There's no need for Redim Preserve unless you're resizing your array. Start with a fresh project, for testing purposes...

    Code:
    Module Module1
        'declare here
        Public pACAP(2) As Single
    
        Public Sub mySub()
            pACAP(0) = 1
            pACAP(1) = 2
            pACAP(2) = 3
        End Sub
    
        Public Sub myOtherSub()
            MsgBox(pACAP(0) + pACAP(1) + pACAP(2))
        End Sub
    
    End Module
    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            mySub()
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MsgBox(pACAP(0))
            myOtherSub()
        End Sub
    
    End Class
    Then in your test project, try changing the form code, like this...

    Code:
    Public Class Form1
    
        Public pACAP(2) As Single
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            mySub()
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MsgBox(pACAP(0))
            myOtherSub()
        End Sub
    
    End Class
    The local array variable hides the module level variable.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,984

    Re: Public arrays across multiple forms

    It seems to me that you're making a poor job of explaining the problem. There's no reason to have any ReDim statements anywhere. You create the arrays in one place and you use them everywhere else. You haven't actually explained why you think you need to use ReDim at all. Are you saying, without actually saying, that you are setting the elements of the arrays in one place and then when you try to use them elsewhere you are not seeing the values you set? I'm afraid that that just doesn't happen. I'm not seeing anywhere in your code that you are assigning anything to any element of any array so I'm not sure why you would expect to see anything change in those arrays. My guess is that you think you're changing something in the array here:
    vb.net Code:
    1. X = pPCAP(indx)
    2. X = pACAP(indx) * pAEXP(indx) * suMODP(indx)
    3. X = X * pPCAP(indx) * suTOOL(indx) * eRESP(indx)
    You're not. That first line doesn't create any relationship between the X variable and the element of the pPCAP array. Any changes you make to X after that are made to X, not the array element. If you want to change the array element then you have to actually assign to the array element, e.g.
    vb.net Code:
    1. X = pPCAP(indx)
    2. X = pACAP(indx) * pAEXP(indx) * suMODP(indx)
    3. X = X * pPCAP(indx) * suTOOL(indx) * eRESP(indx)
    4. pPCAP(indx) = X
    That's just a guess though, because I really am not sure what problem you're trying to solve.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jun 2018
    Location
    Brigham City Utah
    Posts
    16

    Re: Public arrays across multiple forms

    I ran across another symptom of the problem associated with the Public Arrays Across Multiple Forms that is apparent in the application initialization sequence as follows:
    Code:
       Sub InitializeSage()
            Dim sagePath As String
            sagePath = Application.StartupPath & "\Sage.DAT"
            LoadWorkingVectors() 'load working vector initial value
            LoadPublicVectors()  ' Load Task structures into working vectors
    The first subroutine call initializes the values for each working array as part of the application startup sequence. The Public array definitions are contained in the SageData Module.
    Code:
          Public sSOrig(0 To 2) As Single          ' Working size data arrays
        Public sSNew(0 To 2) As Single
        Public sSMod(0 To 2) As Single
        Public sDesF(0 To 2) As Single
        Public sImpF(0 To 2) As Single
        Public sTestF(0 To 2) As Single
    .
    .
    I have included a sample of the startup LoadWorkingVectors code.
    Code:
     Sub LoadWorkingVectors()
            Dim i As Integer
            Dim j As Integer
            ' Initialize Sage Size and Complexity parameters
            sSOrig(0) = 8000.0
            sSOrig(1) = 10000.0
            sSOrig(2) = 12000.0
            SizeTask.SOrigCom = ""
            sSNew(0) = 4000.0
    		.
    The LoadPublicVectors subroutine copies the Task Structures to the Working Arrays to the structure arrays into the working arrays.
    Code:
     Sub LoadPublicVectors()
            ' Unload Task Data into public data arrays
            ' Load public size vectors
            sSOrig = SizeTask.SOrig
            sSNew = SizeTask.SNew
            sSMod = SizeTask.SMod
            sDesF = SizeTask.DesF
            sImpF = SizeTask.ImpF
            ' Load public function point arrays
            fFPcount = FP.FPcount
            diDIcount = DI.DIcount
            diDIeff = DI.DIeff
            ' Load public complexity vector
            cD = Cmplx.D
            MsgBox(cD(2))
            ' Load public personnel vectors
    The application program halts without an error message at the first statement involving specific array locations. In this example that situation occurs in the MsgBox statement. In the sample I loaded the values in the working arrays before the structure copy. Obviously, the LoadPublicVectors subroutine overwrites the LoadWorkingVectors operation. If the order of the two subroutine calls is reversed as it should be to order the loading correctly halts the program upon encountering the first subscripted operation. The operations do not deal with the subscripts correctly. The plot thickens.
    Last edited by Shaggy Hiker; Yesterday at 04:22 PM. Reason: Added CODE tags.

  8. #8
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    524

    Re: Public arrays across multiple forms

    The LoadPublicVectors subroutine copies the Task Structures to the Working Arrays to the structure arrays into the working arrays
    What does that sentence mean?

    Code:
    Sub LoadPublicVectors()
    ' Unload Task Data into public data arrays
    ' Load public size vectors
    sSOrig = SizeTask.SOrig
    What is SizeTask? What is SOrig? What is sSOrig supposed to contain after this line completes? If, in that line of code I quoted above, you are intending on filling the sSOrig array with new values, why do you populate sSOrig array elements with values initially?

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,951

    Re: Public arrays across multiple forms

    I edited the posts to wrap the code in [CODE][/CODE] tags. You can do this, as well by pressing the # button and pasting code between the resulting tags.

    One thing to note is that you don't have to use the array range:
    Code:
    Public sSNew(0 To 2) As Single
    In VB6, you could use (I think) any range you wanted, though that may have only been allowed in even earlier versions of VB. All arrays in .NET are 0 based, so they all start at 0. So, you can just write:

    Code:
    Public sSNew(2) As Single
    As to the plot, I'm not convinced it is anywhere near as thick as you may think. What do you think this line does:
    Code:
    sSOrig = SizeTask.SOrig
    I'd say that the answer to that question is the source of your confusion, though I may be wrong. This is something that tips up a whole lot of people. People may think that sSOrig holds an array. It does not. It holds a reference to an array. The array is a block of memory somewhere out on the heap, and sSOrig effectively holds the address of that memory. So, when you do that assignment, you replace one address with another, not one array with another, or the contents of one array with the contents of another array. You set up sSOrig to hold the address of one block of memory, then you switched it to hold the address of a different block of memory. What you did NOT do is copy the contents of one array into a different array.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jun 2018
    Location
    Brigham City Utah
    Posts
    16

    Re: Public arrays across multiple forms

    The SizeData module contains data structures that are used in the estimating application. There are several structures (records) in the program including:
    Code:
    #
    SizeRecord  SizeTask
    TaskRecord  TaskData
    FPRecord  FPData
    CmplxRecord  CmplxData
    PersRecord  PersonData
    SupRecord  SupportData
    EnvRecord  EnvirData
    ProdRecord  ProductData
    MaintRecord  MaintData
    Code:
    #
    Each of the arrays within the records (and the working arrays) contain 3 values for least, most likely and maximum values for each parameter

    For example:
    Code:
        <Serializable()> Public Structure PersRecord
            Public ACAP() As Single            '0 To 2
            Public ACAPCom As String           ' comment* 48
            Public AEXP() As Single            '0 To 2
            Public AEXPType() As Single        '0 To 2
            Public AEXPYrs() As Single         '0 To 2
            Public AEXPCom As String           ' comment * 48
            Public DEXP() As Single            '0 To 2
    Arrays in the structures cannot be dimensioned in the structure.
    Public Person as PersRecord
    Public Person.ACAP(2) has an associated working array Public pACAP(2) as Single used in the application computations for simplicity.
    Code:
       <Serializable()> Public Structure SizeRecord                 ' contains development size and complexity information
            Public SOrig() As Single       ' 0 To 2 reused source code (sloc)
            Public SOrigCom As String      ' original source code comments * 48
            Public SNew() As Single        ' 0 To 2 new source code (sloc)
            Public SNewCom As String       ' new source code comments * 48
            Public SMod() As Single        ' 0 To 2 modified source code (sloc)
    
    
      Sub UseStruct()
            'Public SizeTask As SizeRecord
            Dim SizeTask As SizeRecord
            ReDim SizeTask.SOrig(2)
            ReDim SizeTask.SNew(2)
    		.
    		.
            'Public PersonData As PersRecord
            Dim Person As PersRecord
            ReDim Person.ACAP(2)
            ReDim Person.AEXP(2)
            ReDim Person.AEXPType(2)
            ReDim Person.AEXPYrs(2)
        End Sub
    The working size data arrays are defined in SageData as
    Code:
    .   Public sSOrig(0 To 2) As Single          ' Working size data arrays
        Public sSNew(0 To 2) As Single
        Public sSMod(0 To 2) As Single
        Public sDesF(0 To 2) As Single
        Public sImpF(0 To 2) As Single
        Public sTestF(0 To 2) As Single
    The LoadPublicVectors subroutine copies the Task Structures to the Working Arrays. Using SizeTask (a structure that contains all of the component size information) as an example, the task arrays for original size are copied into the working array (SOrig()  sSOrig() like the following loop
    Code:
    Dim I as integer
    For i = 0 to 2
    sSOrig(i) = SizeTask.SOrig(i)
    Next i
    And so forth for all of the SizeTask arrays. Visual Basic allows the shorthand copy, but gets unhappy if the index is used.

    Code:
    Sub LoadPublicVectors()
    ' Unload Task Data into public data arrays
    ' Load public size vectors
    sSOrig = SizeTask.SOrig
    What is SizeTask? What is SOrig? What is sSOrig supposed to contain after this line completes? If, in that line of code I quoted above, you are intending on filling the sSOrig array with new values, why do you populate sSOrig array elements with values initially?
    SizeTask theoretically is loaded with initial values when the application is started for the first time. This information is loaded into a file (sage.dat). Subsequent starts of the application call Sage.dat for the information to start the application. Much of my problem lies in testing the content of sSOrig(1) to validate the assignment. Any attempt to use the subscripted variable causes the application to abort with no supporting information.
    As to the plot, I'm not convinced it is anywhere near as thick as you may think. What do you think this line does:
    sSOrig = SizeTask.SOrig
    Code:
    sSOrig(0) = SizeTask.SOrig(0)
    sSOrig(1) = SizeTask.SOrig(1)
    sSOrig(3) = SizeTask.SOrig(2)
    It holds a reference to an array. The array is a block of memory somewhere out on the heap, and sSOrig effectively holds the address of that memory. So, when you do that assignment, you replace one address with another, not one array with another, or the contents of one array with the contents of another array. You set up sSOrig to hold the address of one block of memory, then you switched it to hold the address of a different block of memory. What you did NOT do is copy the contents of one array into a different array.
    Having set the assignment to move SizeTask.SOrig to sSOrig, it isn’t clear how I can access an element of the array such as sSOrig(1)? An uninformative abort isn't much value.
    Last edited by Shaggy Hiker; Today at 10:33 AM.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jun 2018
    Location
    Brigham City Utah
    Posts
    16

    Re: Public arrays across multiple forms

    The SizeData module contains data structures that are used in the estimating application. There are several structures (records) in the program including:
    Code:
    SizeRecord  SizeTask
    TaskRecord  TaskData
    FPRecord  FPData
    CmplxRecord  CmplxData
    PersRecord  PersonData
    SupRecord  SupportData
    EnvRecord  EnvirData
    ProdRecord  ProductData
    MaintRecord  MaintData
    Each of the arrays within the records (and the working arrays) contain 3 values for least, most likely and maximum values for each parameter

    For example:
    Code:
        <Serializable()> Public Structure PersRecord
            Public ACAP() As Single            '0 To 2
            Public ACAPCom As String           ' comment* 48
            Public AEXP() As Single            '0 To 2
            Public AEXPType() As Single        '0 To 2
            Public AEXPYrs() As Single         '0 To 2
            Public AEXPCom As String           ' comment * 48
            Public DEXP() As Single            '0 To 2
    Arrays in the structures cannot be dimensioned in the structure.
    Public Person as PersRecord
    Public Person.ACAP(2) has an associated working array Public pACAP(2) as Single used in the application computations for simplicity.
    Code:
       <Serializable()> Public Structure SizeRecord                 ' contains development size and complexity information
            Public SOrig() As Single       ' 0 To 2 reused source code (sloc)
            Public SOrigCom As String      ' original source code comments * 48
            Public SNew() As Single        ' 0 To 2 new source code (sloc)
            Public SNewCom As String       ' new source code comments * 48
            Public SMod() As Single        ' 0 To 2 modified source code (sloc)
    
    
      Sub UseStruct()
            'Public SizeTask As SizeRecord
            Dim SizeTask As SizeRecord
            ReDim SizeTask.SOrig(2)
            ReDim SizeTask.SNew(2)
    		.
    		.
            'Public PersonData As PersRecord
            Dim Person As PersRecord
            ReDim Person.ACAP(2)
            ReDim Person.AEXP(2)
            ReDim Person.AEXPType(2)
            ReDim Person.AEXPYrs(2)
        End Sub
    The working size data arrays are defined in SageData as
    Code:
    .   Public sSOrig(0 To 2) As Single          ' Working size data arrays
        Public sSNew(0 To 2) As Single
        Public sSMod(0 To 2) As Single
        Public sDesF(0 To 2) As Single
        Public sImpF(0 To 2) As Single
        Public sTestF(0 To 2) As Single
    The LoadPublicVectors subroutine copies the Task Structures to the Working Arrays. Using SizeTask (a structure that contains all of the component size information) as an example, the task arrays for original size are copied into the working array (SOrig()  sSOrig() like the following loop
    Code:
    Dim I as integer
    For i = 0 to 2
    sSOrig(i) = SizeTask.SOrig(i)
    Next i
    And so forth for all of the SizeTask arrays. Visual Basic allows the shorthand copy, but gets unhappy if the index is used.

    Code:
    Sub LoadPublicVectors()
    ' Unload Task Data into public data arrays
    ' Load public size vectors
    sSOrig = SizeTask.SOrig
    What is SizeTask? What is SOrig? What is sSOrig supposed to contain after this line completes? If, in that line of code I quoted above, you are intending on filling the sSOrig array with new values, why do you populate sSOrig array elements with values initially?

    SizeTask theoretically is loaded with initial values when the application is started for the first time. This information is loaded into a file (sage.dat). Subsequent starts of the application call Sage.dat for the information to start the application. Much of my problem lies in testing the content of sSOrig(1) to validate the assignment. Any attempt to use the subscripted variable causes the application to abort with no supporting information.

    As to the plot, I'm not convinced it is anywhere near as thick as you may think. What do you think this line does:
    sSOrig = SizeTask.SOrig
    Code:
    sSOrig(0) = SizeTask.SOrig(0)
    sSOrig(1) = SizeTask.SOrig(1)
    sSOrig(3) = SizeTask.SOrig(2)
    It holds a reference to an array. The array is a block of memory somewhere out on the heap, and sSOrig effectively holds the address of that memory. So, when you do that assignment, you replace one address with another, not one array with another, or the contents of one array with the contents of another array. You set up sSOrig to hold the address of one block of memory, then you switched it to hold the address of a different block of memory. What you did NOT do is copy the contents of one array into a different array.

    Having set the assignment to move SizeTask.SOrig to sSOrig, it isnít clear how I can access an element of the array such as sSOrig(1)? An uninformative abort isn't much value.

  12. #12
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    524

    Re: Public arrays across multiple forms

    This all sounds like you just need to take a few steps back and thoroughly debug your code. I would imagine that at some point, one or more of your variables isn't containing the values that you assume they are. Debugging will shed light on that.

    Good luck.

  13. #13
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,723

    Re: Public arrays across multiple forms

    It looks like you're creating a Structure, and you have a Sub as part of the structure to initialize the structure.
    But instead of having the sub initialize the structure elements, you have it initialize a local instance of the structure, which then goes away as soon as the Sub exits, so you've accomplished nothing.

    I don't know what you want to happen with your structures when you copy to "global" arrays. While the structures are value types, the arrays in the structures are reference types, so when you "copy" the array, you are just setting the global array to "point" to your structure's array. When you modify the global array, you are modifying your structures array.
    The global value is just a temporary alias for the array you set it to.

    I think you probably should just be using a class, rather than a structure, and you can easily assign a reference of that class to any instance you want. The normal way you would assign a reference to an instance would be by passing the instance as an argument to a method. Of course, if the methods were members of the class, then you wouldn't have to pass the instance, you would use the instance to access the method.

    If you wanted to have multiple forms access the instance, and didn't want to setup a way of passing the instance to the form, then you could have a global reference that the forms could use to work on a given instance. It isn't the best way to go about it, but it would be a lot better than copying structure members to parallel array objects. And if you want to work on the copy without affecting the original, then you have to do a real copy of the members of the array to another array (i.e. use Array.Copy method) and not assign one array instance to another reference.

    I don't know that debugging your current code is the best advice. I think some refactoring is in order. Define what data you have, where it comes from, and what you want to do with it, and where. If you give a good description of what you want to do, perhaps getting an approach to accomplish that will be of more use than trying to get a bad approach to work by a lot of convoluted manipulation.

    Just as an experiment, based on your current approach of using structures, and to see how copying the array reference to a global will effect the original array in the structure when you modify the global reference, I cut out most of the members of your structure to make a short test case.

    Code in the Module
    Code:
     Module SageData
        Public sSOrig(0 To 2) As Single          ' Working size data arrays
    
        Public Structure SizeRecord                 ' contains development size and complexity information
            Public SOrig() As Single       ' 0 To 2 reused source code (sloc)
    
            Sub UseStruct()
                ReDim SOrig(2)
            End Sub
        End Structure
    
    End Module
    Test Code in the form.
    Code:
    Public Class Form1
        Dim SizeStructs(5) As SizeRecord
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            For i As Integer = 0 To SizeStructs.Count - 1
                SizeStructs(i).UseStruct()  'initialize the size of the arrays in the structure
            Next
            For i As Integer = 0 To SizeStructs.Count - 1
                With SizeStructs(i)
                    .SOrig(0) = i
                    .SOrig(1) = i * 10
                    .SOrig(2) = i * 100
                End With
            Next
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            sSOrig = SizeStructs(2).SOrig
            sSOrig(1) += 1
            Debug.Print("sSOrig(1)= {0} and SizeStructs(2).SOrig(1) = {1}", sSOrig(1), SizeStructs(2).SOrig(1))
        End Sub
    End Class
    This code creates a local array of six of the sizeRecord structures.
    In the form_load event handler it calls the method in the structure to initialize the size of the array in the structure.
    In a second loop, it sets various values in the array elements of the different structures to help identify they are different elements of different structures.

    When you press the button, it copies the array reference from one of the structures to the global reference. It updates one of the elements in the global reference. It then prints out the current value of the modified element in the global reference, and it prints out the same element from the original structure. You should see that both values have changed, because they are not separate values, but are the same value in memory, just accessed by two different array references.

    I don't know if that is what you want, or not.

  14. #14
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,984

    Re: Public arrays across multiple forms

    Quote Originally Posted by passel View Post
    I think you probably should just be using a class, rather than a structure
    That's probably the crux of that matter. Class should be the default choice and structures should only be used when a structure is specifically called for.

  15. #15
    Fanatic Member
    Join Date
    Sep 2017
    Posts
    712

    Re: Public arrays across multiple forms

    I spend a bit of time in the "sandbox" practicing on making things work and learning new things. Usually in the past I would just pass an entire Form Class reference in to what ever Form Class I was creating. I keep reading it's wasteful and bad practice, etc, but exactly how wasteful? I dont know... like at what point does it actually make a difference, or is it just non-OCD compliant.
    At any rate, I had a thought that you could pass a Sub Class in as an argument just as easy as you can pass the entire form class in and reference only the bits of the Form Class that you want to use in the Target Class. I suppose it would be better practice than passing reference to the entire sha-bang, but certainly do not take my word for it. To me it looks like you can keep the Source Class objects private and not expose them to target class.

    Here is an example
    Source Form:
    Code:
    Public Class FormSource
        'Should be the first form that opens in your app
        'And last to close
        Dim MyArray1(2) As String
        Dim MyArray2(4) As String
    
        Public Class FormArgs
            'Reference specific objects from SourceForm here
            'Will be only objects passed to TargetForm
            Public _MyArray1() As String = FormSource.MyArray1
            Public _MyArray2() As String = FormSource.MyArray2
        End Class
    
        Private Sub ButtonShowFormTarget_Click(sender As Object, e As EventArgs) Handles ButtonShowFormTarget.Click
            Dim _FormArgs As New FormArgs
            Using _FormTarget As New FormTarget(_FormArgs)
                _FormTarget.ShowDialog()
            End Using
        End Sub
    End Class
    Target Form:
    Code:
    Public Class FormTarget
        Dim _FormArgs As FormSource.FormArgs
        Public Sub New(FormArgs As FormSource.FormArgs)
            InitializeComponent()
            _FormArgs = FormArgs
        End Sub
    
        Private Sub ButtonUseArgs_Click(sender As Object, e As EventArgs) Handles ButtonUseArgs.Click
            _FormArgs._MyArray1 = {"BlahBlah", "BlahBla"}
            _FormArgs._MyArray2 = {"BlahBlah", "BlahBla", "BlahBlah", "BlahBla"}
        End Sub
    
    End Class

  16. #16
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,723

    Re: Public arrays across multiple forms

    Since passing a "class" involves passing a reference to the class, the data passed is the same size whether the class contains a single byte, or 100 megabytes of nested hierarchies of classes.
    You could pass a million class references in the space of one screen capture.
    Most people don't give much consideration to working with multiple images (this is GUI development naturally), but feel that creating extra references to a class instance, or passing a class (reference) around is a lot of work. No, a picture (quite literally), can be worth a thousand class references. {and easily, millions of them actually}.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width