Use collection to store class objects

KDEBR

New Member
Joined
Aug 2, 2018
Messages
8
Hello
I want to use a collection that contains different car objects.
I'm familiar to do this in Java but I don't find the solution in VBA.

I want to create a class car where I can create my car object with contains the following parameters
make
model
year
therefore I have to make a class module?

I want to a list that I have in excel with the actual makes and models.

How can I store these objects in a collection?
I declare the collection in the module section?

Thanks for you feedback!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Thanks for the feedback but I'm still facing diffficulties.
I've tried to do this in C# and there I would use the code below.

I have the main class to run the program. I also have a vehicle class to make vehicle objects and those vehicle objects I'll keep the in a collection list class overview.
How could I do this in Excel?

class Program
{
static void Main(string[] args)
{
Overview lijst = new Overview();

lijst.add(new Vehicle("Audi","A3"));
lijst.add(new Vehicle("BMW", "1"));
Console.ReadLine();
}
}


class Vehicle
{
private string merk;
private string model;



public Vehicle(string merk, string model)
{
this.merk = merk;
this.model = model;


}
public string getMerk()
{
return merk;
}


public string getModel()
{
return model;
}
}


class Overview
{
private List<Vehicle> lijst;


public Overview()
{
lijst = new List<Vehicle>();
}


public void add(Vehicle vehicle)
{
lijst.Add(vehicle);
}
}
 
Upvote 0
The link above has examples of exactly what you are trying to do. Where are you stuck? - Post your VBA code, not your C#
 
Last edited:
Upvote 0
I do not have much experience using Classes, but this worked for me

Class Module
Vehicle
Code:
Option Explicit

Private strMaker As String
Private strModel As String
Private lngYear As Long

Public Property Get Maker() As String
    Maker = strMaker
End Property

Public Property Let Maker(ByVal sMaker As String)
    strMaker = sMaker
End Property

Public Property Get Model() As String
    Model = strModel
End Property

Public Property Let Model(ByVal sModel As String)
    strModel = sModel
End Property

Public Property Get Year() As String
    Year = lngYear
End Property

Public Property Let Year(ByVal lYear As String)
    lngYear = lYear
End Property

Code in a standard module
Code:
Sub Main()
    Dim oVeh As Vehicle, myCol As Collection
    Dim V As Vehicle
        
    Set myCol = New Collection
    
    Set oVeh = New Vehicle
    oVeh.Maker = "Audi"
    oVeh.Model = "A3"
    oVeh.Year = 2017
    myCol.Add oVeh
    
    Set oVeh = New Vehicle
    oVeh.Maker = "BMW"
    oVeh.Model = "1"
    oVeh.Year = 2018
    myCol.Add oVeh
    
    For Each V In myCol
        Debug.Print "Maker= " & V.Maker
        Debug.Print "Model= " & V.Model
        Debug.Print "Year= " & V.Year
    Next V
End Sub

Hope this helps

M.
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,986
Members
448,538
Latest member
alex78

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top