ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Get the Data Type of a Property (https://www.excelbanter.com/excel-programming/397343-get-data-type-property.html)

DrRocket

Get the Data Type of a Property
 
Hi,
I'm building a microsoft excel program that reads through the
information in a text and creates a report off of it. I'm doing this
by building a class which I refer to as a CRecord as well as a
CRecords class which is just the collection of all the CRecords.
Obviously the all of the properties of the CRecord class aren't of the
same data type which has made me run into the problem that unless I
explicitly specify the data type when parsing the data and filling in
the properties of the CRecord I will sometimes get a type mismatch
error. What I would really like to do be able to somehow determine
the data type of the property that is being written to and have my
function automatically convert the data to that data type. Is that
possible? Here is my code:

Here is the Calling Procedu

....

Set gclsRecord = New CRecord

sSettlementDate = ImportData(6, 6)
sTradeDate = ImportData(3, 6)

With gclsRecord
.RecordType = clbRecordTypeData
.Record = CStr(lCurrentRow)

.TradeDate = DateSerial(Left(sTradeDate, 2),
Mid(sTradeDate, 3, 2), _
Right(sTradeDate, 2))

.SettlementDate = DateSerial(Left(sSettlementDate, 2),
_
Mid(sSettlementDate, 3, 2),
Right(sSettlementDate, 2))

.BuySellCode = ImportData(2, 1)
.MarketCode = ImportData(15, 1)
.BlotterCode = ImportData(16, 1)
.CancelCode = ImportData(17, 1)

.Branch = ImportData(18, 3)
.AccountNumber = ImportData(21, 6)
.CUSIP = ImportData(28, 9)
.TradeReferenceNumber = ImportData(37, 6)

.SecurityType = ImportData(43, 1)
.SecurityTypeModifier = ImportData(44, 1)
.SecurityTypeCalculation = ImportData(45, 1)

.RegisteredRep = ImportData(46, 3)
.AccountClassification = ImportData(59, 2)

.QuantitySign = ImportData(63, 1)
.Quantity = ChargeAdjustment(64, 14, ,
100000, , .QuantitySign)

.PrincipalSign = ImportData(97, 1)
.Principal = ImportData(98, 4)

.AlphaPriceDecimal = bIsDecimal(ImportData(87, 1))
.AlphaPriceDollar = GetAlphaPrice(ImportData(78, 9),
ImportData(88, 9))

End With
....

Here is the function that parses the data:

Public Function ImportData(lStartingPoint As Long, lLength As Long, _
Optional ByVal uDataType As clbDataType =
clbDataTypeString) As Variant

Dim sPropertyType As String

ImportData = Trim$(Mid(gclsMasterRecord.LineData, lStartingPoint,
lLength))

sPropertyType = gclsRecord.ActivePropertyType

If uDataType = clbDataTypeInteger Then
ImportData = CInt(Val(ImportData))

ElseIf uDataType = clbDataTypeDouble Then
ImportData = CDbl(Val(ImportData))

ElseIf uDataType = clbDataTypeLong Then
ImportData = CLng(Val(ImportData))

ElseIf uDataType = clbDataTypeString Then
ImportData = CStr(ImportData)

End If

End Function

I would really like to get rid of the uDataType variable in the
ImportData Function but doing so causes the type mismatch. Is it
possible to convert the ImportData to the proper data type without
having an argument telling ImportData what the data type is? It works
the way it is set up right now but I was just wondering if there is a
better way to do it without explicitly specifying the data type.
Thanks.



All times are GMT +1. The time now is 12:22 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com