Tyron Harford
New member
Didn't know where else to put this, but this was the closest.
This is just a general question regarding narrowing conversions. What I have is one abstract class:
Public MustInherit Class GeneralObject
Default Public MustOverride Property Item(ByVal Index As Integer) As Object
Default Public MustOverride Property Item(ByVal Name As String) As Object
End Class
Then I have another class that inherits that class and overrides the above properties. This class also has some public properties to extend GeneralObject.
Now I have a function that converts a DataRow to the new class, but uses GeneralObject as a parameter. So this function looks a bit like (simplified):
Public Function ConvertData(ByRef ConvObject As Object, dr as DataRow)
For i As Integer = 0 To dRow.Table.Columns.Count - 1
CType(ConvObject, GeneralObject).Item(i) = dRow(i)
Next
End Function
This works fine and dandy, but I'm just wondering, is this the best way to accomplish what I'm doing. To pass an object that inherits GeneralObject to this function I get a conversion narrowing error unless I cast it to a GeneralObject type (which is expected), but I am wondering what data loss there is (if any). If I change the ConvObject type in the above function to a type of GeneralObject, same thing.
To give you a broader picture of what I'm doing, I have several classes that inherit GeneralObject. These classes all have their own public properties, but sometimes I need to access those properties via the indexers rather than accessing the properties - like in the above function where I don't need to know what those properties are, just the index position (or property name).
I'm not too sure if I'm doing this the correct way or if there is a better way to do it. Any help is much appreciated.
If I didn't explain something (most likely), give us a yell.
Cheers,
Ty
This is just a general question regarding narrowing conversions. What I have is one abstract class:
Public MustInherit Class GeneralObject
Default Public MustOverride Property Item(ByVal Index As Integer) As Object
Default Public MustOverride Property Item(ByVal Name As String) As Object
End Class
Then I have another class that inherits that class and overrides the above properties. This class also has some public properties to extend GeneralObject.
Now I have a function that converts a DataRow to the new class, but uses GeneralObject as a parameter. So this function looks a bit like (simplified):
Public Function ConvertData(ByRef ConvObject As Object, dr as DataRow)
For i As Integer = 0 To dRow.Table.Columns.Count - 1
CType(ConvObject, GeneralObject).Item(i) = dRow(i)
Next
End Function
This works fine and dandy, but I'm just wondering, is this the best way to accomplish what I'm doing. To pass an object that inherits GeneralObject to this function I get a conversion narrowing error unless I cast it to a GeneralObject type (which is expected), but I am wondering what data loss there is (if any). If I change the ConvObject type in the above function to a type of GeneralObject, same thing.
To give you a broader picture of what I'm doing, I have several classes that inherit GeneralObject. These classes all have their own public properties, but sometimes I need to access those properties via the indexers rather than accessing the properties - like in the above function where I don't need to know what those properties are, just the index position (or property name).
I'm not too sure if I'm doing this the correct way or if there is a better way to do it. Any help is much appreciated.
If I didn't explain something (most likely), give us a yell.
Cheers,
Ty