Page 1 of 1

C# - Unboxing a class from an ArrayList

Posted: 14 Mar 2009, 12:49
by Bladerunner
Good day everyone :)

I'm trying to develop a web browser with multible "tabs". I did not insert a tabcontrol yet, I'm adding the classes to an ArrayList called "Tabs".

So here's the basic layout:

Code: Select all

public abstract class CAbsBrowse
    {
        protected string sAddress;
        protected bool useIP = false;
    }

Code: Select all

 public class CBrowser : CAbsBrowse
    {
        WebBrowser myBrowser;
        CNavigation Navigation = new CNavigation();              

        public CBrowser()
        {
            myBrowser = new WebBrowser();
        }

        public void Browse(string sAddy)
        {
            if (UsingIP)
            {
                myBrowser.Navigate(Navigation.NavigateViaIP(sAddy));
            }
            else
            {
                myBrowser.Navigate(Navigation.NavigateNormally(sAddy));
            }
        }

        public string Address
        {
            get
            {
                return sAddress;
            }
            set
            {
                sAddress = value;
            }
        }

        public bool UsingIP
        {
            get
            {
                return useIP;
            }
            set
            {
                useIP = value;
            }
        }
------------------------

Now that you have a rough idea of it, this is what I want to do in my main form:

Code: Select all

//First browser window
            CBrowser Browse = new CBrowser;
            Tabs.Add(Browser);  //Tabs is an ArrayList
But I want to refer back to the objects in the ArrayList.

What if I want to know the Navigation address of the Browser object at index 3 of Tabs? How would I do that?

I've tried this:

Code: Select all

(CBrowser) Tabs[0].Address.........
But unfortunately after Tabs[0]. the intellisense only returns: Equals, GetHashCode, GetType, ToString.

Any help is appreciated.

Re: C# - Unboxing a class from an ArrayList

Posted: 14 Mar 2009, 13:06
by Bladerunner
Nevermind, got it :D

It was

Code: Select all

((Cbrowser)Tabs[0]).Address

Re: C# - Unboxing a class from an ArrayList

Posted: 15 Mar 2009, 08:39
by Bladerunner
Okay, one more prob :lol:

Now that I have created an array of browser objects like this:

WebBrowser [] Browser = new WebBrowser [50];

How can I refer to the events of the WebBrowser? Such as the Navigating / Navigated events?

Re: C# - Unboxing a class from an ArrayList

Posted: 16 Mar 2009, 08:29
by rustypup
how's your MVC?..

components, when triggering events, generally include a self-reference in the event package... this means that the sole purpose of the array is to prevent untimely collection by the gc...

using generics, you'd implement the necessary listener interface and register some class on each WebBrowser instance to process the pertinent events... (care should be taken to de-register a defunct browser instance to prevent leaks..)

Actor - event generator.
Event - notification package, (generally just a reference to Actor and maybe a state flag).
Listener - generic interface used by objects interested in Actor events.
Consumer - event consumer.

Consumer implements Listener interface.
Consumer is registered with Actor to receive events.
Actor processes event to each registered Consumer.

(Note that, should the consumer fall out of scope elsewhere, as long as Actor retains a reference, the instance will not be collected...)

Consumer can be connected to multiple a Actor instances...
Event is out of scope once each Consumer has left the implemented Listener function...

C# also has the thoroughly wacky delegate...

Re: C# - Unboxing a class from an ArrayList

Posted: 16 Mar 2009, 09:08
by Bladerunner
rustypup wrote:how's your MVC?..

components, when triggering events, generally include a self-reference in the event package... this means that the sole purpose of the array is to prevent untimely collection by the gc...

using generics, you'd implement the necessary listener interface and register some class on each WebBrowser instance to process the pertinent events... (care should be taken to de-register a defunct browser instance to prevent leaks..)

Actor - event generator.
Event - notification package, (generally just a reference to Actor and maybe a state flag).
Listener - generic interface used by objects interested in Actor events.
Consumer - event consumer.

Consumer implements Listener interface.
Consumer is registered with Actor to receive events.
Actor processes event to each registered Consumer.

(Note that, should the consumer fall out of scope elsewhere, as long as Actor retains a reference, the instance will not be collected...)

Consumer can be connected to multiple a Actor instances...
Event is out of scope once each Consumer has left the implemented Listener function...

C# also has the thoroughly wacky delegate...
Thanks for the reply rusty. :) Not everything you said makes sense but I'll read up on it.

I got it right using the following code:

Code: Select all

((WebBrowser)(Tabs)[TabCont.SelectedIndex]).DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(frmSpider_DocumentCompleted);

((WebBrowser)(Tabs)[TabCont.SelectedIndex]).ProgressChanged += new WebBrowserProgressChangedEventHandler(frmSpider_ProgressChanged);

Re: C# - Unboxing a class from an ArrayList

Posted: 28 Mar 2010, 01:20
by 1gn1t0r
You have taken quite a weird design for developing a web browser. For your other question look into delegates and event handling.

Please check the date on threads before you reply to something that has probably been solved a long time ago already. - The Rat