Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 62
Default Question: Can't pass an Array to a class

Hello

I am struggling to pass an array to a class. Basically I defined the
class as follows:

Public xVar As Integer
Public Property Let AddtheStuff(TheStuff() As Integer)
Dim i As Integer
For i = LBound(TheStuff) To UBound(TheStuff)
xVar = xVar + TheStuff(i)
Next i
End Property


and I tested the array with the following procedu

Sub TestTheObject()
Dim MyObject As TheObject
Set MyObject = New TheObject
Dim ArrVar(1 To 10) As Integer
Dim i As Integer
For i = 1 To 10
ArrVar(i) = 10 * i
Next i
MyObject.AddtheStuff = ArrVar
MsgBox MyObject.xVar
End Sub

I always end up with the same compilation error: can't assign an
array. Am I doing something wrong?

Thanks in advance for your help
Charles

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Question: Can't pass an Array to a class

Hi Charles,

You can't assign an array to a Property. Instead of the property method pass
the array as an argument of a normal function. If you particularly want to
use the Property method try adapting something like this (untested)

change
Public Property Let AddtheStuff(TheStuff() As Integer)

to
Public Property Let AddtheStuff(TheStuff() As Integer, nValue as long)

change -
MyObject.AddtheStuff = ArrVar

to
MyObject.AddtheStuff(ArrVar) = 0 ' ie some value

In passing, there's no advantage to declaring As Integer vs As Long, quite
the contrary

Regards,
Peter T

"Charles" wrote in message
oups.com...
Hello

I am struggling to pass an array to a class. Basically I defined the
class as follows:

Public xVar As Integer
Public Property Let AddtheStuff(TheStuff() As Integer)
Dim i As Integer
For i = LBound(TheStuff) To UBound(TheStuff)
xVar = xVar + TheStuff(i)
Next i
End Property


and I tested the array with the following procedu

Sub TestTheObject()
Dim MyObject As TheObject
Set MyObject = New TheObject
Dim ArrVar(1 To 10) As Integer
Dim i As Integer
For i = 1 To 10
ArrVar(i) = 10 * i
Next i
MyObject.AddtheStuff = ArrVar
MsgBox MyObject.xVar
End Sub

I always end up with the same compilation error: can't assign an
array. Am I doing something wrong?

Thanks in advance for your help
Charles



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 62
Default Question: Can't pass an Array to a class

Thanks for the quick answer. I am actually confronted to another bug.
What I would like to do is to load a range of data from a spreadsheet
into the class. If I declare in the function AddtheStuff the argument
as an array of double, it doesn't seem to work. It works however if
the argument is defined as a variant, but only if I don't plug the
data directly from the spreadsheet, i.e.:

If I do this:

MyObject.AddtheStuff S.Range("thedata").value2

I get a bug, but if I do

Dim Grab() as variant
Grab=S.Range("thedata").value2
MyObject.AddtheStuff Grab

it works

I suspect it has to do with the ability to pass an argument by
reference, but since we can't pass an array by value. But do you see
away to spare the additional instruction?

Charles

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default Question: Can't pass an Array to a class

I sometimes put the array into a variant, and pass it that way.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"Peter T" <peter_t@discussions wrote in message
...
Hi Charles,

You can't assign an array to a Property. Instead of the property method
pass
the array as an argument of a normal function. If you particularly want to
use the Property method try adapting something like this (untested)

change
Public Property Let AddtheStuff(TheStuff() As Integer)

to
Public Property Let AddtheStuff(TheStuff() As Integer, nValue as long)

change -
MyObject.AddtheStuff = ArrVar

to
MyObject.AddtheStuff(ArrVar) = 0 ' ie some value

In passing, there's no advantage to declaring As Integer vs As Long, quite
the contrary

Regards,
Peter T

"Charles" wrote in message
oups.com...
Hello

I am struggling to pass an array to a class. Basically I defined the
class as follows:

Public xVar As Integer
Public Property Let AddtheStuff(TheStuff() As Integer)
Dim i As Integer
For i = LBound(TheStuff) To UBound(TheStuff)
xVar = xVar + TheStuff(i)
Next i
End Property


and I tested the array with the following procedu

Sub TestTheObject()
Dim MyObject As TheObject
Set MyObject = New TheObject
Dim ArrVar(1 To 10) As Integer
Dim i As Integer
For i = 1 To 10
ArrVar(i) = 10 * i
Next i
MyObject.AddtheStuff = ArrVar
MsgBox MyObject.xVar
End Sub

I always end up with the same compilation error: can't assign an
array. Am I doing something wrong?

Thanks in advance for your help
Charles





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 62
Default Question: Can't pass an Array to a class

Actually found the answer myself. I was declaring Addthestuff(x() as
variant) instead of Assthestuff(x as variant) which works. Still don't
know why but the code behaves normally

Charles



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Question: Can't pass an Array to a class

You are now using exactly what Jon suggested as an alternative, but note he
said "into a variant" which is not the same as declaring a variant array.

Regards,
Peter T

"Charles" wrote in message
oups.com...
Actually found the answer myself. I was declaring Addthestuff(x() as
variant) instead of Assthestuff(x as variant) which works. Still don't
know why but the code behaves normally

Charles



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
combining cells and array from different sheets into an array to pass to IRR() [email protected] Excel Discussion (Misc queries) 3 September 11th 06 07:17 AM
Pass an array to Rank Biff Excel Worksheet Functions 12 June 29th 05 04:15 PM
How do I pass an array to a listbox? Titus A Ducksass - AKA broken-record Excel Programming 4 March 23rd 05 07:20 PM
How can I pass an array as TextToDisplay to a hyperlink? Jay Fincannon Excel Programming 4 January 28th 05 01:45 AM
Pass a variable from a class module pk Excel Programming 1 October 2nd 03 08:24 PM


All times are GMT +1. The time now is 12:20 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"