473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Unable to cast to extended class

If I extend the WebBrowser class like this:

public class CustomWebBrowser : System.Windows.Forms.WebBrowser
{
public string myProperty()
{
return "myProperty";
}
}

and get the ControlsCollection of a Form like this:

ControlsCollection formControls = MyForm.Controls;

and iterate thru the collection like this:

foreach (Control ctl in formControls)
{
// if I am at the CustomWebBrowser control I am unable to cast like this
CustomWebBrowser browser = ctl as CustomWebBrowser;
}

In the above code the browser object is null when doing this with my
CustomWebBrowser class. However, if I just use the WebBrowser class where it
is not extended the control 'ctl' I am able to cast to a WebBrowser class.

How can I cast from the Control class to my CustomWebBrowser class? Any
ideas?

Thank you!
Feb 10 '07 #1
4 3867
"Matt Love" <do*@newsgroup.nospamwrote in message
news:6F**********************************@microsof t.com...
If I extend the WebBrowser class like this:
....
and get the ControlsCollection of a Form like this:

ControlsCollection formControls = MyForm.Controls;

and iterate thru the collection like this:

foreach (Control ctl in formControls)
{
// if I am at the CustomWebBrowser control I am unable to cast like
this
CustomWebBrowser browser = ctl as CustomWebBrowser;
}

In the above code the browser object is null when doing this with my
CustomWebBrowser class. However, if I just use the WebBrowser class where
it
is not extended the control 'ctl' I am able to cast to a WebBrowser class.
I have tested it and I have got positive result (CustomWebBrowser control
was properly discovered). My modified code:

private void button2_Click(object sender, EventArgs e)
{
foreach (Control ctl in this.Controls)
{
CustomWebBrowser browser = ctl as CustomWebBrowser;

if (browser != null)
MessageBox.Show(browser.myProperty());
}
}
--
Regards,
Grzegorz

Feb 11 '07 #2
Thanks Grzegorz,

I should have mentioned that the CustomeWebBrowser class is defined in
another project and imported into the one I am having problems casting. I am
building an automated test tool where one app launches another.

In the test tool app I am launching my app (which implements the
CustomWebBrowser control) like this:

[STAThread]
public static object StartAUT(string applicationPath, string typeName)
{
Assembly asm = Assembly.LoadFrom(applicationPath);
Type typeUT = asm.GetType(typeName);
object obj = Activator.CreateInstance(typeUT);

Type[] types = new Type[0];
MethodInfo mi = typeUT.GetMethod("Show", types);
mi.Invoke(obj, null);
return obj;
}

and then I am calling the StartAUT method like this:

AUT = (Form)GUITestUtil.StartAUT(AUTPath, startupType);

....this is where I then run thru the foreach (Control ctl in AUT.Controls)

Perhaps this detail is the issue?

"Grzegorz Danowski" wrote:
"Matt Love" <do*@newsgroup.nospamwrote in message
news:6F**********************************@microsof t.com...
If I extend the WebBrowser class like this:
....
and get the ControlsCollection of a Form like this:

ControlsCollection formControls = MyForm.Controls;

and iterate thru the collection like this:

foreach (Control ctl in formControls)
{
// if I am at the CustomWebBrowser control I am unable to cast like
this
CustomWebBrowser browser = ctl as CustomWebBrowser;
}

In the above code the browser object is null when doing this with my
CustomWebBrowser class. However, if I just use the WebBrowser class where
it
is not extended the control 'ctl' I am able to cast to a WebBrowser class.

I have tested it and I have got positive result (CustomWebBrowser control
was properly discovered). My modified code:

private void button2_Click(object sender, EventArgs e)
{
foreach (Control ctl in this.Controls)
{
CustomWebBrowser browser = ctl as CustomWebBrowser;

if (browser != null)
MessageBox.Show(browser.myProperty());
}
}
--
Regards,
Grzegorz

Feb 11 '07 #3
Hi Matt,

This is probably because the copy of the assembly contains the
CustomWebBrowser that you referenced in your test app is not the same one
as you're loading from. In other word, if you load two copies of the same
assembly from different location, the same class in two copies are NOT the
same type. Here's a simple test to verify the behavior:

1) Create a solution, add a Class Library which contains the custom
WebBrowser. It also contains a WinFom to use the custom WebBrowser.
2) Add a Windows Application, reference the Class Library, note the
ClassLibrary1.dll will be copied to this Windows Application's output
directory.
3) Use the output directory of Class Library to load the ClassLibrary1.dll,
however, since you're referencing the copy in output directory of Windows
Application, they will not be equal:

private void button1_Click(object sender, EventArgs e)
{
string path1 = @"c:\temp\ClassLibrary1\bin\Debug\ClassLibrary1.dl l";
string path2 =
@"c:\temp\WindowsApplication1\bin\Debug\ClassLibra ry1.dll";
Form aut = (Form)StartAUT(path1, "ClassLibrary1.Form1");
foreach(Control ctl in aut.Controls)
{
ClassLibrary1.Class1 c1 = ctl as ClassLibrary1.Class1;
Debug.Assert(c1 != null);
}
}
If you use path2 above to test, the code will run successfully.

Hope this helps.
Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 12 '07 #4

Thank you! Problem solved...

"Walter Wang [MSFT]" wrote:
Hi Matt,

This is probably because the copy of the assembly contains the
CustomWebBrowser that you referenced in your test app is not the same one
as you're loading from. In other word, if you load two copies of the same
assembly from different location, the same class in two copies are NOT the
same type. Here's a simple test to verify the behavior:

1) Create a solution, add a Class Library which contains the custom
WebBrowser. It also contains a WinFom to use the custom WebBrowser.
2) Add a Windows Application, reference the Class Library, note the
ClassLibrary1.dll will be copied to this Windows Application's output
directory.
3) Use the output directory of Class Library to load the ClassLibrary1.dll,
however, since you're referencing the copy in output directory of Windows
Application, they will not be equal:

private void button1_Click(object sender, EventArgs e)
{
string path1 = @"c:\temp\ClassLibrary1\bin\Debug\ClassLibrary1.dl l";
string path2 =
@"c:\temp\WindowsApplication1\bin\Debug\ClassLibra ry1.dll";
Form aut = (Form)StartAUT(path1, "ClassLibrary1.Form1");
foreach(Control ctl in aut.Controls)
{
ClassLibrary1.Class1 c1 = ctl as ClassLibrary1.Class1;
Debug.Assert(c1 != null);
}
}
If you use path2 above to test, the code will run successfully.

Hope this helps.
Sincerely,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Pankaj Jain | last post by:
Hi All, I have a class A which is derived from ServicesComponent to participate in automatic transaction with falg Transaction.Required. Class A is exposed to client through remoting on Http...
8
by: | last post by:
I have the following class : Public Class MyTreeNode Inherits TreeNode Dim mystring As String End Class Now, when I try to do this : ''''''''''''nodes is a TreeNodeCollection, s is string
9
by: Jim in Arizona | last post by:
I get this error: Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputText' to type 'System.Web.UI.WebControls.TextBox'. Using this code: Dim test3 As TextBox test3 =...
4
by: JackBlack | last post by:
Hi, all! Need a little help tracking down a runtime error problem. I'm getting this error: "Unable to cast object of type 'myStruct' to type 'myStruct'... but the two types are identical! I...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
0
by: Pieter | last post by:
Hi, I'm using NHibernate 1.2 (CR1), and I'm using a custom list (inherited from BindingList(Of T) ) for all my lists. The NHibernate documentation told me that I had to implement...
2
by: Bigi | last post by:
Hi, Please help, this has been driving me nuts for nearly 2 days now. This vb6 code works: Public oEng As New ebizEngine Public oMsg As ebizMessage Function EbizGetFromQueue() As String
1
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
Hello, Using VS2008 in a C# web service application, a class has been created that inherits from the ConfigurationSelection. This class file has been placed in the App_Code folder. The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.