1

Why this doesn't accept a parameter of a class-type? What can I read about it? Here is my useless code

using System;

public class Class1
{
  public int a = 5;
}

public class Class2
{
  private readonly int num;
  Class1 obj2 = new Class1();
  public Class2(Class1 obj)
  {
    num = obj.a;
  }
  public Class2(string l) : this (Class1 obj2)
  {

  }
}
1
  • 1
    I think you meant public Class2(string l) : this (obj2) Commented Dec 7, 2012 at 8:34

4 Answers 4

3

Your constructor takes an instance of Class1, so you should use:

public Class2(string l) : this (new Class1())
{
}

You will probably also want to assign the constructor argument to your obj2 member instead of creating a new one:

public class Class2
{
    private readonly int num;
    Class1 obj2;
    public Class2(Class1 obj)
    {
        this.obj2 = obj;
        num = obj.a;
    }
    public Class2(string l) : this (new Class1()) { }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Lee can you tell me what can i read about this? what's the name of the subject, how to ask the google?
@user1884807 - Calling one constructor from another is called 'constructor chaining' I believe.
still confused though (new Class1()) this instance is without a reference if i inderstand right, didn't know the instance could be without a reference, isn't that when GC shows up?
@user1884807 - It will be referenced by the obj parameter in the other constructor so it won't get collected.
2

First let's look at:

public Class2(string l) : this (Class1 obj2) {...}

Firstly, you don't include the type in an invocation, so it would be:

public Class2(string l) : this (obj2) {...}

But: obj2 is not valid at that point: the constructor (which kinda includes the field initializer) has not run yet, so fields on this (such as this.obj) are not legal at that location. as Lee notes: just pass in a null or new instance.

2 Comments

why i don't have to indicate the type of invocation here? it's true only for :this parameters?
@user1884807 it is the same as any method call: you don't call int i = Math.Min(int 1, int 3) - you call int i = Math.Min(1,3)
0

A class can inherit from another class. The this keyword refers to an instance of a class and instances do not exist at "develop-time"

Comments

0

You cannot use that string and pass it to another contructor that takes Class1 as argument. You need to create a Class1 object. But since that needs an int i would not use a constructor that takes string but int. Otherwise you would need to use int.Parse which could throw an excption and exceptional constructors are not good practise(when they could be avoided).

public Class2(int i)
    : this(new Class1() { a=i })
{

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.