views:

32

answers:

3

Hello everybody

In below example, i defined number field. This field will work as i wanted but it is not enough efficient to provide my expectations.

number value is fixed value for each class,number is not dependent instances and number support polymorphism. How can i do that ? Or is there another solution for not use unneccesary number field for instances ?

abstract class Main
{
    public int number;
    public virtual void dostuff(){
        int x = number;
    }
}
class Derived:Main
{

    public ovverride void dostuff(){
        int x = number;
    }
}
A: 

You could make the property static and then add it to each class:

   abstract class Main
{
    public static int number;
    public virtual void dostuff(){
        int x = Main.number;
    }
}
class Derived : Main
{
    public static int number;
    public overide void dostuff(){
        int x = Derived.number;
    }
}

Edit: I am a bit confused by your comments about polymorhism so i have added some more examples.

Main obj = new Derived();
obj.doStuff(); //This will use Derived.number; as doStuff is and overidden virtual method.

However if you do the following:

abstract class Main
{
    public static int number;
    public void dostuff(){
        int x = Main.number;
    }
}
class Derived : Main
{
    public static int number;
    public new void dostuff(){
        int x = Derived.number;
    }
}

Then you get different behaviour as below:

Main obj = new Derived();
obj.doStuff() // Will use Main.number
Derived obj2 = (Derived)obj;
obj2.doStuff() // Will use Derived.number

If you want some other kind of behaviour i havn't defined here please exaplin because i do not understand what you want.

Ben Robinson
It's not required to duplicate number in Derived - the derived class can access Main.number just by saying `number`.
Mark H
I edited question so can check it pls ? I forgot to define method as virtual.
Freshblood
He said it should be different for each class, but not per instance. This will do that.
Ben Robinson
@Ben Robinson, If Derived does not override dostuff method so base class field will work on base class method. So static field of Derived is not used in that case.
Freshblood
If derived does not overide do stuff method but simply hides it, it will call the method of whichever class your instance is cast to. IF it simply does not define a dostuff method it will obviously always call the base method. I am slightly confused about what you are trying to do.
Ben Robinson
In my example, i will create millions number field unneccesary so i am trying to avoid it.
Freshblood
@Freshblood - that's what the `static` keyword solves. Oh, and in Derived, you should put the `new` keyword on number. I thought it looked ugly as it is. Even better, it should be private in both classes, or do you have a good reason for making it public?
Mark H
Then my example will work. You will have one number field per class and not per instance. If you want derived to have a different number to main then follow my example. If you want all main and all derived classes to have the same number simply remove the number field and do stuff method from the derived classes.
Ben Robinson
@Mark, I presume it is public so he can set it at run time.
Ben Robinson
@Ben Robinson But there is a problem in your example for me.Question asked generaly so think that if a method which does not override base class methods cannot use his own field in base class methods.
Freshblood
@Mark H if i put new keyword so i will lose polymorphism.
Freshblood
@Freshblood, adding the new keyword will not make any difference in practice, it will simply remove a compiler warning.Obviously English is not you're first language, i'm afraid I am not sure what you mean by "if a method which does not override base class methods cannot use his own field in base class methods" Please provide an example of a situation that demonstrates what cannot be acheived using my example.
Ben Robinson
@Ben Robinson firstly u used static field in each class instances directly.this is not what i wanted. If u write number instead of Main.number or Derived number in methods then remove Derived dostuff method and now if u create instance of Derived and using it via Main obj variable. When u call do stuff method like obj.dostuff() x gets base class static field.My expectation is x gets derived class value. I think it is not possible to do that with static keyword. Static keyword cause losing polymorphism.
Freshblood
Static field help us to have instance independent fields but it doesn't work in polimorphism.Do u understand what i mean ? Yes, unfortunality my main language is not english.
Freshblood
Sorry no i don't understand, i have updated my answer i hope it helps.
Ben Robinson
A: 

static int number

Mark H
A: 

You could just make the number a property and initialise is in each class constructor:

abstract class Main
{
    public int number{get; private set;}

    public void dostuff(){
        int x = number;
    }
}

class Derived:Main
{
    public Derived()
    {
        number = 5; // Specific value for each derived class
    }
    public void dostuff(){
        int x = number;
    }
}

Looks like I got the wrong end of the stick -- you want to be able to set it statically per class type, which has already been answered.

Dr Herbie