C#C
C#9mo ago
FiftyTifty

How to access functions in another class in the same namespace?

Hey dudes. I come from a background of Pascal and Lua, so bear with me if this is an obvious question.

I've got a big class, and I want to move some of my functions into another one to keep things tidy. But it appears to not be as simple as just making another class in the same namespace, and calling the function from there. That throws the error:
An object reference is required for the non-static field, method, or property 'Util.CanMarry(Hero, Hero)'
So what's the approach to adding functions that are called by another class?

Pic 1: The new helper class with a function in it
Pic 2: The big class that I want to use that helper function inside
Pic 3: The error

c#
namespace TeachSpouses
{

    public class Util
    {
        
        public bool CanMarry(Hero heroPlayer, Hero heroMarry)
        {
            return (heroMarry.IsActive
                && heroMarry.Spouse == null
                && heroMarry != heroPlayer
                && heroMarry.IsFemale != heroMarry.IsFemale
                && heroMarry.IsLord
                && !heroMarry.IsMinorFactionHero
                && !heroMarry.IsNotable
                && !heroMarry.IsTemplate
                && heroMarry.CanMarry() == true);
        }

    }
...
internal class sbTeachSpousesCampaign : CampaignBehaviorBase
...
private Hero GetUnwedHeroInSettlement(Hero heroOnTinder, Settlement settlementToSearch)
{

    Hero heroSpouse = null;
    Hero heroCurrent;


    for (int iCounter = 0; iCounter < settlementToSearch.HeroesWithoutParty.Count - 1; iCounter++)
    {
        heroCurrent = settlementToSearch.HeroesWithoutParty[iCounter];

        if (Util.CanMarry(heroOnTinder, heroCurrent)
        )
        {
            heroSpouse = heroCurrent;
            break;
        }
    }

    return heroSpouse;

}
image.png
image.png
image.png
Was this page helpful?