Jermain Defo
Jermain Defo

Reputation: 189

Object Orientating C#

I have a general question for Object Orientating in C#.

Suppose I have multiple XAML pages which all have this method:

private void SubmitButton_Click(object sender, RoutedEventArgs e)
{
   // Do Something
}

Now I have a class to hold a method, the class is called Methods.cs and holds this:

public void StopUpperAndSpace(TextBox txtBox) 
{
   txtBox.Text = txtBox.Text.Trim();
   // Set it to lowercase
   txtBox.Text = txtBox.Text.ToLower();
}

When a user clicks the SubmitButton on any of the pages I want it to perform this method stored in the Methods.cs. If i have a textbox on one XAML page called NameTextBox when a user clicks the SubmitButton how would I perform the method so the NameTextBox in the SubmitButton_Click method is equal to the txtBox in the StopUpperAndSpace method?

I understand the question is hard to understand but Im finding it really hard to explain. If you need clarification, just comment! Thanks:)

Upvotes: 0

Views: 98

Answers (1)

Derrick Moeller
Derrick Moeller

Reputation: 4950

Have I misunderstood you, why wouldn't you be able to pass it to your method? I would also make your method a static, but if you wanted to create an instance of your Methods class you technically could.

SomePage.xaml

<TextBox x:Name="SomeTextBox" />
<TextBox x:Name="SomeOtherTextBox />

SomePage.xaml.cs

private void SubmitButton_Click(object sender, RoutedEventArgs e)
{
    // Your initial method call could potentially look like this.
    Methods.StopUpperAndSpace(this.SomeTextBox);

    // Alternatively you could do something like this.
    this.SomeOtherTextBox.Text = Methods.ToLowerAndTrim(this.SomeOtherTextBox.Text);
}

Methods.cs

public static void StopUpperAndSpace(TextBox txtBox) 
{
    txtBox.Text = txtBox.Text.Trim();
    // Set it to lowercase
    txtBox.Text = txtBox.Text.ToLower();
}

public static string ToLowerAndTrim(string text)
{
    return text.Trim().ToLower();
}

Upvotes: 4

Related Questions