Why does a class definition in one cell not recognize another class’s fields made in another cell?

I used a Visual Studio Code’s Polyglot notebook and wrote the following in 3 separate cells, in the order indicated below.

Cell 1:

class Z;

Cell 2:

class A
{
    public static void f()
    {
        Console.WriteLine(Z.x);
    }
}

Cell 3:

class Z: A
{
    public static int x = 42;
}

I then executed the cells in order, and got the following error message.

'Z' does not contain a definition for 'x'

My original question was:

Is it possible, in C#, to write a (possibly abstract) class A that has a (possibly static) function with signature void f(B b), whose parameter’s type B is a (possibly abstract) class that derives from A (class B: A {...})? If not, why not?

and it was titled Can a C# class use a derived class?

I accepted the answer below based on this formulation. This formulation was later deleted by another user who edited my question, and changed its title.

16

The notebook executes the cells one by one. So when you execute cell 2, Z is nothing but a class, without any notion of x. That’s why you get the error.

It’s not clear what behaviour you actually want. Of course you can just write the entire code into a single cell:

class A
{
    public static void f()
    {
        Console.WriteLine(Z.x);
    }
}    
class Z: A
{
    public static int x = 42;
}

So your problem is not related to inheritance, but to how Polyglot executes the cells. Be aware, though, that your approach is pretty strange. A base-class should never make any assumptions about derived classes, this it it shouldn’t even know that there exist any derived classes in the first place. However that’s more of a conceptual problem, which is way too broad for this questions scope.

1

Yes, it is possible.

Here’s an example that follows exactly what you requested:

using System;

class A
{
    public void f(B b)
    {
        Console.WriteLine("in A.f.  b.Prop=" + b.Prop.ToString());
    }
}

class B : A
{
    public int Prop { get; set; } = 0;
}

class Program
{
    static void Main(string[] args)
    {
        B b1 = new B();
        b1.Prop = 1;
        B b2 = new B();
        b2.Prop = 2;
        b1.f(b2);
        A a = new A();
        a.f(b1);
    }
}

Output:

in A.f.  b.Prop=2
in A.f.  b.Prop=1

Live demo

Note:
Since you asked, A and/or B can also be abstract.


Update:
The questiom was editted after this answer was given, with additional details about the usage of Polyglot notebook.
Regarding this additional information: the problem is related to the way the notebook executes the cells one by one. It is not an issue related to C# in general or the usage of inheritance in particular.

Yes, sure. This is fully valid:

class A
{
   public B GetDerived() => new B();
}

class B : A
{
   // ...
}

Of course, the two classes need to be in the same assembly, because they need to know each other. The other question is for a relevant use case, which I find hard to imagine. Normally, if A is abstract, you would return A’s (which then need to be derived types of A), and not B’s, since that prevents you from later creating a class C that derives from A and which would replace B.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật