During coding, since we started to perform simple testing and development under winform, we wanted to divide the code into different classes because we had more code, however, because the original test is in the same form, it is very convenient for function calling. Once the cross-class is passed, we will find that the coupling degree of this function is too high, so that I don't know how to decouple it into the class. In this case, you may wish to use the Delegate-type Func and Action.
Below is the simple code written in winform during the initial test.
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Text.RegularExpressions;using System.Threading.Tasks;using System.Windows.Forms;namespace FunctionPass{ public partial class FormMain : Form { public FormMain() { InitializeComponent(); } private void buttonTest_Click(object sender, EventArgs e) { test(); } public void test() { string sourceStr = collectData(); string digitStr = fetchDigit(sourceStr); MessageBox.Show("source:" + sourceStr + "\r\n digit:" + digitStr); } public string collectData() { return Guid.NewGuid().ToString(); } public string fetchDigit(string sourceStr) { Regex regex = new Regex(@"\d*"); MatchCollection digitCollection=regex.Matches(sourceStr); string digitStr = ""; foreach (Match digitMatch in digitCollection) { digitStr += digitMatch; } return digitStr; } }}
Here we use the colloectData function to collect data, extract the numbers in the data through fetchDigit, and then use the test function to display the results.
Now I want to make the display result into a separate class (such as the Test class) for better extension.
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace FunctionPass{ public class Test { public void test() { string sourceStr = collectData(); string digitStr = fetchDigit(sourceStr); MessageBox.Show("source:" + sourceStr + "\r\n digit:" + digitStr); } }}