HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Performing a procedure for each form edit

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
performingediteachprocedureforform

Problem

I am a bit new to programming. I have some edits on my form, and for each edit completed it will have to do a procedure.

Currently, I use this code that works perfectly, but I am looking for a better way to accomplish this task. If I add another edit not have to repeat all the code.

```
string rgxExpr = @"(\w+)\s([\w\s]*)"; // Pega primeiro nome
string rgxExpr2 = @"\b(\w{1})"; // Abreviação
string s1, s2, s3, s4,s5;

/ EDIT 1 /
Match match1 = Regex.Match(edtAutor1.Text, rgxExpr);
Match match2 = Regex.Match(match1.Groups[1].ToString(), rgxExpr2);
s1 = match1.Groups[2].ToString().ToUpper() + ", " + match2.Groups[1].ToString().ToUpper();
// Apenas um autor
s4 = match1.Groups[2].ToString().ToUpper() + ", " + match1.Groups[1].ToString().ToUpper();
/ EDIT 2 /
Match match11 = Regex.Match(edtAutor2.Text, rgxExpr);
Match match22 = Regex.Match(match11.Groups[1].ToString(), rgxExpr2);
s2 = match11.Groups[2].ToString().ToUpper() + ", " + match22.Groups[1].ToString().ToUpper();
/ EDIT 3 /
Match match111 = Regex.Match(edtAutor3.Text, rgxExpr);
Match match222 = Regex.Match(match111.Groups[1].ToString(), rgxExpr2);
s3 = match111.Groups[2].ToString().ToUpper() + ", " + match222.Groups[1].ToString().ToUpper();
/ EDIT 4 /
Match match1111 = Regex.Match(edtAutor4.Text, rgxExpr);
Match match2222 = Regex.Match(match1111.Groups[1].ToString(), rgxExpr2);
s5 = match1111.Groups[2].ToString().ToUpper() + ", " + match2222.Groups[1].ToString().ToUpper();

if (radioButton1.Checked == true)
{
if (edtAutor1.Text != "")
{
/ Bibliográfia - 1 AUTOR /
if (string.IsNullOrWhiteSpace(edtAutor2.Text) && string.IsNullOrWhiteSpace(edtAutor3.Text))
{
addbb.dataGridView1.Rows.Add(s4+". ", abntrTitulo.Text+"." , abntrEdicao.Text +" Ed. ", abntrLocal.Text + ": ", abntrEditor.Text + ", ",
abntrAno.Text + ". ", abntrPaginas.Text +"p.");
}

/ Bibliográfia - 2 AUTORES /
if (string.IsNullOrWhiteSpace(edtAutor3.Text))

Solution

It looks to me that you could use a method that returns the appropriate string. Something like the following, which follows the pattern in your code:

static string GetEdit(string txt)
{
    string rgxExpr = @"(\w+)\s([\w\s]*)"; // Pega primeiro nome
    string rgxExpr2 = @"\b(\w{1})"; // Abreviação    
    Match match1 = Regex.Match(txt, rgxExpr);
    Match match2 = Regex.Match(match1.Groups[1].ToString(), rgxExpr2);
    return match1.Groups[2].ToString().ToUpper() + ", " + match2.Groups[1].ToString().ToUpper();
}


You could do something similar with the other duplicated code by using a list of strings that correspond to a list of textboxes. allowing you to add or delete as needed.

Code Snippets

static string GetEdit(string txt)
{
    string rgxExpr = @"(\w+)\s([\w\s]*)"; // Pega primeiro nome
    string rgxExpr2 = @"\b(\w{1})"; // Abreviação    
    Match match1 = Regex.Match(txt, rgxExpr);
    Match match2 = Regex.Match(match1.Groups[1].ToString(), rgxExpr2);
    return match1.Groups[2].ToString().ToUpper() + ", " + match2.Groups[1].ToString().ToUpper();
}

Context

StackExchange Code Review Q#143555, answer score: 2

Revisions (0)

No revisions yet.