﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » .NET Forums » General .NET Discussion  » avoid use of typeOf to known a class from a base class variable</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Mon, 08 Jun 2026 20:47:15 GMT</lastBuildDate><ttl>20</ttl><item><title>avoid use of typeOf to known a class from a base class variable</title><link>http://forum.strataframe.net/FindPost14614.aspx</link><description>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&lt;br&gt;
&lt;br&gt;
 in the following code:&lt;br&gt;
[code]&lt;br&gt;
If TypeOf MainView Is Dx.DxGridView Then &lt;br&gt;
    With CType(MainView, Dx.DxGridView)&lt;br&gt;
        .Columns(dc.ColumnName).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric&lt;br&gt;
        .Columns(dc.ColumnName).DisplayFormat.FormatString = "$ #,0.00"&lt;br&gt;
    End With&lt;br&gt;
ElseIf TypeOf MainView Is Dx.DxCardView Then 'DevExpress.XtraGrid.Views.Grid.GridView Then&lt;br&gt;
    With CType(MainView, Dx.DxCardView)&lt;br&gt;
        .Columns(dc.ColumnName).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric&lt;br&gt;
        .Columns(dc.ColumnName).DisplayFormat.FormatString = "$ #,0.00"&lt;br&gt;
    End With&lt;br&gt;
End If&lt;br&gt;
[/code]&lt;br&gt;
&lt;br&gt;
to something like: &lt;br&gt;
    With CType(MainView, ThetypeOf(MainView))&lt;br&gt;
        .Columns(dc.ColumnName).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric&lt;br&gt;
        .Columns(dc.ColumnName).DisplayFormat.FormatString = "$ #,0.00"&lt;br&gt;
    End With&lt;br&gt;
&lt;br&gt;
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&lt;br&gt;
I say, if mainview is a "cardview" or "gridView" when I put ctype(mainview,somefunctionthatgivemethetype(mainview).column work&lt;br&gt;
&lt;br&gt;
the same to use with ctype(businesslayerVariable,getmyowntype(businesslayerVariable)).someproperty and ctype(businesslayerVariable,getmyowntype(businesslayerVariable)) give me myBO type&lt;br&gt;
&lt;br&gt;
it is posible? thanks!&lt;br&gt;
&lt;br&gt;
sorry if I cannot write my question correctly, I speak in spanish and it's hard to me :ermm:</description><pubDate>Thu, 28 Feb 2008 14:04:45 GMT</pubDate><dc:creator>Fabian R Silva, -</dc:creator></item><item><title>RE: avoid use of typeOf to known a class from a base class variable</title><link>http://forum.strataframe.net/FindPost14618.aspx</link><description>Thanks for the fastest reply :)&lt;br&gt;
&lt;br&gt;
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,&lt;&lt;gridview or cardview or the derived type&gt;&gt;).column = xxxxx&lt;br&gt;
&lt;br&gt;
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" &lt;br&gt;
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....&lt;br&gt;
&lt;br&gt;
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:&lt;br&gt;
&lt;br&gt;
thanks for all the help</description><pubDate>Thu, 28 Feb 2008 14:04:45 GMT</pubDate><dc:creator>Fabian R Silva, -</dc:creator></item><item><title>RE: avoid use of typeOf to known a class from a base class variable</title><link>http://forum.strataframe.net/FindPost14616.aspx</link><description>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.&amp;nbsp; This could turn into a REALLY deep and complicated post since there are a lot of avenues to travel.&amp;nbsp; But let's stick to the simple road :D&amp;nbsp; If you are in control of the MainView class, then create a enum and test on the type:&lt;/P&gt;&lt;P&gt;[codesnippet]Public Enum MyGridType As Integer&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CardView = 0&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; GridView = 1&lt;BR&gt;End Enum&lt;/P&gt;&lt;P&gt;Public Class MainView&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Inherits Whatever&lt;/P&gt;&lt;P&gt;Private _ItemType As MyGridType = MyGridType.CardView&lt;BR&gt;Public Property ItemType As MyGridType&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Get&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Return _ItemType&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Get&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Set (Byval value as MyGridType)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _ItemType = value&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Set&lt;BR&gt;End Property&lt;/P&gt;&lt;P&gt;End Class[/codesnippet]&lt;/P&gt;&lt;P&gt;Then, you can test on the item type (which would reduce the overhead):&lt;/P&gt;&lt;P&gt;[codesnippet]Select Case MainView.ItemType&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Case MyGridType.GridView&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Add your code&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Case MyGridType.CareView&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; '-- Add your code&lt;BR&gt;End Select[/codesnippet]&lt;/P&gt;&lt;P&gt;Generics would be another route, but would probably require that you change a bit of code and it is a bit more complicated.</description><pubDate>Thu, 28 Feb 2008 13:00:47 GMT</pubDate><dc:creator>Trent L. Taylor</dc:creator></item></channel></rss>