TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Jeff Plummer
NA
2
0
How to declare a variable such that it exists once per class, kind of like static, but derived classes would also receive a new instances
Jul 27 2008 2:45 AM
I am looking for a way to declare a variable such that it exists once per class, kind of like static, but derived classes would also receive a new instances of the variable because they are different classes.
Example:
public class MyBaseClass
{
public static int i = 0;
}
public class MyDerivedClass : MyBaseClass
{
}
public class test
{
static void main (...)
{
MyBaseClass obj1 = new MyBaseClass();
MyDerivedClass obj2 = new MyDerivedClass();
obj2.i = 57; //Now obj1.i also equals 57 even though it is an instance of a different class (which I don't want)
}
}
In the example above I have two different objects of two different classes, and when I set the "i" value of one, it sets the "i" value of all. What I would like is for all instances of MyBaseClass to share an "i" value. And all instances of MyDerivedClass to share an "i" value. But instances of MyBaseClass do not share the "i" value with instances of MyDerivedClass.
The only thing I can think of is to make MyBase a generic (a template class), but that complicates things elsewhere. Any ideas?
Thanks,
Jeff Plummer
Reply
Answers (
2
)
Add Amount of Rows & Delete All Items in Rows
NP109 Correct or Not