[lil explanation]

04/14/2012 15:51 DyjgK64J451Jhv0#1
well simply i need someone to explain more about this for me
Code:
void AccessAll(Control.ControlCollection cc)
	{
		foreach (Control c in CC)
		{
			if (c is CheckBox)
			{
				CheckBox ch = c as CheckBox;
				ch.Checked = true;
			}
			if (c.HasChildren) AccessAll(c.Controls);
		}
	}
umm it's a method who takes argument of control type , for each loop to get all the controls in cc , c is umm instance from cc ?
anyway the part i cant understand over here is this one
Quote:
CheckBox ch = c as CheckBox;
he create an instance(ch) of an instance(c) as the instance c (of controls) wont have the property checked ?
c as checkbox ?
sorry my first time to see this , thanks in advance :(

edit : same goes for umm "Button b = c as Button;" as c isnt a button , it's an instance of cc , so we create an instance b from buttons with type c which it's type is cc which is control ? wdf o-0 lol
04/14/2012 16:01 pro4never#2
Ok... You seem slightly confused but I'll go ahead and explain from basic to more complex/specific terms...

#1: Everything* in C# is an object. That is why it is an object oriented programming language and that's how you should approach it...

#2: This method (AccessAll) accepts a single object of type "ControlCollection". This means it can be ANY type of controls grouped into a collection (think of a list of buttons, inputs, toggles, checkboxes, etcetc)

#3: You then iterate through each control in this collection (collection = CC)

#4: You check the type of each object iteration (each iteration = c)

#5: IF the object type is CheckBox, you cast the data to that type so you can now deal with it as a non generic object (or generic control object in this case)

Note: You are not creating a new instance, you are simply taking existing data and treating it as a different object type. Think of this like doing (int)number to force it to conform to the int data type.

You then are using iteration to deal with the fact that ControlCollections can contain OTHER ControlCollections which you would then need to iterate through (as many levels deep as required)

As for your final question.... When you modify ch, it modifies the original collection object because you are not creating a new object, simply modifying the one that already exists.


It's like saying...


Player user = Kernel.Clients[UID];
user.Name = "new Name";

If you now check Kernel.Clients[UID].Name it will be the same as user.Name
04/14/2012 17:09 DyjgK64J451Jhv0#3
first of all thanks for replying :)
well about
if (c is CheckBox)
{
CheckBox ch = c as CheckBox;
ch.Checked = true;
}
u said that u create an object ch of c so u can apply changes on cuz other c may be another control and not checkbox ? or because c isnt a checkbox object and .checked property is a property of checkbox ? i guess the second one , well the most confusing part for me is "checkbox ch = c as checkbox" , why he said "c as checkbox" ?

maybe cuz u cant do the 2nd one ?
checkbox ch = new checkbox >> 1
checkbox ch = c control >> 2
checkbox ch = c as checkbox >> 3

guess ill go to read more about "as" , thanks for replying ^^
04/14/2012 17:18 Korvacs#4
Basically because when your itterating through the CC collection here:

Code:
foreach (Control c in CC)
c there is actually a Control object, however the Control object is just a base class that all Controls are built upon and expand. So the Checkbox is a type of Control, and what your doing with c as CheckBox is allowing you to access all the Checkbox specific methods, properties etc.

Thats the none technical explanation atleast.
04/14/2012 17:38 pro4never#5
Quote:
Originally Posted by DyjgK64J451Jhv0 View Post
first of all thanks for replying :)
well about
if (c is CheckBox)
{
CheckBox ch = c as CheckBox;
ch.Checked = true;
}
u said that u create an object ch of c so u can apply changes on cuz other c may be another control and not checkbox ? or because c isnt a checkbox object and .checked property is a property of checkbox ? i guess the second one , well the most confusing part for me is "checkbox ch = c as checkbox" , why he said "c as checkbox" ?

maybe cuz u cant do the 2nd one ?
checkbox ch = new checkbox >> 1
checkbox ch = c control >> 2
checkbox ch = c as checkbox >> 3

guess ill go to read more about "as" , thanks for replying ^^
The As keyword is basically just casting between object types.


It's like saying....

Foreach(Fruit fr in Bowl)
{
if(fr is Apple)
{
Apple _apl = fr as Apple;
_apl.RemoveSeeds();
}
}


Fruit is a base type of object... (all apples are fruit but not all fruits are apples) which other object types build upon. Apple is a more specific object type which can have its own methods and variables (in addition to the basic ones assigned to all fruits). We can say 'ok we know this fruit is an apple... cause we checked, so lets treat it as an apple and then do what we want with it'.

Yay generic explanations!
04/14/2012 19:15 DyjgK64J451Jhv0#6
Quote:
Originally Posted by pro4never View Post
The As keyword is basically just casting between object types.


It's like saying....

Foreach(Fruit fr in Bowl)
{
if(fr is Apple)
{
Apple _apl = fr as Apple;
_apl.RemoveSeeds();
}
}


Fruit is a base type of object... (all apples are fruit but not all fruits are apples) which other object types build upon. Apple is a more specific object type which can have its own methods and variables (in addition to the basic ones assigned to all fruits). We can say 'ok we know this fruit is an apple... cause we checked, so lets treat it as an apple and then do what we want with it'.

Yay generic explanations!
i want an apple ! :D
04/14/2012 20:53 pro4never#7
I feel like I should have gone with...

PieManager.MakePie(_apl);

But then I'd need some other ingredients for the method. :D
04/14/2012 22:26 InfamousNoone#8
Quote:
Originally Posted by pro4never View Post
The As keyword is basically just casting between object types.


It's like saying....

Foreach(Fruit fr in Bowl)
{
if(fr is Apple)
{
Apple _apl = fr as Apple;
_apl.RemoveSeeds();
}
}


Fruit is a base type of object... (all apples are fruit but not all fruits are apples) which other object types build upon. Apple is a more specific object type which can have its own methods and variables (in addition to the basic ones assigned to all fruits). We can say 'ok we know this fruit is an apple... cause we checked, so lets treat it as an apple and then do what we want with it'.

Yay generic explanations!
This is the incorrect usage of the as keyword.
The purpose of the is keyword is to check type-safety for regular type-casting, for instance:

Code:
int num = 1337;
object obj = num;
if (obj is long)
{
	long n = (long)obj;
	// ...
}
However, checking type-safety does not have to be done with the as keyword. If an object is not of the type you are casting to null is returned, and no error will be thrown.

Code:
int num = 1337;
object obj = num;
string s = obj as string;
if (s != null)
{
	// ...
}
04/15/2012 02:41 Chalkie#9
I like apples.
04/15/2012 08:04 pro4never#10
Quote:
Originally Posted by InfamousNoone View Post
This is the incorrect usage of the as keyword.
The purpose of the is keyword is to check type-safety for regular type-casting, for instance:

Code:
int num = 1337;
object obj = num;
if (obj is long)
{
	long n = (long)obj;
	// ...
}
However, checking type-safety does not have to be done with the as keyword. If an object is not of the type you are casting to null is returned, and no error will be thrown.

Code:
int num = 1337;
object obj = num;
string s = obj as string;
if (s != null)
{
	// ...
}

I'm well aware that if you use as on an incorrect data type you'll receive a null result... I was trying to dumb it down as best I could while at work.

I apologize for any missused terms or mistakes in the process.