well..how can i make that the child class can access the myMain function in the mother class?PHP Code:class mother
{
public void myMain()
{
}
class child
{
public void test()
{
}
}
}
Printable View
well..how can i make that the child class can access the myMain function in the mother class?PHP Code:class mother
{
public void myMain()
{
}
class child
{
public void test()
{
}
}
}
PHP Code:public class Mother
{
public void myMain()
{
MessageBox.Show("myMain");
}
class child
{
Mother mother = new Mother();
public void test()
{
mother.myMain();
}
}
}
yea but i dont want a NEW mother...i want to use that instanciated class mother..what i really want to do is a class that has inside itself a collection of all items...the class itself is the mother, and the collection is a class inherited from BaseCollection or DicitionaryCollection..and i want that when i add a new item to the child class(the collection) it at the same time executes a command in the mother class (the mother class is inherited from a tabPage, and i want that when a new item to the collection is added it calls a function that will draw a button into the panel)...well...how i am supose to do this?
maybe this?
PHP Code:using System;
using System.Collections;
namespace NestedClassTest
{
public class MainApp
{
public static void Main(string[] args)
{
Mother mother = new Mother();
mother.AddChild(new Mother.Child("Sally"));
mother.AddChild(new Mother.Child("Chris"));
Console.ReadLine();
}
}
public class Mother
{
private ArrayList children = new ArrayList();
public void DrawButton(object obj)
{
Console.WriteLine("Drawing button for {0}...",((Child)obj).name);
}
public void RemoveButton(object obj)
{
Console.WriteLine("Removing button for {0}...", ((Child)obj).name );
}
public void AddChild(object obj)
{
children.Add(obj);
DrawButton(obj);
}
public void RemoveChild(object obj)
{
children.Remove(obj);
RemoveButton(obj);
}
public class Child
{
public string name = "";
public Child() {}
public Child(string name)
{
this.name = name;
}
}
}
}
I tried something similar but it did'nt work. Now I see why. I was instantiating the child class like this:
Code:child chd = new child(); //child was not coming up in intellisense
hmm pvb tks..but there should be another way..when u put a listbox in ur form u dont have to put all thhis code to add a new item to the listbox,dont u?
hmm...arent there any other ways?PHP Code:Mother mother = new Mother();
mother.AddChild(new Mother.Child("Sally"));
mother.AddChild(new Mother.Child("Chris"));
well, here's the code to load a listbox:
only reason i have a nested class in my example named Child is because the original post had it. If you really didn't want the Child class then you can rewrite it this way:PHP Code:this.listBox1 = new System.Windows.Forms.ListBox();
this.listBox1.Items.Add("Chris");
this.listBox1.Items.Add("Sally");
PHP Code:using System;
using System.Collections;
namespace NestedClassTest
{
public class MainApp
{
public static void Main(string[] args)
{
Mother mother = new Mother();
mother.AddChild("Sally");
mother.AddChild("Chris");
Console.ReadLine();
}
}
public class Mother
{
private ArrayList children = new ArrayList();
public void DrawButton(object obj)
{
Console.WriteLine("Drawing button ... {0}", obj.ToString());
}
public void RemoveButton(object obj)
{
Console.WriteLine("Removing button ... {0}", obj.ToString());
}
public void AddChild(object obj)
{
children.Add(obj);
DrawButton(obj);
}
public void RemoveChild(object obj)
{
children.Remove(obj);
RemoveButton(obj);
}
}
}
no man, the Child Class is a BaseCollection class! i want 2 classes, one that is a tabPage and another that is a BaseCollection-inherited class
but that ideas isnt bad...hehe lol ..maybe i use a private arraylist and then create my own functions..welll.
hmm well...now how do i make that the users make this:
myClass mclass = new myClass();
mclass.items.add("...)";
??
k, how bout this guy:
PHP Code:using System;
using System.Collections;
namespace NestedClassTest
{
public class MainApp
{
public static void Main(string[] args)
{
Mother mother = new Mother();
mother.Items.Add("Chris");
Console.WriteLine(mother[0].ToString());
Console.ReadLine();
}
}
public class Mother
{
private ArrayList items = new ArrayList();
public object this[int index]
{
get
{
if (index < 0 || index > (items.Count - 1))
throw new IndexOutOfRangeException();
else
return items[index];
}
}
public ArrayList Items
{
get { return items; }
}
}
}
let me check ..but it looks just like i want it :D:D
i've reed about indexeres and that but i dont understand what is this:
public object this[int index]
{
get
{
if (index < 0 || index > (items.Count - 1))
throw new IndexOutOfRangeException();
else
return items[index];
}
}
what is that? i know its a data type but what really?
it's just the return type of the indexer, so when you do ArrayList.Add, the Add method takes an object as a parameter. Basically all the indexer does is return the same Type that is contained within the ArrayList. So what you have is a really generic collection that can hold anything that's an object. If you know your collection will always only hold strings then the type of the indexer would be "string".
You could do something like this too:
It all depends on how you want to use the object (your mother).PHP Code:static void Main(string[] args)
{
Mother MyMum = new Mother();
// Add children
MyMum.Add( "Tommy" );
MyMum.Add( "Simon" );
MyMum.Add( new Child("Bill") );
MyMum.Add( new Child("William") );
// Write out child names
Console.WriteLine("Kid1: {0}", ((Child)MyMum[0]).Name);
Console.WriteLine("Kid2: {0}", ((Child)MyMum[1]).Name);
Console.WriteLine("Kid3: {0}", ((Child)MyMum[2]).Name);
Console.WriteLine("Kid4: {0}", ((Child)MyMum[3]).Name);
// or
Console.WriteLine("Kid1: {0}", MyMum[0].ToString());
Console.WriteLine("Kid2: {0}", MyMum[1].ToString());
Console.ReadLine();
}
class Mother : ArrayList
{
// Add child by name
public int Add(string NewChildName)
{
return base.Add(new Child(NewChildName));
}
// Add child by a new child object
public override int Add(object NewChild)
{
if (NewChild.GetType() == Type.GetType("CollectionTest.Child"))
return base.Add( NewChild );
else
return base.Count;
}
}
class Child
{
private string childName;
public Child(string Name)
{
childName = Name;
}
public string Name
{
get {return childName; }
set {childName = value; }
}
public override string ToString()
{
return childName.ToString();
}
}
i changed the type to string but it keeps saying in the IDE intelsense that it's an object ArrayList :(
PT,
I think you will have to change the type to string as well as casting the ArrayList to string for the return of the indexer method.
ah ok tks ill try
dakmn im getting damn confused..tomorrow ill give another try at this :confused:
PHP Code:public string this[int Index]
{
get
{
// exception will be thrown by ArrayList if Index is out of bounds
return Items[Index].ToString();
}
}
no, the problem is:
how do i make that MY commands of the arraylist appear in the .Items part of the class?
i want by eg put:
myClass.Items.MakeFolder
myClass.Items.MakeFile
how do i make this? i tryed making what someone of u done but i seem to not can do it..i am making something wrong
Code:using System;
using System.Collections;
namespace NestedClassTest
{
public class MainApp
{
public static void Main(string[] args)
{
MyClass myClass = new MyClass();
myClass.Items.MakeFile(@"C:\myfile\something.txt");
myClass.Items.MakeFolder(@"C:\somefolder");
Console.ReadLine();
}
}
public class MyClass
{
private ArrayListEx items = new ArrayListEx();
public ArrayListEx Items
{
get { return items; }
}
}
public class ArrayListEx : ArrayList
{
public void MakeFolder(string path)
{
Console.WriteLine("Making a folder here: {0}", path);
}
public void MakeFile(string path)
{
Console.WriteLine("Making a file here: {0}", path);
}
}
}
but the problem is still the same..if the upper class is a tabPage how do i make that the subclass can draw a button on the tabPage?
assuming the code from pvb...
Is that what you are trying to do??PHP Code:interface IDrawButton
{
DrawButton(int , int);
}
public class MyApp : IDrawButton
{
public MyApp()
{
items.SetDrawInterface( this );
}
...
}
public class ArrayListEx : ArrayList
{
IDrawButton parentToDraw;
// Make a reference to the parent's IDrawButton interface
public void SetDrawInterface( object Parent )
{
parentToDraw = (IDrawButton)Parent;
}
// Call the parent's DrawButton method though the IDrawButton interface
public void DrawButton()
{
parentToDraw.DrawButton;
}
...
}
no it isnt..when i come from school ill explain again
the problem is i want it to draw the button in the ADD() of the collection..
myClass.Items.Add()
I guess I'm still uncertain about what you are trying to accomplish.
Essentially, anything is possible. You can have your children call into their parent via a reference to the parent object. You can have delegates that the parent consumes. You can inherit from a collection to provide the collection functionality (Parent[n].Method()) or you can use composition to provide the collection functionality (Parent.Kids[n].Method() and Parent.Kids.Method()).
Maybe you could describe some broader examples a what you are doing (not showing the classes).
did u ever look at a listbox?
it has what it appears to be an instance of a class called Items...it's probably a collection or something like that but right now it doesnt mattar much...now..when u make ListBox.Items.Add() ur in that moment using the Items class that has a Add function...that function will add to the collection 1item and ALSO will add one line to the GUI(interface) of the listbox...but to add that line to the listbox it somewhat will have to reference the listbox class that is the class that has the Items' class inside.
i am making a class inheriting from tabPage...i want to have just like a listbox an Items field that inside has some kind of collection...and when i make the tabPage.Items.Add() it adds to the collection 1item and also creates a button on the tabPage...how do i make this?
was i more explicit now?
alright, this is it(lol):
usage, in the Form_Load event put this:PHP Code:public class PanelEx : Panel
{
private int height = 0;
private int width = 0;
private int top = 0;
private int left = 0;
private PanelItems items = null;
public PanelItems Items
{
get { return items; }
}
public PanelEx()
{
base.Height = height;
base.Width = width;
base.Top = top;
base.Left = top;
items = new PanelItems(this);
}
public PanelEx(int Height, int Width, int Top, int Left)
{
base.Height = height = Height;
base.Width = width = Width;
base.Top = top = Top;
base.Left = left = Left;
items = new PanelItems(this);
}
public void CoverForm(Form frm)
{
this.Height = frm.Height;
this.Width = frm.Width;
this.Refresh();
}
public class PanelItems : CollectionBase
{
private PanelEx parent = null;
private PanelItems(){}
public PanelItems(PanelEx Parent)
{
parent = Parent;
}
public void Add(string ButtonName)
{
Button btn = null;
if (this.List.Count > 0)
{
Button lastButton = ((Button)this.List[this.List.Count - 1]);
int newLeft = lastButton.Left + lastButton.Width;
btn = getButton(ButtonName, 25, 50, 0, newLeft);
}
else
{
btn = getButton(ButtonName, 25, 50, 0, 0);
}
add(btn);
}
public void Add(string ButtonName, int Height, int Width)
{
Button btn = getButton(ButtonName, Height, Width, 0, 0);
add(btn);
}
public void Add(string ButtonName, int Height, int Width, int Top, int Left)
{
Button btn = getButton(ButtonName, Height, Width, Top, Left);
add(btn);
}
private void add(Button btn)
{
parent.Controls.Add(btn);
this.List.Add(btn);
}
protected void btn_Click(object sender, System.EventArgs e)
{
MessageBox.Show(((Button)sender).Text);
}
private Button getButton(string text, int height, int width, int top, int left)
{
Button btn = new Button();
btn.Text = text;
btn.Height = height;
btn.Width = width;
btn.Top = top;
btn.Left = left;
btn.Click += new System.EventHandler(btn_Click);
return btn;
}
}
}
i left out alot of rules, just gives the basic concept. lol, lemme guess, not quite what you're lookin for? :)PHP Code:private void Form1_Load(object sender, System.EventArgs e)
{
PanelEx myPanel = new PanelEx();
myPanel.CoverForm(this);
myPanel.Items.Add("Chris");
myPanel.Items.Add("Sally");
myPanel.Items.Add("Joe");
this.Controls.Add(myPanel);
}
it has nothing to do with what i asked!!!
kiddin..ill look at it now :D
very well done..that was what i was looking for! i've already tryed used the 'this' method but it didnt work...i think it's because it must have the thing = null first and only in the constructor assign the value to the var..:confused:
well im working really cool now but now i've come with another question...how do i make something like this
i know it uses an indexer but what about the following thing?PHP Code:myClass.Items[0].Text;
myClass.Items[0].Status;
myClass.Items[0].Progress;
easy, in terms of the example i last posted, create new class that represents the object you plan on adding to your collection, I'll call mine ButtonEx that inherits from Button:
Then change all references to the Button class to the ButtonEx class(now with new super properties) and the PanelEx code looks like this:PHP Code:public class ButtonEx : Button
{
public string Status;
public string Progress;
public ButtonEx()
{
Status = "Some Status";
Progress = "Some Progress";
}
}
Note the indexer i added to the PanelItems:Code:public class PanelEx : Panel
{
private int height = 0;
private int width = 0;
private int top = 0;
private int left = 0;
private PanelItems items = null;
public PanelItems Items
{
get { return items; }
}
public PanelEx()
{
base.Height = height;
base.Width = width;
base.Top = top;
base.Left = top;
items = new PanelItems(this);
}
public PanelEx(int Height, int Width, int Top, int Left)
{
base.Height = height = Height;
base.Width = width = Width;
base.Top = top = Top;
base.Left = left = Left;
items = new PanelItems(this);
}
public void CoverForm(Form frm)
{
this.Height = frm.Height;
this.Width = frm.Width;
this.Refresh();
}
public class PanelItems : CollectionBase
{
private PanelEx parent = null;
private PanelItems(){}
//indexer
public ButtonEx this [int index]
{
get { return (ButtonEx)this.List[index]; }
}
public PanelItems(PanelEx Parent)
{
parent = Parent;
}
public void Add(string ButtonName)
{
ButtonEx btn = null;
if (this.List.Count > 0)
{
ButtonEx lastButton = ((ButtonEx)this.List[this.List.Count - 1]);
int newLeft = lastButton.Left + lastButton.Width;
btn = getButton(ButtonName, 25, 50, 0, newLeft);
}
else
{
btn = getButton(ButtonName, 25, 50, 0, 0);
}
add(btn);
}
public void Add(string ButtonName, int Height, int Width)
{
ButtonEx btn = getButton(ButtonName, Height, Width, 0, 0);
add(btn);
}
public void Add(string ButtonName, int Height, int Width, int Top, int Left)
{
ButtonEx btn = getButton(ButtonName, Height, Width, Top, Left);
add(btn);
}
private void add(ButtonEx btn)
{
parent.Controls.Add(btn);
this.List.Add(btn);
}
protected void btn_Click(object sender, System.EventArgs e)
{
MessageBox.Show(((ButtonEx)sender).Text);
}
private ButtonEx getButton(string text, int height, int width, int top, int left)
{
ButtonEx btn = new ButtonEx();
btn.Text = text;
btn.Height = height;
btn.Width = width;
btn.Top = top;
btn.Left = left;
btn.Click += new System.EventHandler(btn_Click);
return btn;
}
}
}
This enables the user of your class to use the notation:Code:public ButtonEx this [int index]
{
get { return (ButtonEx)this.List[index]; }
}
Code:myPanel.Items.Add("Chris");
MessageBox.Show (myPanel.Items[0].Status);
MessageBox.Show (myPanel.Items[0].Progress);
in IDE it looks ok but when in run-time it gives me the following error:
An unhandled exception of type 'System.InvalidCastException' occurred in deviantion.exePHP Code:public devSingleItem this[int index]
{
get
{
if (index > -1 && _array.Count - 1 >= index)
{
return (devSingleItem)_array[index];
}
else
{
throw (new ArgumentOutOfRangeException("Error",null,"The given index was out of bounds"));
}
}
}
Additional information: Specified cast is not valid.
:confused:
hmmm...how do you declare "_array"?
array:PHP Code:using System;
using System.Collections;
using System.Windows.Forms;
namespace deviation
{
public class devTab : System.Windows.Forms.TabPage
{
private devItems _items = null;
public devTab(string caption)
{
_items = new devItems(this);
this.Text = caption;
}
public devItems Items
{
get
{
return _items;
}
}
public class devItems
{
private devTab _parent = null;
private devOptions _options = null;
private ArrayList _array = new ArrayList();
public devItems(devTab instance)
{
_parent = instance;
_options = new devOptions(this);
}
public devSingleItem this[int index]
{
get
{
if (index > -1 && _array.Count - 1 >= index)
{
return (devSingleItem)_array[index];
}
else
{
throw (new ArgumentOutOfRangeException("Error",null,"The given index was out of bounds"));
}
}
}
public devOptions Options
{
get
{
return _options;
}
}
/// <summary>
/// Adds an item to the devTab
/// </summary>
/// <param name="url">The url of the picture to be downloaded</param>
/// <param name="buttonWidth"></param>
/// <param name="buttonHeight"></param>
public void Add(string url, int buttonWidth, int buttonHeight)
{
_array.Add((string)url);
Button button = new Button();
_parent.Controls.Add(button);
}
public class devSingleItem
{
public string Status;
public string Progress;
public devSingleItem()
{
Status = "Some Status";
Progress = "Some Progress";
}
}
public class devOptions
{
private devItems _parent = null;
private int _numberOfPictures;
private int _pictureWidth;
private int _pictureHeight;
private int _spaceWidthBetweenPictures;
private int _spaceHeightBetweenPictures;
private bool _canDownload = true;
/// <summary>
/// Gets the number of pictures in the current devTab
/// </summary>
public int numberOfPictures
{
get
{
return _numberOfPictures;
}
set
{
_numberOfPictures = value;
}
}
/// <summary>
/// Gets / Sets the width size in pixels of the pictures in the current devTab
/// </summary>
public int pictureWidth
{
get
{
return _pictureWidth;
}
set
{
_pictureWidth = value;
}
}
/// <summary>
/// Gets / Sets the height size in pixels of the pictures in the current devTab
/// </summary>
public int pictureHeight
{
get
{
return _pictureHeight;
}
set
{
_pictureHeight = value;
}
}
/// <summary>
/// Gets / Sets the width size in pixels of the space between pictures
/// in the current devTab
/// </summary>
public int spaceWidthBetweenPictures
{
get
{
return _spaceWidthBetweenPictures;
}
set
{
_spaceWidthBetweenPictures = value;
}
}
/// <summary>
/// Gets / Sets the height size in pixels of the space between pictures
/// in the current devTab
/// </summary>
public int spaceHeightBetweenPictures
{
get
{
return _spaceHeightBetweenPictures;
}
set
{
_spaceHeightBetweenPictures = value;
}
}
/// <summary>
/// Defines if the pictures in the current devTab can be downloaded
/// into the buttons
/// </summary>
public bool canDownload
{
get
{
return _canDownload;
}
set
{
_canDownload = value;
}
}
public devOptions(devItems instance)
{
_parent = instance;
}
}
}
}
}
PHP Code:private ArrayList _array = new ArrayList();
It looks like your ArrayList stores stuff other than of type devSingleItem, so you possibly you're trying to cast a String type stored in your ArrayList as type devSingleItem which will throw an exception. Make sure you're only storing items of type devSingleItem in your ArrayList. I think this is the guy that's saving your string:
i get the feeling this is probably going to mess things up a bit for ya...:(VB Code:
public void Add(string url, int buttonWidth, int buttonHeight) { [b]_array.Add((string)url);[/b] Button button = new Button(); _parent.Controls.Add(button); }
i removed the (string) casting and still givin me error =\
ah now i re-read ur post and i think i understood...just let me thing a bit..lool
well...i removed all the things that might cause trouble because of their data type and still the bug
well, the problem is what's getting stored in your internal collection, not the cast. In your code url is string to begin with, so no cast would be necessary anyway. The problem is that you're storing something other than a devSingleItem in the ArrayList. Then when your indexer code runs it tries to cast an item in your ArrayList, which is a string type, to a devSingleItem type and that won't work. In my example I only store ButtonEx types in my internal collection so when my indexer is referenced it casts an item in my internal collection(which I already know only holds ButtonEx types) to a ButtonEx type, and all is good. But if I allow anything other than ButtonEx types to be added to my internal collection, my indexer would break as well. So the solution is gonna be something like adding only devSingleItems to your internal collection...
no...the arraylist has nothing there now..i changed the code and has nothing and still the error..
DAMN WORKED LOL..! it was the string..i first tryed casting string into object..but didnt work..well..whatever u god tkstkstks tks tks tks :D:D:D
so repost what your code looks like now.
worked =P
so now what i have to do is always i do an class.items.add() it creates a new devsintgleItem with all it's propreties set up and runnning very tks again :DPHP Code:public void Add(string url, int buttonWidth, int buttonHeight)
{
_array.Add(new devSingleItem());
}
its kinda hard to start working like this i am not used to work with oop like this..but seems very nice :D good night time to sleep