Inheritance and Narrowing Conversion...

Tyron Harford

New member
Joined
Jan 23, 2006
Messages
3
Location
New Zealand
Programming Experience
5-10
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
 
Do you intend to import a set of values into a datarow?

Why not jsut store them in a datarow in the first place? Why not make a GeneralObject that wraps a relevant datarow?
 
Thanks for the DataRow idea - I'm not too sure of any overhead the DataRow has behind the scenes though. All I wanted was an object that represents an entity of sorts.

Populating data into the GeneralObject via a DataRow is only one possible way though. Right now the performance is awesome - through remoting and web services, getting data in and out (out is faster) of the DAL is fast. Would you happen to know if the GeneralObject wrapped a DataRow, would this impede performance/memory usage much?

As soon as I get some time, I'll give it a shot though.

Cheers,
Ty
 
Back
Top