Solved

Array come back from a Function --- 300 Points

Posted on 2004-10-28
207 Views
Last Modified: 2010-04-24
Hi, my question should be easy or impossible, i don't know..

Let me explain..
I'm working on a Control...my custom control....(Like OCX)...to implement it on Vb.Net standard EXE project..

Into my control i declared a structure and a Array like this:

Structure structSomething
   dim Variable1 as string
   dim Variable2 as string 'and so on...
End Structure
Friend myArrayVariable(100) as structSomething

I also have a Function to fill the array like this:

Friend Function FullMyArrayVariable()
   myArrayVariable(1).variable1 = "Something"
   myArrayVariable(1).variable2 = "SomethingOther"
   'And so on...
End Function


Ok, now as you already know i can access myArrayVariable(100) within anywhere my control...

I want now to use this my control (Dll) into a standard Vb.Net project...
I need to pass myArrayVariable to a similar one to the standard EXE Project...

Naturally i declared a similar Variable into a Module:

Structure structSomething
   dim Variable1 as string
   dim Variable2 as string 'and so on...
End Structure
Friend myArrayVariable(100) as structSomething

How can i pass the myArrayVariable from the control to the project?

I tried to call a function from within my control like this:

Public Function PassData() as myArrayVariable
   Return myArrayVariable
End Function
But i cannot do this because i cannot call a function and decleare it as an Array...

Then i tried to do this:
Public Function PassData()
   Return myArrayVariable
End Function

And from within my project do somithing like this:

myArrayVariable = myControl.PassData

But i cannot do this because i receive an error...


Another way to do all this should be to use a Property, i already know, but i want to access myArrayVariable only at runtime....(After a developer request it by a function)....

I'm Italian, then i don't know if you should understand my bad English language...Sorry..

Then this is a little summing up:
How i can pass an array(100) declared with a structure from a Control to a Form?

Please if you don't understand something then please let me know...i will try to explain it better...

Really thanks for any type of Help! ;)

Stefano



0
Question by:SubSonicS
    19 Comments
     
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Unless I'm missing something, you should be able to declare a property that returns an array.  Here is my control code:

    Public Class myUserControl
        Inherits System.Windows.Forms.UserControl

        Public Structure myStructure
            Public dataA As String
            Public dataB As String
            Public dataC As String
        End Structure

        Friend myStructArray(100) As myStructure

        Public ReadOnly Property StructArray() As myStructure()
            Get
                Return myStructArray
            End Get
        End Property

        Private Sub myUserControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim i As Integer

            For i = 0 To myStructArray.GetUpperBound(0)
                myStructArray(i).dataA = "a" & i
                myStructArray(i).dataB = "b" & i
                myStructArray(i).dataC = "c" & i
            Next
        End Sub
    End Class

    And here is a form that makes a copy of the array in the control using the exposed StructArray() property:

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private myStructArray() As myUserControl.myStructure

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer

            mystructarray = MyUserControl1.StructArray
            For i = 0 To mystructarray.GetUpperBound(0)
                Debug.WriteLine(mystructarray(i).dataA & vbTab & mystructarray(i).dataB & vbTab & mystructarray(i).dataC)
            Next
        End Sub
    End Class
    0
     
    LVL 8

    Accepted Solution

    by:
    Of course you can return an array, you just didn't declare it properly. Try this:

        Public Function PassData() As structSomething()
            Return myArrayVariable
        End Function
    0
     
    LVL 5

    Expert Comment

    by:Ignacio Soler Garcia
    How to move arrays into parameters and returns? Just like any other type:

    Public Function CopyByteArray(arraySource() as Byte) As Byte()
        Dim result() as Byte
        Dim index as Integer

        Redim result(arraySource.getUpperBound(0)

        For index = 0 To arraySource.getUpperBound(0)
             result(index) = arraySource(index)
        Next

        Return result
    End Function

    A little example that shows how the system works ...

    Just tell me if you need more!
    0
     

    Author Comment

    by:SubSonicS
    Thanks to all..BUT...

    Idle_Mind: Thanks for this, i will try it soon, but i need before to search a way to do it without Property....

    Todhd: Thanks for this, but i'm able to pass only a single matrix Array like myArray(1) and so on.. I need to pass a complete full Array with all the matrix, like simple myArray()

    SoMos: It does not work for me...

    Please, let me explain more:

    ok, this is my real code on the UserControl:

        Structure strDevices
            Dim CompleteString As String
            Dim BusId As String
            Dim Type As String
            Dim Revision As String
        End Structure
        Friend Devices(100) As strDevices

    The UserControl now is able to fill the Devices(100) with a function named ScanBus..
    At the end of ScanBus i could have a variable like this:
    Devices(1).CompleteString = "Liteon....."
    Devices(1).BusId = "1:1:0"
    Devices(1).Type = "DVD-RW"
    Devices(1).Revision = "1.0"
    Devices(2).CompleteString = "Pioneer....."
    Devices(2).BusId = "1:2:0"
    and so on.....

    Now the user of my UserControl will need to fill a similar variable like Devices(100) inside its application...

    Naturally i'm able to create a simple function like this:
        Public Function WolverineRecDev_GetDevices()
            ScanBus()
            Return Devices(1).CompleteString
        End Function
    And the user should be able to create this:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Devices(1).CompleteString = WolverineRecDev1.WolverineRecDev_GetDevices()
            MsgBox(Devices(1).CompleteString)
        End Sub

    OK, this work with only Devices(1) (Only one matrix) and most important with only one property (in this case the .CompleteString)

    My real need is to pass the entire Devices() in one time......

    I hope this is more clear for you....

    350 Points now...
    0
     
    LVL 5

    Assisted Solution

    by:Ignacio Soler Garcia
    Why don't you try:

        Public Function WolverineRecDev_GetDevices() As strDevices()
            ScanBus()
            Return Devices
        End Function

    Then the user can do:

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim auxiliarDevices() as strDevices ' or use a class attribute if the user has it instead of method variable!!!

            auxiliarDevices = WolverineRecDev1.WolverineRecDev_GetDevices()
            MsgBox(auxiliarDevices(1).CompleteString)
        End Sub

    What I don't see well is why your user control has to fill one array of strDevices and the user has to fill another one. They will not have the same content? If they have you only need to store its reference inside the user program, not need to fill another array, of course!
    0
     
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    >> Idle_Mind: Thanks for this, i will try it soon, but i need before to search a way to do it without Property....

    A function and a property are essentially the same thing.  As others and I have already pointed out, you can return an entire array.  Out of curiosity, why can't you use a property?

    Change my property to a function:

        Public Function StructArray() As myStructure()
            Return myStructArray
        End Property

    Then declare your array and call the function:

        Private myStructArray() As myUserControl.myStructure
        myStructArray = = MyUserControl1.StructArray

    ~IM
    0
     

    Author Comment

    by:SubSonicS
    I SoMos,
    if i do this i get this error:
    Unable to convert type "1 dimension matrix WolverineRecDev.modVariables.strDevices." in "WolverineRecDev.WolverineRecDev.strDevices".

    I think of if i declare a function as a structure, maybe i cannot Return back a simple 1 dimension array...

    >>>>What I don't see well is why your user control has to fill one array of strDevices and the >>>>user has to fill another one. They will not have the same content? If they have you only >>>>need to store its reference inside the user program, not need to fill another array, of >>>>course!

    Well, in my User Control i naturally use Modules...
    my Devices(100) array is declared within a module, elsewhere if i declare in on the User Control i cannot access it from modules....then....i cannot use it from a Form because it only can see Public Variables declared into the Main User Control and not into the modules....

    Please copy and paste this code into a standard Exe Project:

        Structure myStructure
            Dim String1 As String
            Dim String2 As String
        End Structure
        Friend myVariable(100) As myStructure

        Public Function GetVariables() As myStructure
            'Assign something like my ScanBus do...
            myVariable(1).String1 = "Hi"
            myVariable(1).String2 = "Stefano"
            'Now try to return back the Array....
            Return myVariable
        End Function

    Like me you will get an error on myVariable....
    Really thanks for your time.... :)

    Message for IDLE_MIND: Thanks, now i will try your code...
    Please wait a while for the reply...

    P.S. i'm starting to think to really use a property....eheh
    0
     

    Author Comment

    by:SubSonicS
    Another strange....
    From within a single project, i'm able to declare two Arrays as the same structure and copy between they like this:

        Structure myStructure
            Dim String1 As String
            Dim String2 As String
        End Structure

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim myFirstVariable() As myStructure
            Dim mySecondVariable() As myStructure

            'Assign some values:
            myFirstVariable(1).String1 = "Hi"
            myFirstVariable(1).String2 = "Stefano"

            'Now i can copy one to another:
            mySecondVariable = myFirstVariable
            'And/Or viceversa:
           myFirstVariable = mySecondVariable

            MsgBox(mySecondVariable(1).String2)
        End Sub

    If i can do this within the same project, why i cannot copy myArray from the user control to a similar Array to the Form?

    lol: I'm not a real programmer, i know!

    0
     

    Author Comment

    by:SubSonicS
    IDLE_MIND

    I think your code is the same as SoMos
    and this reply it is also for you:

        Structure myStructure
            Dim String1 As String
            Dim String2 As String
        End Structure
        Friend myVariable(100) As myStructure

        Public Function GetVariables() As myStructure
            'Assign something like my ScanBus do...
            myVariable(1).String1 = "Hi"
            myVariable(1).String2 = "Stefano"
            'Now try to return back the Array....
            Return myVariable
        End Function

    Like me you will get an error on myVariable....
    Really thanks for your time.... :)
    0
     
    LVL 8

    Expert Comment

    by:toddhd
    When you declare the structure in your project, are you re-creating it? In other words do you have this:

     Structure myStructure
            Dim String1 As String
            Dim String2 As String
        End Structure

    In both the class and the calling project?

    My suggestion would be to make the structure in the class public, like this:

     Public Structure myStructure
            Dim String1 As String
            Dim String2 As String
        End Structure

    And then in the calling project, assign variables like this:

    MyVariable(100) as myClass.myStructure
    0
     
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    This was your last post:
    --------------------------------------------------------------------------------------------------------
    IDLE_MIND

    I think your code is the same as SoMos
    and this reply it is also for you:

        Structure myStructure
            Dim String1 As String
            Dim String2 As String
        End Structure
        Friend myVariable(100) As myStructure

        Public Function GetVariables() As myStructure
            'Assign something like my ScanBus do...
            myVariable(1).String1 = "Hi"
            myVariable(1).String2 = "Stefano"
            'Now try to return back the Array....
            Return myVariable
        End Function

    Like me you will get an error on myVariable....
    Really thanks for your time.... :)
    --------------------------------------------------------------------------------------------------------

    Of course you are going to get an error.  You have defined your GetVariables() function to return on only one instance of your myStructure but you are returning an entire array.

    Change
       
        Public Function GetVariables() As myStructure

    to
     
        Public Function GetVariables() As myStructure()

    Notice the parenthesis on the end of myStructure indicating that an array is being returned.  If you look closely at the posts from myself, toddhd and SoMos, we are all returning arrays, not simple structures.

    ~IM
    0
     
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    I agree with toddhds last post and if you look at my first post, this is exactly what I have done.

    ~IM
    0
     

    Author Comment

    by:SubSonicS
    Hello friends,
    thank to you all i think of i'm now able to proceed...

    Please give me a little time to try the best way for my app and i will give you my points...

    Really thanks for your time...
    Hope to meet you all again..
    0
     

    Author Comment

    by:SubSonicS
    WELL,

    it is all OK!!! Thanks!

    This is my SIMPLE! code:

    On the User Control:

        Public Structure structureDevices
            Dim CompleteString As String
            Dim BusId As String
            Dim Type As String
            Dim Revision As String
        End Structure
        Public Devices(100) As structureDevices

        Public Function WolverineRecDev_GetDevices() As structureDevices()
            ScanBus()
            Return Devices
        End Function

    And on the Form:

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim myDevices() As WolverineRecDev.WolverineRecDev.structureDevices
            myDevices = WolverineRecDev1.WolverineRecDev_GetDevices()
            MsgBox(myDevices(1).CompleteString)
            MsgBox(myDevices.Length)
        End Sub

    THANKS!

    I used also MsgBox(myDevices.Length) because i was able to redimensionate the array after filled with only the necessary devices preserving all the data:
    Redim Preserve Devices(NewValue)


    Really Thanks to all...
    A great See You by Wolverine from Italy!

    Well, for my Points...
    All you are the winners...
    Then i will distribute it to you all, Ok?

    Thanks a lot...

    0
     

    Author Comment

    by:SubSonicS
    Ops, i accepted only Tohhd....mmmmm....
    I cannot distribute points? I cannot select also Assisted Answer?
    0
     
    LVL 8

    Expert Comment

    by:toddhd
    Who else should have gotten points? Maybe the admins can help? Or maybe I can open a bogus question myself and reward the points to the other users?
    0
     
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    You can post a request to the question reopened from in the Support TA:
    http://www.experts-exchange.com/Community_Support/

    Then you can click on "Split Points" at the bottom of the page once it is reopened.

    ~IM
    0
     

    Author Comment

    by:SubSonicS
    Hi, i asked to be reopened...

    P.S. Thanks Again...
    0
     

    Author Comment

    by:SubSonicS
    LOL, thanks to moderator to re-open...

    Accepted the Answer of todhd because it was the first to get my bug...

    118 Points to Idle_Mind because it follow me evrywhere! (Joke! only because it has given more then 1 solution...)

    Thanks to the posters...

    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    The Complete Ruby on Rails Developer Course

    Ruby on Rails is one of the most popular web development frameworks, and a useful tool used by both startups and more established companies to build strong graphic user interfaces, and responsive websites and apps.

    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    3 Experts available now in Live!

    Get 1:1 Help Now