C
C#4d 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;

}
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;

}
No description
No description
No description
4 Replies
Angius
Angius4d ago
Well, CanMarry is not a static function So it needs an instance of the class Since it seems that it's just a pure utility function, you could probably just make both the class and the method static That will let you use it with just Util.CanMarry(...)
FiftyTifty
FiftyTiftyOP4d ago
Oh heck it was that simple huh. Yeah it's just going to be a class to put logic checks in. Awesome thanks for that
Angius
Angius4d ago
:Ok:
Betrueone
Betrueone3d ago
🙁

Did you find this page helpful?