avoid use of typeOf to known a class from a base class variable


Author
Message
Fabian R Silva, -
Fabian R Silva, -
StrataFrame User (269 reputation)StrataFrame User (269 reputation)StrataFrame User (269 reputation)StrataFrame User (269 reputation)StrataFrame User (269 reputation)StrataFrame User (269 reputation)StrataFrame User (269 reputation)StrataFrame User (269 reputation)StrataFrame User (269 reputation)
Group: StrataFrame Users
Posts: 153, Visits: 1.2K
How can I avoid to use typeOf when I have some classes that inherit from a base class and I have to compare what type of class the variable is



in the following code:



If TypeOf MainView Is Dx.DxGridView Then

With CType(MainView, Dx.DxGridView)

.Columns(dc.ColumnName).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric

.Columns(dc.ColumnName).DisplayFormat.FormatString = "$ #,0.00"

End With

ElseIf TypeOf MainView Is Dx.DxCardView Then 'DevExpress.XtraGrid.Views.Grid.GridView Then

With CType(MainView, Dx.DxCardView)

.Columns(dc.ColumnName).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric

.Columns(dc.ColumnName).DisplayFormat.FormatString = "$ #,0.00"

End With

End If





to something like:

With CType(MainView, ThetypeOf(MainView))

.Columns(dc.ColumnName).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric

.Columns(dc.ColumnName).DisplayFormat.FormatString = "$ #,0.00"

End With



MainView is a generic Class (devExpress BaseView) and I want to set a column property, but the generic class don't have a "column" and I have to use a ctype, I like to use a ctype (or something else) on the same object to know his type

I say, if mainview is a "cardview" or "gridView" when I put ctype(mainview,somefunctionthatgivemethetype(mainview).column work



the same to use with ctype(businesslayerVariable,getmyowntype(businesslayerVariable)).someproperty and ctype(businesslayerVariable,getmyowntype(businesslayerVariable)) give me myBO type



it is posible? thanks!



sorry if I cannot write my question correctly, I speak in spanish and it's hard to me Ermm
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Well, in this case if the MainView is your class, then I would create a Generic class which would then allow you to work with a strong-typed value all the time...or you could have a type property on the MainView class that tells you what type it is.  This could turn into a REALLY deep and complicated post since there are a lot of avenues to travel.  But let's stick to the simple road BigGrin  If you are in control of the MainView class, then create a enum and test on the type:

Public Enum MyGridType As Integer
    CardView = 0
    GridView = 1
End Enum

Public Class MainView
    Inherits Whatever

Private _ItemType As MyGridType = MyGridType.CardView
Public Property ItemType As MyGridType
    Get
       Return _ItemType
    End Get
    Set (Byval value as MyGridType)
       _ItemType = value
    End Set
End Property

End Class

Then, you can test on the item type (which would reduce the overhead):

Select Case MainView.ItemType
    Case MyGridType.GridView
      '-- Add your code
    Case MyGridType.CareView
      '-- Add your code
End Select

Generics would be another route, but would probably require that you change a bit of code and it is a bit more complicated.

Fabian R Silva, -
Fabian R Silva, -
StrataFrame User (269 reputation)StrataFrame User (269 reputation)StrataFrame User (269 reputation)StrataFrame User (269 reputation)StrataFrame User (269 reputation)StrataFrame User (269 reputation)StrataFrame User (269 reputation)StrataFrame User (269 reputation)StrataFrame User (269 reputation)
Group: StrataFrame Users
Posts: 153, Visits: 1.2K
Thanks for the fastest reply Smile



I try to use a inherited devexpress gridcontrol, and It have a mainview property that have the current visible view, I don't known if it are a cardview or gridview, but both of them have columns, I actually see what type is it with typeof, and then use ctype(mainview,<>).column = xxxxx



I'm reading about it and it appears to be called upcast? I have a variable of type "base" with the content of object "derived" and like to access a property "someproperty" of "derived"

if I put "base.someproperty" it doesn't exist, but .net dont allow me to use ctype(base,realtypeofbase(base)).someproperty and it is annoying (and not useful) to use and "if" or "select" all the time to compare the actual content of a base variable to see what derived object it have and use his properties....



actually I don't have control on MainView (I not sure if I can witout recompile the devexpress xtragrid source code), I will see if I can to use your aproach if I can edit BaseView (or derive it or something else), otherwise will try to use a generic (method?) that retrieve the type or something weird and out of my actual knowledge of .net w00t



thanks for all the help
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search