Visual Basic Classic
--
Questions
--
Followers
Top Experts
Having trouble doing this.
Thanks
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Thanks
On "Dim Box as Box" received error
"By user defined object type not defined"
Changed it to "object"
After that "type don't match on "colBox.Add oBox, 1"
1) Do you have an object named Box or a class named Box? If not you need to change the word Box to your object or class. If you are using a "Box" object from somebody else, you need to specify what the type of that object is. The code given is generic in nature. I could have used "Giraffe" instead of "Box" as long as my code can draft up a giraffe object"
2) Since you changed the dim oBox as object, you then drafted up an object that is set to nothing when you hit the = new Box code (related to problem #1). You can't add a variable that is set to nothing to the collection.
Here are the typical procedures for drafting up an object
1) Create a class that represents that object.
Using your Box example, I would create a class called clsBox.
clsBox
Property Width
Property Height
Property Color
Property HasRoundedCorners
Property Is3D
2) Somebody may have already built a Box class for you that you'd like to use (say perhaps Excel has a box object called ExcelBox that needs your needs). In this case, you don't need #1 above.
3) You draft up a box object by dimensioning a variable as that type of object. In case #1 you would use Dim oBox as clsBox. In case #2 you would use dim oBox as Excel.ExcelBox.
4) You instantiate a new box object by performing the following: In case #1 you would do set oBox = new clsBox. In case #2 you would use set oBox = new Excel.ExcelBox (note Excel doesn't have an ExcelBox object as far as I know, it is just an example).
The collection is separate from these instructions. It simply can take several object of the same type or different types and put them in a common holding area as long as the opbject is set to something (i.e. oBox not is nothing).






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
Sorry I look a little bit stupid here, it has been a very long while, programming.
To explain you what I want to do is.
I have several of those retangles. They change from red to green and back due to occupied or open.
instead of cycle through every single one of them. I like to call them from the collection, this saves a lot of input, just by givving those objects only one name with an index. The boxes (retangles) are called "bxbaan1" to "bxbaan8". So as you wrote the object is already created.
I did put a watch on it. The oBox is filled with "bxbaan1" caused by the set command.
After this it stops by the row where the oBox needs to be added to the collection.
Here is where the "types do not match" comes up, before it can go to add the second one.
Sorry to bother you, I appreciate your help.
Set colBoxes = New VBA.Collection ' put this in general declarations of the module so you can access it anywhere in that module
Dim oBox As Rectangle
Dim colBoxes As Collection
Set oBox = Box27
colBoxes.Add oBox, oBox.Name
Set oBox = Box28
colBoxes.Add oBox, oBox.Name
MsgBox colBoxes(1).Name
MsgBox colBoxes(2).Name
Then to get a box out, you can use set oBox = colBoxes("Box27"), then set oBox.backcolor = vbred, etc.
Visual Basic Classic
--
Questions
--
Followers
Top Experts
Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.