Click to See Complete Forum and Search --> : Using a function from a different class


bixel
February 26th, 2009, 10:27 PM
2008 C# Forms

I have extended class called, LevelChanger : NumericUpDown, and have added a function called ChangeLevel

The Class, and the Function I will be using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Battler
{
class LevelChanger: NumericUpDown
{
private decimal levelNumber;

public Battler battler = new Battler();

public decimal LevelNumber
{
get { return levelNumber; }
set { levelNumber = value; }
}

private float HPrate;
private float holderHP;
private float holderMP;
private int oldLevel;
public int newStrength, newVitality, newAgility;

private void ChangeLevel(string measure1, string measure2, string measure3)
{
int newValue = Convert.ToInt32(LevelNumber);
int oldStrength = Convert.ToInt32(measure1);
int oldVitality = Convert.ToInt32(measure2);
int oldAgility = Convert.ToInt32(measure3);
if (newValue == 0)
{
holderHP = 100f;
holderMP = 0f;
}
else if (newValue > 0)
{
float newHitPoints;
float newMagicPoints;
if (6 > newValue)
{
HPrate = 1.25f;
}
else if (5 < newValue && newValue < 11)
{
HPrate = 1.20f;
}
else if (10 < newValue && newValue < 16)
{
HPrate = 1.15f;
}
else if (15 < newValue && newValue < 21)
{
HPrate = 1.10f;
}
else if (20 < newValue && newValue < 31)
{
HPrate = 1.05f;
}
else if (30 < newValue && newValue < 41)
{
HPrate = 1.02f;
}
if (newValue > oldLevel)
{
newHitPoints = (holderHP + battler.CheckClassHPMod()) * HPrate;
newMagicPoints = (holderMP + battler.CheckClassMPMod());
// new stats
newStrength = oldStrength + battler.CheckClassStrMod();
newVitality = oldVitality + battler.CheckClassVitMod();
newAgility = oldAgility + battler.CheckClassAglMod();
}
else
{
newHitPoints = (holderHP - battler.CheckClassHPMod()) / HPrate;
newMagicPoints = (holderMP - battler.CheckClassMPMod());
// new stats
newStrength = oldStrength - battler.CheckClassStrMod();
newVitality = oldVitality - battler.CheckClassVitMod();
newAgility = oldAgility - battler.CheckClassAglMod();
}
//convert to Int to get rid of decimal value.
int adjustedMagicPoints = Convert.ToInt32(newMagicPoints);
int adjustedHitPoints = Convert.ToInt32(newHitPoints);
holderHP = adjustedHitPoints;
holderMP = adjustedMagicPoints;
}
oldLevel = newValue;
}
}
}


in my main.cs I initialize it like this

this.levelChanger2 = new LevelChanger();


Since LevelChanger is a NumericUpDown tool I am using it in my forms. I have this event fired when its used.


private void ChangeLevels1(object sender, EventArgs e)
{
//do some stuff
}


In this event I want to fire that object's function but I can't seem to access it. Doing this
this.levelChanger2.ChangeLevel(measure1, measure2, measure3)

doesn't work. But I can access its variables. Like;
levelChanger2.newStrength = something...

Why can't I get to its function even though I already instantiated it??

BigEd781
February 26th, 2009, 10:42 PM
Because it is a private function. If you would like it to be accessible outside of the class, mark it as 'public'. Look here (http://msdn.microsoft.com/en-us/library/wxh6fsc7(VS.71).aspx) for all of the access modifiers.

bixel
February 26th, 2009, 10:53 PM
OH MAH GAWD. I must have been blind, hahahah I a so used to using privite stuff initially I didn't even look there.