Public Group

# Simple Class Problem

This topic is 2526 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

 class Hero { public int health, strength, weaponStrength, defence; public bool isAlive; public string identity; public static void Good Guy(Hero hero) { hero.health = 25; hero.strength = 6; hero.weaponStrength = 15; hero.defence = 7; hero.isAlive = true; hero.identity = "Good Guy"; } } class Nemisis { public int health, strength, weaponStrength, defence; public bool isEvil, isAlive; public string identity; public static void Bad Guy(Nemesis nem1) { nem1.health = 20; nem1.strength = 10; nem1.weaponStrength = 10; nem1.defence = 10; nem1.isEvil = true; nem1.isAlive = true; nem1.identity = "Mean Guy"; } } class Caculations { public int Calculate() { Random numGen = new Random(); double attackValue, defenceValue, reduction; int damage; attackValue = (strength * weaponStrength) * (numGen.Next(75, 126) * 0.01); defenceValue = defence * 2; reduction = (int)attackValue / defenceValue; damage = (int)attackValue - (int)reduction; return damage; } } 

I've got a fairly simple problem in C#. Thank you in advance.

I'm having trouble linking the attributes (strength & weaponStrength) to the calculations to deal the damage in another class that determines the outcome of the battle. I could call the attributes from both the hero and nemesis, but that would require me to write the same formula twice for both classes, is there a better way?

##### Share on other sites
Even though there is a great difference between a hero and his/hers nemesis in real life, if you look at your code there is (almost) no difference at all. I'd suggest that you create a class Creature/Human/Entity instead and create two separate instances instead.

 class Entity { private int health, strength, weaponStrength, defence; private bool isAlive, isEvil; private string identity; public void Entity(int health, int strength, int weaponStrength, int defence, bool isAlive, bool isEvil, string identity) { this.health = health; this.strength = strength; this.weaponStrength = weaponStrength; this.defence = defence; this.isAlive = isAlive; this.isEvil = isEvil; this.identity = identity; } public int getStrength() { return this.strength; } public Calculate(Entity enemy) { return this.strength - enemy.getStrength(); } } ..... Entity hero = Entity(100, ..., "Good Guy"); Entity nemesis = Entity(100, ..., "Nemesis"); 

Heads up, I've never written a single line of code in C# so the syntax may be a little off.. Took a guess from the code you provided. Although the concept should be correct.

P.S You should be using getters and setters instead of public variables, gives you more control over you code; google "getters and setters C#" and it should be one of the first results. D.S

Edit: Added a getter for strength. Edited by Deprecated

##### Share on other sites
Thanks. I'm haven't learned a great deal in C# as of yet. I'm new to programming, so I haven't come across anything beyond basic class, objects, and methods, so the [color=#0000cd]this reference is new to me. Looks like I have more reading tonight.

##### Share on other sites
Oh sorry!

[font=courier new,courier,monospace]this[/font] will refer to the actual instance created from your class. You don't have to use it, but since I used the same names for parameters as instance variables it depends on the implementation of that particular language whether it'll work without [font=courier new,courier,monospace]this[/font] or not.

Think of it like [font=courier new,courier,monospace]the_variables_that_are_defined_on_the_very_top_of_the_class.strength[font=arial,helvetica,sans-serif] , it is the same as when you're using [font=courier new,courier,monospace]numGen[font=arial,helvetica,sans-serif] to call [/font]numGen.Next(...)[/font] but as there is no name for the instance yet you can use the keyword [font=courier new,courier,monospace]this[/font].[/font][/font]

##### Share on other sites

P.S You should be using getters and setters instead of public variables, gives you more control over you code; google "getters and setters C#" and it should be one of the first results. D.S

Or properties in C#.
 class Entity { // ...... // ...... public int Strength { get { return this.strength; } } } 

You then use it as if it was a public variable and not a method. And you can only read from the property if you define only the get part and not set.

##### Share on other sites

 public static void Good Guy(Hero hero) public static void Bad Guy(Nemesis nem1) 

Just a note: I assume this is only in your post and not in your actual code, but spaces in identifiers are not allowed (in C# and any other programming language I know of). So this would have to be
 public static void GoodGuy(Hero hero) public static void BadGuy(Nemesis nem1) 

##### Share on other sites

Oh sorry!

[font=courier new,courier,monospace]this[/font] will refer to the actual instance created from your class. You don't have to use it, but since I used the same names for parameters as instance variables it depends on the implementation of that particular language whether it'll work without [font=courier new,courier,monospace]this[/font] or not.

Think of it like [font=courier new,courier,monospace]the_variables_that_are_defined_on_the_very_top_of_the_class.strength[font=arial,helvetica,sans-serif] , it is the same as when you're using [font=courier new,courier,monospace]numGen[font=arial,helvetica,sans-serif] to call [/font]numGen.Next(...)[/font] but as there is no name for the instance yet you can use the keyword [font=courier new,courier,monospace]this[/font].[/font][/font]

Don't think of it as a variable declared at the top of the class because that's not what it is or how it works, think of it as a reference to the current instance of the object. But you do use it like a normal instance of that object however. It is also only valid within the scope of a class and nowhere else, this matters for languages that aren't purely OO, like C/C++.

Each language that supports this has it's own way of how it is implemented and when you start to think of it as something it is not you can get in to trouble.

##### Share on other sites

[quote name='Froggggy' timestamp='1326009932' post='4900576']
 public static void Good Guy(Hero hero) public static void Bad Guy(Nemesis nem1) 

Just a note: I assume this is only in your post and not in your actual code, but spaces in identifiers are not allowed (in C# and any other programming language I know of). So this would have to be
 public static void GoodGuy(Hero hero) public static void BadGuy(Nemesis nem1) 
[/quote]

It was a simple mistake on my end when posting the topic.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633669
• Total Posts
3013255
×