Use of a CLASS for vba - General opinion sought?

jxb

Board Regular
Joined
Apr 19, 2007
Messages
172
Office Version
  1. 2010
Platform
  1. Windows
This is a bit of an intellectual exercise. I am trying to gain more knowledge on the use of CLASS.
I think I understand the principle, have read bits-and-bobs on their use but have never have real use for them. I think I created my own CLASS for one program
This is mainly because I do simple coding for my own use.

I am looking at an old vba programme which parse data from a file, data is extracted and value put in excel – Standard stuff
The simple workflow is

Read the whole file
Split the file content into blocks at a specific “header”
Process each block
Split the block into lines
Process each line
If the line contains specific string then split line , extract data

It works just fine (speed is not an issue as the file is not very large)
A typical block data is shown at the end of thread. The 1st line is always the dashed line as it is the “header” used to split the file into blocks

The question

Could a CLASS be used to get the data wanted? For example

VBA Code:
Dim Cdb As ClassDatablock

Set Cdb = New ClassDatablock
'And in the main code

CG = Cdb.GettheCG()
'And so on for each “attribute” wanted



The example would return an array (3 values) containing the data from the line after the containing the keyword ‘ INERTIA at CENTER OF GRAVITY with respect to _YYYYYY’

This is what the current code does for a given block

VBA Code:
For i = 0 to lastline
Check if line contains keyword
If true then select next line (i+1)
  Split line at the space
  Get field (3,4,5)


Example of a block data - The 1st line is always the dashed line as it is the “header” used to split the file into blocks

---------------------------------------------
MASS PROPERTIES FOR XXXXX
VOLUME = 3.0237829e+01
SURFACE AREA = 9.8488930e+01
DENSITY = 2.7000000e-03
MASS = 8.1642139e-02

CENTER OF GRAVITY with respect to _ XXXXX coordinate frame:
X Y Z 0.0000000e+00 2.5000000e+00 0.0000000e+00

INERTIA at CENTER OF GRAVITY with respect to _ XXXXX coordinate frame
INERTIA TENSOR:

Ixx Ixy Ixz 2.5226579e-01 0.0000000e+00 0.0000000e+00
Iyx Iyy Iyz 0.0000000e+00 1.6435583e-01 0.0000000e+00
Izx Izy Izz 0.0000000e+00 0.0000000e+00 2.5226562e-01

PRINCIPAL MOMENTS OF INERTIA
I1 I2 I3 1.6435583e-01 2.5226535e-01 2.5226606e-01

CENTER OF GRAVITY with respect to _YYYYYY coordinate frame:
X Y Z -8.5350000e+01 2.1200000e+02 -9.0750000e+01

INERTIA at CENTER OF GRAVITY with respect to _YYYYYY coordinate frame
INERTIA TENSOR:
Ixx Ixy Ixz 1.6435583e-04 0.0000000e+00 0.0000000e+00
Iyx Iyy Iyz 0.0000000e+00 2.5226579e-04 0.0000000e+00
Izx Izy Izz 0.0000000e+00 0.0000000e+00 2.5226562e-04
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
A class can do anything that you can do without one. The only question is whether you actually need/benefit from one.
 
Upvote 0
Ok. Let's assume that there is a benefit in this case.
jhow does one would go about creating a class to get a specific attribute in the block?
 
Upvote 0
The actual process code isn't really going to be any different, it's just going to be a method/property of the class. There isn't really enough information to say what the best approach is, but you will probably want a generic function to find the data section you want in the block based on the attribute name, then you can either call that from specific properties (so the function code would be private) or just expose the function as a GetAttribute method.
 
Upvote 0

Forum statistics

Threads
1,224,746
Messages
6,180,705
Members
452,994
Latest member
Janick

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