Process a string to extract number for a Class property?

jxb

Board Regular
Joined
Apr 19, 2007
Messages
172
Office Version
  1. 2010
Platform
  1. Windows
I am playing around with setting up a class to process block of (string) data. Just a test
I would like to have a property (called TheMass) to return a number ie the mass ! which is extracted from a line of data (a string)

I set up a class (Module) as follows

VBA Code:
Dim pthemass As Double

Public Property Get TheMass() As Double
    TheMass = pthemass
End Property

Public Property Let TheMass(theline As String)
    Dim linedata() As String
    linedata() = Split(theline, "=")
    pthemass = CDbl(linedata(1))
End Property

and in "standard" module

VBA Code:
Sub TestDataBlockClass()
    Dim TheDataBlock As TheDataBlock
    Set TheDataBlock = New TheDataBlock
    
    thestring = "MASS = 999"
    
    TheDataBlock.TheMass = thestring
        Debug.Print "3: " & TheDataBlock.TheMass
End Sub

I want to pass the variable call thestring to the property TheMass() which should extract the desired number from the supplied line
This not working (not too surprised as this is my 1st foray in Class). This is just a test as I want to set up other properties on the same concept (pass a block of data to extract specific numbers)

What is wrong with the code?
Thanks
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi. I'm no expert with Class modules, but I tinkered with your code - the difficulty appears to be that you are LET the property, TheMass, as a string, but GET TheMass as a double. I kept getting compile errors because of the different data types, so I changed them to Variant[ (not ideal, I know), and it appears to work. The class code has been slightly adjusted as below:

VBA Code:
Dim pthemass As Double

Public Property Get TheMass() As Variant
    TheMass = pthemass
End Property

Public Property Let TheMass(theline As Variant)
    Dim linedata() As String
    linedata() = Split(theline, "=")
    pthemass = CDbl(linedata(1))
End Property
 
Upvote 0
I am no expert in Classes, but this works. Name the Class module TheDataBlock and insert this code:
VBA Code:
Option Explicit

Private ptheString As String
Private pTheMass As Double

Property Get TheMass() As Double
    TheMass = pTheMass
End Property

Property Let TheString(theline As String)
    ptheString = theline
    Dim linedata() As String
    linedata() = Split(theline, "=")
    pTheMass = CDbl(linedata(1))
End Property
In this way you only insert one line of text and you can extract multiple properties.
Then insert this in the standard module to test:
VBA Code:
Option Explicit

Sub TestDataBlockClass()
    Dim TheString As String
    Dim TheBlock As New TheDataBlock
    
    TheString = "MASS = 999"
    TheBlock.TheString = TheString
    Debug.Print "3: " & TheBlock.TheMass
End Sub
make sure not to use variable names matching the class name.
 
Upvote 0
Thanks. The reply from @Dan_W works but then started experimenting and you replied way quicker than my brain can think of. Will test right now
 
Upvote 0
looks like it's working. Thanks. Instead of a line might be able to pass en entire block of data with a keyword to extract specific value :)
something like that

VBA Code:
Property Let TheString(sDataBlock As String,skeyword)
   asDataBlockLine = Split(sDataBlock , vbCrLf)
  if skeyword = "mass" then ...
  if skeyword = "anotherkeyword" then ...
  'and so on
End Property
and depending on the keyword I could allocate the value to the appropriate class property
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,317
Members
449,081
Latest member
tanurai

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