|
-
Jan 3rd, 2002, 09:11 AM
#1
Thread Starter
Fanatic Member
Function returning Array
I dont think that is posible..
Is there a work around?
Seahag
-
Jan 3rd, 2002, 09:13 AM
#2
Why not a disconnected / virtual ADO or DAO recordset ?
-
Jan 3rd, 2002, 09:18 AM
#3
Thread Starter
Fanatic Member
Hmmmm
Idea was to load from recordset into array .. than manipulate
as I desire.
Problem is I want to use class mods to handle all the calculating, and all the info returned is in groups of 6
??
-
Jan 3rd, 2002, 09:19 AM
#4
I've not used them really, but what about a collection object, would that maybe be better ?
-
Jan 3rd, 2002, 09:21 AM
#5
Thread Starter
Fanatic Member
I don`t know.
I think I am getting what your saying.
Use an object instead of Array>?
-
Jan 3rd, 2002, 09:31 AM
#6
The way a couple of the guys explained it to me - that the collection was similar to an array, although the collection is, yes, an object so it'd take more memory up.
You can't return an array from a function, but thinking about it you could just use a sub & declare the array outside of it (private or public) :
Code:
Option Explicit
Dim aryNumbers() As Integer
Private Sub Form_Load()
Dim i As Integer
For i = 0 To 11
ReDim Preserve aryNumbers(i)
aryNumbers(i) = i
Next i
End Sub
Private Sub Command1_Click()
Dim i As Integer, strTemp As String
For i = 0 To UBound(aryNumbers) - 1
strTemp = strTemp & aryNumbers(i) & vbCrLf
Next i
MsgBox strTemp
End Sub
-
Jan 3rd, 2002, 09:34 AM
#7
Thread Starter
Fanatic Member
I guess
I like your record set idea better. Makes more sense.
(I hate Variables.)
Thanks
Seahag
-
Jan 3rd, 2002, 09:36 AM
#8
Hyperactive Member
you can return and array....
[Highlight=VB]
Private Sub mySub()
Dim x
x = myFunction
msgbox x(0)
msgbox x(1)
msgbox x(2)
msgbox x(3)
End Sub
'---------------------------------------------------------------------------
Private Function myFunction()
Dim y() As String
ReDim y(3)
y(0) = "One"
y(1) = "Two"
y(2) = "Three"
y(3) = "Four"
myFunction = y
End Function
-
Jan 3rd, 2002, 09:42 AM
#9
True, if you left it as a variant, then you could.
-
Jan 3rd, 2002, 09:44 AM
#10
Hyperactive Member
Why don't you send it as a paramter ByRef
Private Sub ChangeArrayData(ByRef MyArray as String())
'The code
End Sub
-
Jan 3rd, 2002, 09:46 AM
#11
Thread Starter
Fanatic Member
Neet It worked
Don't know why it work?
But thanks..
Whats the ByRef thing do?
-
Jan 3rd, 2002, 09:52 AM
#12
Hyperactive Member
Not positive about the specifics but it means
By Reference as opposed to By Value.
It basically make a variable local to the function.
It's really useful with controls.
-
Jan 3rd, 2002, 09:57 AM
#13
Yep, well done there jcfowl, I didn't even think of that one!
This is your best option seahag. There are 2 ways of passing a variable.
1) ByRef or By Reference passes in the actual value, so you can manipulate the source straight away (like you're doing)
2) ByVal or By Value is a copy of the variable & has a fixed value which you cannot alter (bit like a constant).
-
Jan 3rd, 2002, 10:01 AM
#14
Thread Starter
Fanatic Member
OK
Bit confused.. could you show an example
using a Function? Pls
Thanks
-
Jan 3rd, 2002, 10:46 AM
#15
Hyperactive Member
VB Code:
Private Function ChangeData(p_MyVar as String) As String
ChangeData = p_MyVar & "I changed it"
End Function
Private Sub ChangeDataByRef(ByRef p_MyVar as String)
p_MyVar = p_MyVar & "I changed it"
End Sub
Private Sub Main()
Dim MyVariable, MyVariable2 as String
MyVariable = "Hello "
MyVaribale2 = "Hello "
MyVariable = ChangeData(MyVariable)
ChangeDataByRef(MyVariable2)
End Sub
Both MyVariable and MyVarible2 will equal the same thing.
"Hello I changed it"
-
Jan 3rd, 2002, 10:51 AM
#16
Beat me to it - sorry SeaHag, had a long lunch there (hic)! 
Private Sub Form_Load()
'set up variables
Dim x As String, y As String
x = "Hello World"
y = "Hello World"
'show the altered values returned from the function
MsgBox myfunction(x, y), , "Function values"
'now show the original values again
MsgBox "ByVal x variable = " & x & vbCrLf & vbCrLf & _
"ByRef y variable = " & y, , "Original Values"
Unload Me
Set Form1 = Nothing
End Sub
Private Function myfunction(Optional ByVal x As String, Optional ByRef y As String) As String
x = Replace(x, "l", "_CHANGED_")
y = Replace(y, "l", "_CHANGED_")
myfunction = "ByVal x variable = " & x & vbCrLf & vbCrLf & _
"ByRef y variable = " & y
End Function
-
Jan 3rd, 2002, 11:26 AM
#17
Thread Starter
Fanatic Member
cool thanks
Now time for My lunch
-
Jan 3rd, 2002, 12:00 PM
#18
Retired VBF Adm1nistrator
... u can return an array from a function ...
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Jan 3rd, 2002, 12:03 PM
#19
Ahhh, I thought we'd argued over this in the past, but couldn't find the post & thought I was dreaming, c'mon then where's the sample code to prove it ('cause I'm interested to know this too )!!!
-
Jan 3rd, 2002, 12:09 PM
#20
Retired VBF Adm1nistrator
yeah i was about to slap together some code, but ive to find a windows xp pro product key for the corporate edition like 10 minute ago ...
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Jan 3rd, 2002, 12:24 PM
#21
Retired VBF Adm1nistrator
VB Code:
Private Function returnArray() As Long()
Dim x() As Long, i As Long: ReDim x(9)
For i = 0 To 9
x(i) = i
Next
returnArray = x
End Function
Private Sub Form_Load()
Dim a() As Long, i As Long
a = returnArray
For i = 0 To UBound(a)
Debug.Print a(i)
Next
End Sub
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Jan 3rd, 2002, 12:29 PM
#22
Hyperactive Member
You can't do it with Fixed Length Arrays though
right?
Dim A(18) as Long
-
Jan 3rd, 2002, 01:23 PM
#23
Just pass it by reference then.
-
Jan 3rd, 2002, 01:30 PM
#24
Thread Starter
Fanatic Member
If i remember, I had trouble doing that.
Will it work through class mods (by using properties?)
-
Jan 3rd, 2002, 01:31 PM
#25
You can't do it with Fixed Length Arrays though
use Redim Preserve
-
Jan 4th, 2002, 03:20 AM
#26
Retired VBF Adm1nistrator
Um you can't ReDim Preserve a fixed array I don't think
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Jan 4th, 2002, 10:51 AM
#27
humm you have to declare you variable like : myArray()
and under it you just use Redim Preserve myArray(8)
I always do that because when it fixed you cant change it so with only 1 line you can change it like you want so I think it's better.
-
Jan 4th, 2002, 11:15 AM
#28
Just to clear this one up - You can do a redim preserve, but NOT on a fixed array.
- Dim arySample()
Is declaring a dynamic array - an open one which you can alter the dimensions of. - Dim arySample(5)
Is declaring a fixed - "you can't resize me I have 6 elements - bugger off" array.
-
Jan 4th, 2002, 12:54 PM
#29
yes that about what I just said
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|