List Info

Thread: C-Sharp (C#) Group: Re: calling base class constructor and current class default constructor




C-Sharp (C#) Group: Re: calling base class constructor and current class default constructor
user name
2006-09-27 09:25:42
Sokun..I think you want to call the B's default one and A's
parameterized one.
If this is requirement then NO..It's not possible. As you
need to pass
some parameter to your parent's (class A) constructor, you
can't send
it.
One more thing..Untill and unless your parent object is
instantiated,
your child will not be initialized.So you can't serve your
purpose by
the following code in B
class B:A
{
 public string childPoperty = "Sokun";
 B():base(childProperty) //default constructor - this won't
work
 {

 }
}

Only work around could be making your parent (A) constructor
static.
But it can't be parameterized. So you need to have a
property there.
Only the first time your application starts, it'll show the
value set
in the parent class.

public class Parent
	{
		public static string parentA = "arindam";
		static Parent()
		{
			MessageBox.Show("Parent default constructor: "
+ parentA);
		}

	}

public class Child:Parent
	{
		public Child()
		{
			parentA = "Default Child";
			MessageBox.Show("Child default constructor: " +
Environment.NewLine
+ parentA);
		}
		public Child(string childA)
		{
			parentA = "Param Child";
			MessageBox.Show("Child parameterized constructor:
" + childA +
Environment.NewLine + parentA);
		}
	}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )