CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2008
    Posts
    154

    Using a function from a different class

    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
    Code:
    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
    Code:
                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.

    Code:
            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??

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Using a function from a different class

    Because it is a private function. If you would like it to be accessible outside of the class, mark it as 'public'. Look here for all of the access modifiers.

  3. #3
    Join Date
    Jun 2008
    Posts
    154

    Re: Using a function from a different class

    OH MAH GAWD. I must have been blind, hahahah I a so used to using privite stuff initially I didn't even look there.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured