avoid use of typeOf to known a class from a base class variable
 
Home My Account Forum Try It! Buy It!
About Contact Us Site Map
StrataFrame Forum
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      



avoid use of typeOf to known a class from a...Expand / Collapse
Author
Message
Posted 02/28/2008 11:42:18 AM


StrataFrame Novice

StrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame Novice

Group: StrataFrame Users
Last Login: Today @ 10:30:50 AM
Posts: 106, Visits: 643
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
Post #14614
Posted 02/28/2008 1:00:47 PM


StrataFrame Developer

StrataFrame Developer

Group: StrataFrame Developers
Last Login: Today @ 11:13:06 AM
Posts: 4,104, Visits: 4,176
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   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.

Post #14616
Posted 02/28/2008 2:04:45 PM


StrataFrame Novice

StrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame NoviceStrataFrame Novice

Group: StrataFrame Users
Last Login: Today @ 10:30:50 AM
Posts: 106, Visits: 643
Thanks for the fastest reply

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

thanks for all the help
Post #14618
« Prev Topic | Next Topic »


Reading This TopicExpand / Collapse
Active Users: 0 (0 guests, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: Ben Chase, Trent L. Taylor, Steve L. Taylor

PermissionsExpand / Collapse

All times are GMT -6:00, Time now is 6:01pm

Powered By InstantForum.NET v4.1.4 © 2008
Execution: 0.047. 10 queries. Compression Enabled.
Site Map - Home - My Account - Forum - About Us - Contact Us - Try It - Buy It

Microsoft, Visual Studio, and the Visual Studio logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries.