-
constructors
PHP Code:
class Class1
{
static void Main(string[] args)
{
MyDerivedClass derived = new MyDerivedClass(4);
}
}
public class MyBaseClass
{
public MyBaseClass()
{
Console.WriteLine("MyBaseClass()");
}
public MyBaseClass(int i)
{
Console.WriteLine("MyBaseClass(int i)");
}
}
public class MyDerivedClass : MyBaseClass
{
public MyDerivedClass()
{
Console.WriteLine("MyDerivedClass()");
}
public MyDerivedClass(int i)
{
Console.WriteLine("MyDerivedClass(int i)");
}
public MyDerivedClass(int i, int j)
{
Console.WriteLine("MyDerivedClass(int i,int j)");
}
}
im reading a C# book who says that if i have a derived class from a inheritable class then when i use the derived class constructor it will try to use the base class constructor with the same parameters as the derived class..i tested..but it didnt work the way the book says!
the way i did returned:
MyBaseClass()
MyDerivedClass(int i)
when it should have returned:
MyBaseClass(int i)
MyDerivedClass(int i)
hmm...what is wrong?
-
btw the book name is Beginning Visual C# its a very good book from Karli Watson..
-
When you derive a class from a base class, if you do not explicity call the base class constructor, the runtime will make a call up the chain to the default New() constructor, which takes zero arguments. You could have done this:
Code:
public MyDerivedClass(int i) : base(i)
{
Console.WriteLine("MyDerivedClass(int i)");
}
-
also i am not seeing the advantage of using the base(i) here:
PHP Code:
class Class1
{
static void Main(string[] args)
{
MyDerivedClass derived = new MyDerivedClass(4,8);
}
}
public class MyBaseClass
{
public MyBaseClass()
{
Console.WriteLine("MyBaseClass()");
}
public MyBaseClass(int i)
{
Console.WriteLine("MyBaseClass(int i)");
}
}
public class MyDerivedClass : MyBaseClass
{
public MyDerivedClass()
{
Console.WriteLine("MyDerivedClass()");
}
public MyDerivedClass(int i)
{
Console.WriteLine("MyDerivedClass(int i)");
}
public MyDerivedClass(int i, int j) : base(i)
{
Console.WriteLine("MyDerivedClass(int i,int j) -> {0},{1}",i,j);
}
}
as the final result will be the same with or without the base..im confused...:confused:
-
i know to use constructors but this is getting me really really confused damn :confused: i want to understand thiss!!! lol!
-
ah sorry lethal didnt see ur post..hm...but what the : base(i) will do? i am not getting that at all...it forces the constructor(int i) to be used?
-
The base contructor will be called by default ,which is the one without the parameters. If you want to call a special contructor in the base class, thats when you would do base(i).
-
Basically, you can call any of the base constructors from your subclass constructors.
The : base(i) syntax allows you to specify which constructor to call, passing the appropriate parameter down to the base. This syntax is just as if you were creating a new instance of the base class.
If you don't do this, then the base constructor, base(), will always be called.
Does that help?
-
-
-
yeah much tks but i tryed base(i) and didnt work lol..
-
i mean...what is the advantage of doin that here:
PHP Code:
public MyDerivedClass(int i, int j)
{
Console.WriteLine("MyDerivedClass(int i,int j) -> {0},{1}",i,j);
}
-
PHP Code:
class Class1
{
static void Main(string[] args)
{
MyDerivedClass derived = new MyDerivedClass(3);
}
}
public class MyBaseClass
{
public MyBaseClass()
{
Console.WriteLine("MyBaseClass()");
}
public MyBaseClass(int i)
{
Console.WriteLine("MyBaseClass(int i)");
}
}
public class MyDerivedClass : MyBaseClass
{
public MyDerivedClass()
{
Console.WriteLine("MyDerivedClass() base(5) -> {0}");
}
// public MyDerivedClass(int i)
// {
// Console.WriteLine("MyDerivedClass(int i)");
// }
public MyDerivedClass(int i, int j)
{
Console.WriteLine("MyDerivedClass(int i,int j) -> {0},{1}",i,j);
}
public MyDerivedClass(int j) : base(i)
{
Console.WriteLine("MyDerivedClass(int j) : base(i) -> i={0} j={0}",i,j);
}
}
doesnt work bcoz he says base(i) doesnt exist . . .
-
B/c you named the variable 'j' in your constructor. All you are doing is passing the variable up the chain to the constructor you are explicitly invoking.
-
if i have up in the baseClass a constructor(int i) and a derivedClass constructor constructor(int i, string s) : base(j) (or base i?)what is the advantage of using base if the only thing im doin is writting more chars in code? lol
-
Ok..You are calling the base class constructor that takes a parameter. Constructors are not inherited when you derive a class. So, your derived class constructor has 'int i' as one of its arguments. You need to pass the i paramater up to the base constructor, for initializtaion. Let's say for instance your base class didn't have a constructor that takes no arguments. If you didn't explicitly make this call from your derived class, the CLR would raise an exception.
-
. . . bah! i am not getting a ****! ill go sleep tomorrow i see this again :D
-
Like Lethal said, you had some bugs in your last constructor. Try the following and let us know if it's working (it should work). Also note, because of what you're doing, i will equal j...
PHP Code:
public MyDerivedClass(int j) : base(j)
{
Console.WriteLine("MyDerivedClass(int j) : base(i) -> i={0} j={1}",i,j);
}
-
but what is the advatage of making (int j) : base(j) ??? i am only writting extra code as the performance will be the same!
-
This is the only way that you can execute the base constructor.
In this example, there's not really a good reason to call the base constructor because it doesn't do anything significant.
But, imagine you are now inheriting the functionality from an array list. If you had a new constructor that took the size of the list and a new property of your class, name, then you could (should) call the base constructor to initialize the array list:
PHP Code:
public class NamedList : ArrayList
{
private String listName;
public NamedList(int Size, String Name) : base(Size)
{
listName = Name;
}
}
This then becomes favorable to accessing the base class directly to intialize the size etc.