C# - Unboxing a class from an ArrayList

Get help on programming - C++, Java, Delphi, etc.
Locked
Bladerunner
Registered User
Posts: 14338
Joined: 04 Sep 2004, 02:00
Processor: i386DX Sooper
Motherboard: A blue one
Graphics card: A red one
Memory: Hard drive
Location: On a Möbius strip
Contact:

C# - Unboxing a class from an ArrayList

Post 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.
If I weren't insane: I couldn't be so brilliant! - The Joker
Bladerunner
Registered User
Posts: 14338
Joined: 04 Sep 2004, 02:00
Processor: i386DX Sooper
Motherboard: A blue one
Graphics card: A red one
Memory: Hard drive
Location: On a Möbius strip
Contact:

Re: C# - Unboxing a class from an ArrayList

Post by Bladerunner »

Nevermind, got it :D

It was

Code: Select all

((Cbrowser)Tabs[0]).Address
If I weren't insane: I couldn't be so brilliant! - The Joker
Bladerunner
Registered User
Posts: 14338
Joined: 04 Sep 2004, 02:00
Processor: i386DX Sooper
Motherboard: A blue one
Graphics card: A red one
Memory: Hard drive
Location: On a Möbius strip
Contact:

Re: C# - Unboxing a class from an ArrayList

Post 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?
If I weren't insane: I couldn't be so brilliant! - The Joker
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Re: C# - Unboxing a class from an ArrayList

Post 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...
Most people would sooner die than think; in fact, they do so - Bertrand Russel
Bladerunner
Registered User
Posts: 14338
Joined: 04 Sep 2004, 02:00
Processor: i386DX Sooper
Motherboard: A blue one
Graphics card: A red one
Memory: Hard drive
Location: On a Möbius strip
Contact:

Re: C# - Unboxing a class from an ArrayList

Post 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);
If I weren't insane: I couldn't be so brilliant! - The Joker
1gn1t0r
Registered User
Posts: 56
Joined: 07 Feb 2006, 02:00
Contact:

Re: C# - Unboxing a class from an ArrayList

Post 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
Locked