MDog

I am currently working on a small game project and trying to figure out how I can compile some stuff I wrote by hand into C# code.

Example of hand written info:
If p > q, and distance between p and q is greater than 10
then b = 30
If p > q, and distance between p and q is greater than 14+
then b = 1
End of hand written info

I understand how to work If, else, and switch statements, but I cannot figure how to make such statements as the ones above. I am not really sure if this is enough detail for anyone to understand what I am talking about, but if not please ask me to state more. Otherwise, would anyone assist me with my problem.
Thank you!


Re: Visual C# Language Complex If and else statements

boban.s

if (p > q)
{
if ((p - q) > 14)
{
b = 1;
}
else if((p - q) > 10)
{
b = 30;
}
}




Re: Visual C# Language Complex If and else statements

MDog

Thanks for the help as it was useful, but I am still having the problem of writing the example.

I am having the problem of putting the code in the correct order and in the proper form. This is the goal I am trying to achieve.

private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Text == "Heavy Weapon")
{
double lvl = double.Parse(leveloperant.Value.ToString());
double dex = double.Parse(dexoperant.Value.ToString());
double luk = double.Parse(lukoperant.Value.ToString());
double mas = double.Parse(masoperant.Value.ToString());
double equip = double.Parse(equipoperant.Value.ToString());
double mlvl = double.Parse(mleveloperant.Value.ToString());
double evade = double.Parse(evadeoperant.Value.ToString());
}
I also want to create two more variables, a and b. Variable b is filled by an if statement similar to my previous post.

If lvl (being double variable listed above) >= mlvl, variable b will equal 100. b will serve in an expression such as a + b, but will need to have the same value as it gained from the if statements. If anyone is still unclear about my question please message me on MSN, toyboytbfb@hotmail.com. Thanks in advance!




Re: Visual C# Language Complex If and else statements

IsshouFuuraibou

private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Text == "Heavy Weapon")
{
double lvl = double.Parse(leveloperant.Value.ToString());
double dex = double.Parse(dexoperant.Value.ToString());
double luk = double.Parse(lukoperant.Value.ToString());
double mas = double.Parse(masoperant.Value.ToString());
double equip = double.Parse(equipoperant.Value.ToString());
double mlvl = double.Parse(mleveloperant.Value.ToString());
double evade = double.Parse(evadeoperant.Value.ToString());
if ( lvl > mlvl )
{
b = 100; // Otherwise "b" keeps it's old value. b must be class scope in order for it to be accessed in multiple methods
}
}




Re: Visual C# Language Complex If and else statements

OmegaMan

IsshouFuuraibou wrote:
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Text == "Heavy Weapon")
{
double lvl = double.Parse(leveloperant.Value.ToString());
double dex = double.Parse(dexoperant.Value.ToString());
double luk = double.Parse(lukoperant.Value.ToString());
double mas = double.Parse(masoperant.Value.ToString());
double equip = double.Parse(equipoperant.Value.ToString());
double mlvl = double.Parse(mleveloperant.Value.ToString());
double evade = double.Parse(evadeoperant.Value.ToString());
if ( lvl > mlvl )
{
b = 100; // Otherwise "b" keeps it's old value. b must be class scope in order for it to be accessed in multiple methods
}
}


As an aside, and if you haven't thought about it...begin to think of things as objects. You have identified an object to work on in the combobox as heavy weapon. (Must be a cooking simulator <g>). I assume you have multiple weapons and each weapon has a common set of properties with the only difference being the values of those properties.

I recommend that you create an IWeapon interface and each weapon derives from that interface. Then when the program needs to access the weapon, you can just past the IWeapon interface around without having a specific object. Here is some example code

Code Snippet

public enum eWeaponType
{
Light,
Heavy,
Heavy_NeedsPlatform

}

public interface IWeapon
{

// Standard properties
double Level { get; set; }
double Dexterity { get; set; }
double Evade { get; set; }

// Identity
string Name { get; }

eWeaponType WeaponType { get; }

bool IsSingleHanded();


}

// Now when we create we can type IWeapon and
// the interface will be stubbed in for us on
// Alt-Shift-F10
public class Lugar : IWeapon
{

public Lugar(double level, double Dexterity, double Evade)
{
_Level = level;
// Fill in the rest.
}

#region IWeapon Members

public double _Level;

public double Level
{
get
{
return _Level);
}
set
{
_Level = value/2; // Specialized level for the lugar
}
}


public string Name
{
get { return "Lugar"; }
}

public eWeaponType WeaponType
{
get { return eWeaponType.Light; }
}

public bool IsSingleHanded()
{
return true;
}

#endregion
}


// Usage later on

// Crfeate the weapon
IWeapon weapon = new Lugar(12, 14, 18);

// Sent to another place, say maybe firefight

// Get weapons stats for firing, doesn't matter what type.

FireWeapon(weapon);

// INside FireWeapon

void FireWeapon(IWeapon weapon)
{
StatusBox.Text = string.Format("Firing {0} ", weapon.Name);


}






If you do this you have a standard way of processing weapons and its easier to pass around differing items. Its more work upfront, but pays off in the future for new items or if you need to maintain the code for future changes.





Re: Visual C# Language Complex If and else statements

MDog

Thank you OmegaMan as that information was very useful. So, it seems I must learn what and how to use a Class scope system I am still new at C# and...I really didn't understand that much about your response. If anyone could list the things I need to know before I can make my application run that be great. Thanks!

By the way, I am only trying to create a calculator application, not a full system yet!




Re: Visual C# Language Complex If and else statements

OmegaMan

Check out the CSharp Articles which is an excellent place to pick varying topics from generics, to patterns to string manipulation.