Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract variable/property? C#

Tags:

c#

I've read some on properties and i'm not sure if thats what i want or not. Basically i have an abstract projectile class so that all "bullets" have a common implementation That any "weapon" they are attached to can expect to be able to use.

I know you can declare an abstract class and force the functions you declare inside it to be defined. I'd like the same thing with the variables (i think, obviously this functionality doesn't seem to exist so perhaps i'm thinking about the solution wrong)

My issue is that since all "bullets" should have a damage i'd like to be forced to declare in code the damage value of a bullet.

There may be a case where the round is from a stun gun and it does no damage but I feel that I should still be made to declare it for 2 reasons.

  1. The explicit declaration in the code of this does zero damage is worth the one line of code. You don't have to go well I guess it does none since it says nothing about damage it's explicitly stated.

  2. Debugging (this is the main reason the other is minor) I want to be forced, so I don't forget. I don't want to mistype the variable name or forget to assign a value altogether and hunt for a half hour trying to figure out why my rocket or missile or bullet or whatever isn't doing any damage or is doing exactly 1 or the amount of the last projectile I used or whatever the default value of the float variable I declared in the abstract class ends up as. I want to be thrown an error right away telling me I can't continue until my new bullet has a damage.

like image 270
sparkzbarca Avatar asked Apr 27 '13 01:04

sparkzbarca


People also ask

What are the properties of abstract?

Abstract Method/PropertiesAn abstract method/property has an implementation in a derived class. An abstract method/property can't be static. An abstract method/property is implicitly a virtual method/property. An abstract method/property is implemented in a derived class using the override keyword.

Can we use abstract for variable?

No you cannot. When you declare a variable you declare an instance of the class/struct you specified. Abstract classes cannot be directly instantiated.

What is abstract property in C#?

An implementation of the property accessors will not be provided by an abstract property declaration. Let us see how to learn how to work with abstract properties. Here we have an abstract class Shape with two derived classes: Square and Circle. Here, we have an abstract Area property.

Can abstract class have variables C#?

In an abstract class we can use constant and readonly variable such as simple variable.


1 Answers

Properties are what you're looking for. on an interface you would just do something like this:

public interface IProjectile 
{
    string Name { get; }

    int Damage { get; }

    void Fire();
}

Only the get method has to be defined on the interface because you only need the consumer of the interface to read from the damage property, and you'd prefer to not allow the consumer to write the damage value.

The implementation would be something like this:

public class Bullet : IProjectile 
{
    public string Name { get { return "Bullet"; } }
    public string Damage { get { return 5; } }

    public void Fire() 
    {
        Console.WriteLine("Did {0} damage.",Damage);
    }    
}
like image 164
nathan gonzalez Avatar answered Oct 21 '22 04:10

nathan gonzalez