Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Constructor and reference question

Status
Not open for further replies.

majkinetor

Programmer
Feb 21, 2006
88
RS
Hello

I have two questions.

1. Is it possible to call base constructor in the body of the current constructor?
I want code like this:

class MyClass
{
public MyClass()
{
// some code here
base();
}
}

2. I don't understand this code. I have an object that returns reference to the DataTable it contains. I am wondering how come that in following code t variable remains valid:

void MyClass()
{
DataTable t;

void SomeFunc(){
using ( ObjectWithTable owt = new ObjectWithTable() )
{
t = owt.Table;
}

}
}

The GC will free the owt as soon as it exits the using statement, but I can still access its table over t.

Thanks.
 
Hi there!

I'm not following the idea on point 1.
base() is the contructor for what? Is it for another object?

On point 2, the owt is released as soon the using statement ends, because it's defined inside the using statement. Kind of like the for statement. If you have for (int i=0; i<1000;i++) the variable i will be lost as soon the for statement ends.

However, the t object is a class object, and as so, when you have t= owt.Table, you are actually cloning the reference to the table. As so, this reference will remain valid, until you explicitly dispose of her.


Hope to be of some help!
Be well and good luck.
 
calling a base ctor
Code:
public class Foo : Bar
{
   public Foo() : base()
   {
       //initialize foo after base
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hello Canelas and thx for answering.

1. base() is constructor of the base class, I didn't write completely

class MyClass : BaseClass
{
public MyClass()
{
// some code here
base();
}
}
Currently, the only method to rise the constructor is:

public MyClass() : base()
{
// some code here
}

... but that doesn't produce the same effect as what I want as base constructor is called BEFORE the current one, not AFTER or on place where i want it. Calling base constructor anywhere in the code of the current constructor is something that other OO languages support. I also read that dotNet supports it but C# doesn't...

2. So, I can't get "outdated" reference in C# like I could in for instance C++? Or there are cases where code similar to above produces invalid reference later ?

Thx.
 
Just to clarify things more about why do I want base constructor after current one.

My base class is abstract it defines some abstract properties and some non abstract methods that use those properties. Constructor calls some of those methods which expect properties to be set up. But base class doesn't set them up, instead, derived class set them up in their constructor.

Yes this case can be solved by designing things differently, but that is not the question here. I like initialization of abstract properties to stay in derived class as they naturally belong there
 
you cannot fire the base ctor after the subclassed ctor.
If your abstract ctor references objects which must be set, then they need to be injected into the ctor.
Code:
public class Foo
{
   public Foo(IDependency dependency) : base(dependency)
   {
   }

   public Foo() : base(new DefaultDependency())
   {
   }
}

public abstract class Bar
{
   private IDependency dependency;
   protected Bar(IDependency dependency)
   {
       this.dependency = dependency;
       Init();
   }

   private void Init()
   {
       dependency.DoSomething();
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I am not sure that t will be copied. I did this test:

class Test :IDisposable
{
public int x;
public Test(int x){ this.x = x ;}

public void Dispose() { this.x = 0; }
}



Test t2;
using (Test t1 = new Test(1))
{
t1.x = 5;
t2 = t1;
}

After the using finishes t2.x = 0 which means that x object wasn't cloned...
 
2. So, I can't get "outdated" reference in C# like I could in for instance C++? Or there are cases where code similar to above produces invalid reference later ?

I don't think that this could happen. However there are two things that could happen, depending on the objects.

Either, you pass the reference to the object (this is your case) and in this case you have two objects referencing the same object (e.g. two datasources that manipulate the same data at the same time. If you delete a record, the record becomes unreachable in both objects).

Or you clone the object, and in this case you have two different objects with independent data (e.g. two tables with the same data, but where one doesn't affect the other. If you delete a record, it only gets deleted in one place).

However, getting outdated refence... i don't remember any scenario at the moment.
 
I would recommend the book CLR via C#. this provides the in and outs of the c# language.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top