OO Upcasting issue

  • Thread starter Thread starter Fred.
  • Start date Start date
F

Fred.

I haven't been in OO all that long but ...
I would like to upcast to a new object type and add functionality
(methods only).
The runtime cast opertion fails in the sample below. Is there a way
to do it explicitly? Conceptually it is straightforward in cases such
as this where no data is added or shielded from the base class in the
derived class.

(This is only a sample. I realize that the method could be put in the
base class but the actual scenario involves a base class from the .NET
framework)

public class Props
{
public int NumberB;
public int NumberC;
}// public class Props

public class CustomProps : Props
{
public int BxC()
{
return NumberB * NumberC;
}
}

Props prop = new Props();
prop.NumberB = 4;
prop.NumberC = 5;

//this fails
CustomProps custprop = (CustomProps)prop;
int result = custprop.BxC ();
 
Hi Fred,
I'm not aware of any oo languages, which can do that. You cannot upcast in
this case because run-tyme type of the obejcts is Props not CustomProps.
Speeking of c# you cannot even overload the cast operation in this case
because upcast and downcast are predifined casting operation and they cannot
be overloaded.

I just came up with this idea. It si not exactly what you want, though.
Anyway, it can lead you to a better solution.

public class Props
{
public int NumberB;
public int NumberC;

public Props(int B, int C)
{
NumberB = B;
NumberC = C;
}
}// public class Props

public class CustomProps : Props
{
public int BxC()
{
return NumberB * NumberC;
}

public CustomProps(int B, int C): base(B, C)
{
}
}

public class CustomPropsUpcastHelper
{
private Props props;
public CustomPropsUpcastHelper(Props p)
{
props = p;
}

public static implicit operator CustomProps(CustomPropsUpcastHelper
helper)
{
return new CustomProps(helper.props.NumberB, helper.props.NumberC);
}

public static explicit operator CustomPropsUpcastHelper(Props props)
{
return new CustomPropsUpcastHelper(props);
}
}


and you can use this like:

Props props = new Props(10, 20);
CustomProps customProps = (CustomPropsUpcastHelper)props;
Console.WriteLine(customProps.BxC());
Console.ReadLine();

HTH

B\rgds
100
 
Thanks for the responses.

While it is true that declaring the object as a custom one initially
solves the sample problem, I still have the issue of beginning with an
intrinsic framework object, DataGridItem.

The suggestion posted by
"100" is what I tried with an explicit implementation of the
constructor in a derived object, CustomDGI.

The actual code below shows demonstrates that the derived object is
not quite fully populated (the FindControl method works on the
intrinsic object but not on the derived one). I'm not sure if this a
coding issue on my part or a bug with DataGridItem. I'd appreciate
any further insight you may have.

- Fred

********ASPX**************
<%@ Page language="c#" Codebehind="Upcasting.aspx.cs"
AutoEventWireup="false" Inherits="UCtest.Upcasting" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Upcasting</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Upcasting" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="col1"
HeaderText="Column1"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="TemplateCol">
<ItemTemplate>
<asp:Label id="lblCol2" runat="server">Col2 Label</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML>


********Code behind**************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace UCtest
{
public class Upcasting : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void DataGrid1_ItemCreated(object sender,
DataGridItemEventArgs e)
{
Label oLabel;
if (e.Item.ItemIndex == 0)
{
//this works
DataGridItem oDataGridItem = e.Item;
oLabel = (Label) oDataGridItem.FindControl("lblCol2");
oLabel.Text = "base";
}
if (e.Item.ItemIndex == 1)
{
//how is this done?
CustomDGI oCustomDGI = new CustomDGI(e.Item.ItemIndex,
e.Item.DataSetIndex, e.Item.ItemType) ;
//CustomDGI oCustomDGI = e.Item as CustomDGI;
oLabel = oCustomDGI.FindLabel("lblCol2");
oLabel.Text = "derived";
}
}

private void Page_Load(object sender, System.EventArgs e)
{
DataTable oDataTable = new DataTable();
oDataTable.Columns.Add("col1");
oDataTable.Rows.Add(new string[1] {"Row1"});
oDataTable.Rows.Add(new string[1] {"Row2"});
DataGrid1.DataSource = oDataTable;
DataGrid1.DataBind ();
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.DataGrid1.ItemCreated += new
DataGridItemEventHandler(this.DataGrid1_ItemCreated);
this.Load += new System.EventHandler(this.Page_Load);
}
}

public class CustomDGI : DataGridItem
{
public CustomDGI(int itemIndex, int DataSetIndex, ListItemType
itemType) : base(itemIndex, DataSetIndex, itemType)
{
}

public Label FindLabel(string sID)
{
return (Label) base.FindControl("lblCol2");
}
}
}

*******************************************************************************
 
Back
Top