|
-
Nov 4th, 2002, 06:04 PM
#1
Thread Starter
yay gay
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?
-
Nov 4th, 2002, 06:06 PM
#2
Thread Starter
yay gay
btw the book name is Beginning Visual C# its a very good book from Karli Watson..
-
Nov 4th, 2002, 06:13 PM
#3
PowerPoster
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)");
}
-
Nov 4th, 2002, 06:14 PM
#4
Thread Starter
yay gay
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...
-
Nov 4th, 2002, 06:22 PM
#5
Thread Starter
yay gay
i know to use constructors but this is getting me really really confused damn i want to understand thiss!!! lol!
-
Nov 4th, 2002, 06:24 PM
#6
Thread Starter
yay gay
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?
-
Nov 4th, 2002, 06:33 PM
#7
Frenzied Member
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).
Dont gain the world and lose your soul
-
Nov 4th, 2002, 06:41 PM
#8
Hyperactive Member
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?
-scott
he he he
-
Nov 4th, 2002, 06:42 PM
#9
-
Nov 4th, 2002, 06:48 PM
#10
-
Nov 4th, 2002, 06:57 PM
#11
Thread Starter
yay gay
yeah much tks but i tryed base(i) and didnt work lol..
-
Nov 4th, 2002, 06:58 PM
#12
Thread Starter
yay gay
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);
}
-
Nov 4th, 2002, 07:01 PM
#13
Thread Starter
yay gay
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 . . .
-
Nov 4th, 2002, 07:16 PM
#14
PowerPoster
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.
-
Nov 4th, 2002, 07:31 PM
#15
Thread Starter
yay gay
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
-
Nov 4th, 2002, 07:42 PM
#16
PowerPoster
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.
-
Nov 4th, 2002, 07:43 PM
#17
Thread Starter
yay gay
. . . bah! i am not getting a ****! ill go sleep tomorrow i see this again
-
Nov 5th, 2002, 02:53 PM
#18
Hyperactive Member
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);
}
-scott
he he he
-
Nov 5th, 2002, 04:57 PM
#19
Thread Starter
yay gay
but what is the advatage of making (int j) : base(j) ??? i am only writting extra code as the performance will be the same!
-
Nov 5th, 2002, 05:55 PM
#20
Hyperactive Member
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.
-scott
he he he
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|