Link to home
Start Free TrialLog in
Avatar of SubSonicS
SubSonicS

asked on

Array come back from a Function --- 300 Points

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



Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of toddhd
toddhd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
Avatar of SubSonicS
SubSonicS

ASKER

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...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> 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
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
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!

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.... :)
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I agree with toddhds last post and if you look at my first post, this is exactly what I have done.

~IM
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..
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...

Ops, i accepted only Tohhd....mmmmm....
I cannot distribute points? I cannot select also Assisted Answer?
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?
You can post a request to the question reopened from in the Support TA:
https://www.experts-exchange.com/Community_Support/

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

~IM
Hi, i asked to be reopened...

P.S. Thanks Again...
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...