Is it possible to change a bool from a public static class when the bool is located in another public class?

Kim Strasser 1,321 Reputation points
2023-08-20T13:42:01.6533333+00:00

I have the public static class RandomExtensions and I want to change the public bool IsBanned in public class Game1.cs. Both classes are in the same project SharedCode. In addition, I have the public class MyAccount.cs that is in the same project SharedCode.

I don't know if I can immediately change IsBanned when I execute the code in CheckMyAccount() or if it is necessary to change IsBanned a little bit later in the class MyAccount.

Is it possible to immediately change IsBanned when I execute the code in CheckMyAccount()?

Or is it necessary to do it like this?

namespace SharedCode
{
    public class Game1
    {
		public bool IsBanned = false;
	}
}
namespace SharedCode
{
    public class MyAccount
    {
        private Game1 game1;

		public MyAccount(Game1 game)
		{ 			
			game1 = game;
		}

		public async void Update()
		{
			var checkaccount = await RandomExtensions.CheckMyAccount(...,...,...);
        	game1.IsBanned = checkaccount.isbanned;
		}
	}
}
namespace SharedCode
{
    public static class RandomExtensions
    {

	public static async Task<(bool isbanned, bool ..., bool ..., string ..., string ...)> CheckMyAccount(this bool ..., bool ..., string ...)
    {
    bool isbanned = false;

    if (result.Error != null)
    {
	  isbanned = true;
    }
    else
    {
    }

    return (isbanned,...,...,...,...);
  }

  }
}
Developer technologies | C#
{count} votes

Accepted answer
  1. Anonymous
    2023-08-23T02:44:05.47+00:00

    Hi @Kim Strasser , Welcome to Microsoft Q&A,

    If you want to modify the IsBanned attribute directly in checkaccount, you can modify it like this:

    namespace SharedCode
    {
        public class Game1
        {
            public bool IsBanned { get; set; } = false;
        }
    }
    
    namespace SharedCode
    {
        public class MyAccount
        {
            private readonly Game1 _game1;
    
            public MyAccount(Game1 game)
            {
                _game1 = game;
            }
    
            public async Task UpdateAsync()
            {
                await RandomExtensions.CheckMyAccount(_game1, ...);
            }
        }
    }
    
    namespace SharedCode
    {
        public static class RandomExtensions
        {
            public static async Task CheckMyAccount(Game1 game, ...)
            {
                // Your account checking logic here.
                bool isBanned = ...;
    
                game.IsBanned = isBanned;
            }
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. P a u l 10,761 Reputation points
    2023-08-20T16:25:17.9033333+00:00

    For these God/single-instance objects like Game1 you could set it up like a singleton (provided there is only one instance in your application):

    https://csharpindepth.com/articles/singleton

    However I think the cleanest approach is what you currently have. I don't know anything about your application so take this with a pinch of salt, but it might make sense to move IsBanned into the MyAccount class, as it feels like the account could take ownership, and that would eliminate the need for MyAccount to know anything about the Game1 God-object and also get rid of that indirection where MyAccount is setting state against Game1. I'm presuming Game1 is the owner of the MyAccount instance, and I find that "owned" instances changing state on "owner" instances tends to result in spaghetti code where it's difficult to track down where a state change has occurred.

    That's just my opinion though, but I find that moving ownership of state into a class that represents that logical component tends to eliminate the need for passing these root objects around.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.