473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Creating a class with a variable which is an array of objects from an other class.

15
Hi there,

I have created a class called Pair which has 2 variable, char1 and char2, a bit like a 2d co-ordinate, (2,4),(1,8), etc... This seems to work ok. Heres my code:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private pChar1 As Integer
  4. Private pChar2 As Integer
  5.  
  6. Public Property Get Char1() As Integer
  7.     Char1 = pChar1
  8. End Property
  9.  
  10. Public Property Let Char1(Value As Integer)
  11.     pChar1 = Value
  12. End Property
  13.  
  14. Public Property Get Char2() As Integer
  15.     Char2 = pChar2
  16. End Property
  17.  
  18. Public Property Let Char2(Value As Integer)
  19.     pChar2 = Value
  20. End Property
  21.  
I tried to create another class PairArray whose variable is an array of an unspecified number of Pair class objects. I.e. a sequence of co-ordinates, like [(1,3),(6,8),(2,5),...]. This is the code for that class:

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private pPairArray() As cPair
  4.  
  5. Public Property Get PairArray(i As Integer) As String
  6.    If i >= LBound(pPairArray) And i <= UBound(pPairArray) Then
  7.       PairArray = pPairArray(i)
  8.    End If
  9. End Property
  10.  
  11. Public Property Set PairArray(i As Integer, p As cPair)
  12.    If i >= LBound(pPairArray) And i <= UBound(pPairArray) Then
  13.       pPairArray(i) = p
  14.    End If
  15. End Property
  16.  
  17. Public Function Resize(i As Integer)
  18.    ReDim pPairArray(1 To i)
  19. End Function
  20.  
I also created a Resize function to change the array size.

I can create a PairArray object, and call the Resize function ok, but get a 'can't assign to a read only property' error when assigning a Pair to the PairArray using the following function:

Expand|Select|Wrap|Line Numbers
  1. Public Function PairArrayTest()
  2.  
  3. Dim pa As cPairArray
  4. Set pa = New cPairArray
  5.  
  6. Dim p As cPair
  7. Set p = New cPair
  8.  
  9. p.Char1 = 1
  10. p.Char2 = 0
  11.  
  12.  
  13. pa.Resize (10)
  14. pa.PairArray(1) = p
  15.  
  16.  
  17. End Function
  18.  
Please can anyone shed some light on this?
Sep 3 '10 #1
2 3942
FishVal
2,653 Expert 2GB
Just a thought.
"PairArray" property is declared to return String and to obtain cPair. This could make VBA think that Get and Set procedures don't belong to the same property.
Sep 4 '10 #2
ADezii
8,834 Expert 8TB
How about using an Array of a User Defined Type to store the individual Pairings?
Expand|Select|Wrap|Line Numbers
  1. Public Type CharType
  2.   Char1 As Integer
  3.   Char2 As Integer
  4. End Type
  5.  
  6. 'Hold 25 pairiings of Char1 and Char2
  7. Public aCharTypes(1 To 50) As CharType
Expand|Select|Wrap|Line Numbers
  1. Dim intCtr As Integer
  2. Dim strBuild As String
  3.  
  4. For intCtr = 1 To UBound(aCharTypes)            '25 Pairs of User Defined Type CharType
  5.   If intCtr Mod 2 <> 0 Then
  6.     aCharTypes(intCtr).Char1 = 50 * intCtr      'Char1 in Odd Indexes
  7.   Else
  8.     aCharTypes(intCtr).Char2 = 100 * intCtr     'Char2 in Even Indexes
  9.   End If
  10. Next
  11.  
  12. 'Play Back
  13. For intCtr = 1 To UBound(aCharTypes)
  14.   If intCtr Mod 2 <> 0 Then
  15.       strBuild = strBuild & "(" & aCharTypes(intCtr).Char1 & ","
  16.   Else
  17.       strBuild = strBuild & aCharTypes(intCtr).Char2 & "),"
  18.   End If
  19. Next
  20.  
  21. 'All Pairings
  22. Debug.Print Left$(strBuild, Len(strBuild) - 1)  'Remove Trailing ','
OUTPUT:
Expand|Select|Wrap|Line Numbers
  1. (50,200),(150,400),(250,600),(350,800),(450,1000),(550,1200),(650,1400),(750,1600),(850,1800),(950,2000),(1050,2200),(1150,2400),(1250,2600),(1350,2800),(1450,3000),(1550,3200),(1650,3400),(1750,3600),(1850,3800),(1950,4000),(2050,4200),(2150,4400),(2250,4600),(2350,4800),(2450,5000)
Sep 4 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: John Black | last post by:
Hi, Suppose I have a class, class MyClass{ public: MyClass(); MyClass(MyClass); int func(); };
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
2
by: Clinton Daniel | last post by:
I have an array like this: MyClass myArray; In a method I allocate it depending on how many rows in the DataTable: myArray = new MyClass[myDataTable.Rows.Count However, when I try to...
10
by: Steve | last post by:
this code: private Array m_arrays = new Array; results in an array of 3 null Array objects. What am I missing?
15
by: tatsudoshi | last post by:
Hello, I have this class http://pastebin.com/807571, where I set some variables on __construct. Originaly I set the $total_? variables when the function showLayout() was called. I know pastebin...
3
by: raylopez99 | last post by:
Below is my problem. I've narrowed it down to one thing: my unfamiliarity on how class instances are instantiated in an array. This is because the "un-array" / "non-array" version of the program...
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
2
by: alefajnie | last post by:
class A: this_is_original_variable_only_for_one_inctance = 0 def __init__(self, v): self.this_is_original_variable_only_for_one_inctance = v class B: this_is_common_for_all_instances =
7
by: Yen Kwoon | last post by:
Note: This problem is related to gcc but after some back and forth in group gnu.gcc.help it seems to have morph into more of a c++ specificiation question, hence the transplanting to this group. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.