Falco
Falco
Explore posts from servers
CC#
Created by Falco on 11/30/2024 in #help
Did I implement State Machine Pattern correctly?
Im trying to implement State Machine Pattern for the state of a form, but I feel like it's not right or adding any convenience. Context: User fills in a form on my webpage. User then saves the form. Upon saving, the form should get one of the following states:
c#
public enum State
{
[Description("NewForm")] // Initial state
Draft = 0,
[Description("Denied")] // Form was denied
Afgekeurd = 1,
[Description("Approved")] // Form was approved
Afgerond = 2,
[Description("WaitForApprovalManager")] // Form needs manager approval
WaitForApprovalManager = 3,
[Description("WaitForApprovalTech")] // Form needs tech department approval
WaitForApprovalTech = 4,
[Description("WaitForApprovalFinance")] // Form needs finance department approval
WaitForApprovalFinance = 5,
}
c#
public enum State
{
[Description("NewForm")] // Initial state
Draft = 0,
[Description("Denied")] // Form was denied
Afgekeurd = 1,
[Description("Approved")] // Form was approved
Afgerond = 2,
[Description("WaitForApprovalManager")] // Form needs manager approval
WaitForApprovalManager = 3,
[Description("WaitForApprovalTech")] // Form needs tech department approval
WaitForApprovalTech = 4,
[Description("WaitForApprovalFinance")] // Form needs finance department approval
WaitForApprovalFinance = 5,
}
The state of a form is defined by business rules. For example:
If (form.totalpay < 500) Finance should approve the form state should be.
If (form.totalpay >= 500) Finance and management should approve the form.
If (form.totalpay >= 500 and form = tech) All departments should approve.
etc..
If (form.totalpay < 500) Finance should approve the form state should be.
If (form.totalpay >= 500) Finance and management should approve the form.
If (form.totalpay >= 500 and form = tech) All departments should approve.
etc..
I made an attempt to make a state machine pattern, but I can't seem to understand why it's useful. So maybe im not doing it correctly. Tips are very welcome. How I implemented the State Machine Pattern: FormStateService.cs
c#
public class FormStateService
{
private FormState _currentState; // Current state of the form
private Form _form; // Form that the user filled in

public FormStateService(Form form)
{
_form = form;
if (_currentState == null)
{
SetState(new DraftState()); // Set initial state to draft
}
}

public void SetState(FormState state)
{
_currentState = state;
_currentState.SetContext(_form);
}

public Form HandleStateTransition()
{
_currentState.HandleStateTransition(this);
return _form;
}

public Status GetCurrentState()
{
return (State)_form.State;
}
}
c#
public class FormStateService
{
private FormState _currentState; // Current state of the form
private Form _form; // Form that the user filled in

public FormStateService(Form form)
{
_form = form;
if (_currentState == null)
{
SetState(new DraftState()); // Set initial state to draft
}
}

public void SetState(FormState state)
{
_currentState = state;
_currentState.SetContext(_form);
}

public Form HandleStateTransition()
{
_currentState.HandleStateTransition(this);
return _form;
}

public Status GetCurrentState()
{
return (State)_form.State;
}
}
FormState.cs:
C#
public abstract class FormState
{
protected Form _form;

public void SetContext(Form form)
{
_form = form;
}

public abstract void HandleStateTransition(FormStateService stateMachine);
}
C#
public abstract class FormState
{
protected Form _form;

public void SetContext(Form form)
{
_form = form;
}

public abstract void HandleStateTransition(FormStateService stateMachine);
}
Each State has its own class like this with the business rules defined. For example this is what each stateclass looks like. WaitForApprovalManagerState.cs
c#
public override void HandleStateTransition(FormStateService stateMachine)
{

if (_form.TotalReceipt >= 500) // Wait for managed to approve
{
_form.State = (int)Status.WaitForApprovalManagerState;
stateMachine.SetState(new WaitForApprovalManagerState());
return;
}

if (_form.TotalReceipt >= 500 && _form.ManagerApproved && _form.TechForm == true) // If manager has approved let tech approve
{
_form.State = (int)Status.WaitForApprovalTechState;
stateMachine.SetState(new WaitForApprovalTechState());
return;
}

}
c#
public override void HandleStateTransition(FormStateService stateMachine)
{

if (_form.TotalReceipt >= 500) // Wait for managed to approve
{
_form.State = (int)Status.WaitForApprovalManagerState;
stateMachine.SetState(new WaitForApprovalManagerState());
return;
}

if (_form.TotalReceipt >= 500 && _form.ManagerApproved && _form.TechForm == true) // If manager has approved let tech approve
{
_form.State = (int)Status.WaitForApprovalTechState;
stateMachine.SetState(new WaitForApprovalTechState());
return;
}

}
1 replies
CC#
Created by Falco on 5/10/2024 in #help
How to refer to the mainwindow class from another class.
Creating a WPF app. I have one main class where all the mainwindow controls are operated.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitializeAPIs();
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitializeAPIs();
}
I have a different class that's called FilterManager. In this class I want to operate some data on the mainwindow as well. But I'm not sure how, what's a clean way to refer to the mainwindow from this class?
internal class FilterManager
{
// Reference to MainWindow here
}
internal class FilterManager
{
// Reference to MainWindow here
}
5 replies
CC#
Created by Falco on 5/9/2024 in #help
How to get rid of nullable warning?
No description
15 replies
CC#
Created by Falco on 4/17/2024 in #help
Practicing OOP in c#, is this logical implementation of an Interface?
c#
public interface IMembershipManager
{
void RegisterMembership(Person person);
void CancelMembership(Person person);
bool CheckMembership(Person person);
}

public class MembershipManager : IMembershipManager
{
public void RegisterMembership(Person person) {
person.Membership = true;
}
public void CancelMembership(Person person) {
person.Membership = false;
}

public bool CheckMembership(Person person) {
return person.Membership;
}
}

public class Person
{
public Person(string firstname, string lastname, bool membership)
{
FirstName = firstname;
LastName = lastname;
Membership = false;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Membership { get; set; }
}

public class Program{
public static void Main()
{
Person person = new Person("John", "John", false);
Console.WriteLine(person.FirstName);
Console.WriteLine(person.LastName);
Console.WriteLine(person.Membership);

IMembershipManager membershipManager = new MembershipManager();
membershipManager.RegisterMembership(person);
Console.WriteLine(person.FirstName);
Console.WriteLine(person.LastName);
Console.WriteLine(person.Membership);
}
}
c#
public interface IMembershipManager
{
void RegisterMembership(Person person);
void CancelMembership(Person person);
bool CheckMembership(Person person);
}

public class MembershipManager : IMembershipManager
{
public void RegisterMembership(Person person) {
person.Membership = true;
}
public void CancelMembership(Person person) {
person.Membership = false;
}

public bool CheckMembership(Person person) {
return person.Membership;
}
}

public class Person
{
public Person(string firstname, string lastname, bool membership)
{
FirstName = firstname;
LastName = lastname;
Membership = false;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Membership { get; set; }
}

public class Program{
public static void Main()
{
Person person = new Person("John", "John", false);
Console.WriteLine(person.FirstName);
Console.WriteLine(person.LastName);
Console.WriteLine(person.Membership);

IMembershipManager membershipManager = new MembershipManager();
membershipManager.RegisterMembership(person);
Console.WriteLine(person.FirstName);
Console.WriteLine(person.LastName);
Console.WriteLine(person.Membership);
}
}
65 replies
CC#
Created by Falco on 3/28/2024 in #help
MainWindow' does not contain a definition for 'Button_Click'
No description
21 replies
CC#
Created by Falco on 3/27/2024 in #help
WPF UI appears different in debug
No description
26 replies
CC#
Created by Falco on 3/25/2024 in #help
How to create a hotkey so the user can stop/pause the program.
I created a WPF app that automates mouse movements. When the program is active the user cannot press the stop button since the mouse is being used. How do I create a hotkey that can be pressed and stop the programming. I searched the internet and I found 2 options, but I don't know which is best 1. Use a side thread for the program and a main thread that listens for any hotkeys. Then the main thread either pauses or kills the side thread. 2. Use Task ? But I'm not sure how that works.
18 replies