Just want to see if this is 'by design' or a bug...
I have a common List<T> defined in a base class, and the base class has a
static property to expose this list.
I wanted the derived class to add items to this list, but have the base
class hold the data and expose the property.
Problem is, however, that the derived class' static constructor doesn't get
called when DerivedClass.MyList is accessed, so the list has no members. If
I move the MyList property up to DerivedClass, it works as expected and
DerivedClass' static constructor is invoked.
I can understand why the BaseClass static constructor is called, but it
would also seem to make sense that DerivedClass' static constructor would be
called when an inherited static member is accessed/invoked..
Thoughts?
Thanks,
Kirk
public class DerivedClass : BaseClass
{
static DerivedClass
{
_list.Add("Foo");
}
[...]
}
public class BaseClass
{
protected static List<string> _list = new List<string>();
public static List<string> MyList
{
return _list;
}
static BaseClass()
{
}
[...]
} 3 11805
The static constructor is just another method. So why would calling one
static method (MyList) cause another static method (derived class ctor) to
be invoked? There is no logical connection between those two. If you want to
add items to your list, you'll have to create a method to do it. (A static
base class method.)
--
kevin aubuchon www.aubuchon-design.com
"Kirk Marple" <Kirk Ma****@discussions.microsoft.com> wrote in message
news:A9**********************************@microsof t.com... Just want to see if this is 'by design' or a bug...
I have a common List<T> defined in a base class, and the base class has a static property to expose this list.
I wanted the derived class to add items to this list, but have the base class hold the data and expose the property.
Problem is, however, that the derived class' static constructor doesn't
get called when DerivedClass.MyList is accessed, so the list has no members.
If I move the MyList property up to DerivedClass, it works as expected and DerivedClass' static constructor is invoked.
I can understand why the BaseClass static constructor is called, but it would also seem to make sense that DerivedClass' static constructor would
be called when an inherited static member is accessed/invoked..
Thoughts?
Thanks, Kirk
public class DerivedClass : BaseClass { static DerivedClass { _list.Add("Foo"); }
[...] }
public class BaseClass { protected static List<string> _list = new List<string>(); public static List<string> MyList { return _list; }
static BaseClass() { }
[...] }
From the CLI guidelines:
<<
The static constructor for a class executes at most once in a given
application domain. The execution of a static constructor is triggered by the
first of the following events to occur within an application domain:
An instance of the class is created.
Any of the static members of the class are referenced.
<<
If BaseClass is an abstract class, then it might make sense that the derived
class' constructor would be invoked when any of the static members of either
class (derived or base) are referenced.
Basically, I wasn't sure if the inheritance chain affected the invocation
semantics of static constructors.
Kirk
"Kevin Aubuchon" wrote: The static constructor is just another method. So why would calling one static method (MyList) cause another static method (derived class ctor) to be invoked? There is no logical connection between those two. If you want to add items to your list, you'll have to create a method to do it. (A static base class method.)
-- kevin aubuchon www.aubuchon-design.com
"Kirk Marple" <Kirk Ma****@discussions.microsoft.com> wrote in message news:A9**********************************@microsof t.com... Just want to see if this is 'by design' or a bug...
I have a common List<T> defined in a base class, and the base class has a static property to expose this list.
I wanted the derived class to add items to this list, but have the base class hold the data and expose the property.
Problem is, however, that the derived class' static constructor doesn't get called when DerivedClass.MyList is accessed, so the list has no members. If I move the MyList property up to DerivedClass, it works as expected and DerivedClass' static constructor is invoked.
I can understand why the BaseClass static constructor is called, but it would also seem to make sense that DerivedClass' static constructor would be called when an inherited static member is accessed/invoked..
Thoughts?
Thanks, Kirk
public class DerivedClass : BaseClass { static DerivedClass { _list.Add("Foo"); }
[...] }
public class BaseClass { protected static List<string> _list = new List<string>(); public static List<string> MyList { return _list; }
static BaseClass() { }
[...] }
Sounds correct. Technically static members are not inherited at all,
but C# and the IDE provide the illusion of inheritance. When you call
DerivedClass.BaseStaticMember the compiler will generate IL for
BaseClass.BaseStaticMember.
--
Scott http://www.OdeToCode.com/blogs/scott/
On Sun, 28 Nov 2004 17:49:07 -0800, "Kirk Marple" <Kirk Ma****@discussions.microsoft.com> wrote: Just want to see if this is 'by design' or a bug...
I have a common List<T> defined in a base class, and the base class has a static property to expose this list.
I wanted the derived class to add items to this list, but have the base class hold the data and expose the property.
Problem is, however, that the derived class' static constructor doesn't get called when DerivedClass.MyList is accessed, so the list has no members. If I move the MyList property up to DerivedClass, it works as expected and DerivedClass' static constructor is invoked.
I can understand why the BaseClass static constructor is called, but it would also seem to make sense that DerivedClass' static constructor would be called when an inherited static member is accessed/invoked..
Thoughts?
Thanks, Kirk
public class DerivedClass : BaseClass { static DerivedClass { _list.Add("Foo"); }
[...] }
public class BaseClass { protected static List<string> _list = new List<string>(); public static List<string> MyList { return _list; }
static BaseClass() { }
[...] } This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ernst Murnleitner |
last post by:
Hello Readers,
Is there a way that only one class can construct a class A and its inherited
classes A2, A3 etc.?
I want to construct a class A (and the inherited classes A2, A3 etc.) from a...
|
by: cppaddict |
last post by:
Hi,
I know that it is illegal in C++ to have a static pure virtual method,
but it seems something like this would be useful when the following 2
conditions hold:
1. You know that every one...
|
by: Elia Karagiannis |
last post by:
The static constructor of a derived class never gets called if it has no
other methods and the base class is only static
I have the following base class and derived class.
public class MyBase...
|
by: Hemanth |
last post by:
Hi,
I have a base class with a static constructor and some abstract methods.
Derived classes implement these methods. From articles on the web, it appears
that there is no guarentee that this...
|
by: none |
last post by:
I'd like to create a new static property in a class "hiding" the
property present in a base class. Since this needs to happen at runtime
I tried doing this via DynamicMethod. But obviously the...
| |
by: Per Bull Holmen |
last post by:
Hey
Im new to c++, so bear with me. I'm used to other OO languages, where it
is possible to have class-level initialization functions, that
initialize the CLASS rather than an instance of it....
|
by: Sandro Bosio |
last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long.
I'm trying to achieve the...
|
by: softwaredoug |
last post by:
Here is some test code I've been attempting to compile (Visual Studio
2003)
test.h:
class Base
{
protected:
Base() {}
public:
|
by: SimonDotException |
last post by:
I've written an abstract base type which uses generics to provide XML serialization and deserialization methods for use by its derived types, but I'm seemingly unable to write it in a way which...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |