简体   繁体   English

如何将代码块放入 c# 中的 function 中?

[英]How can I put the code block into a function in c#?

I would want to use a function in the place of the if/else -clause.我想用 function 代替if/else子句。

public void CopyPasteFiles(string modelInfoFilePath, string definitionFilePath, string pedName)
        {
            string messageBoxText = "Looks like a ped with this name already Exists. Please try a new Name.";

            if (this.IsMale)
            {
                modelInfoFilePath = Tokenizer.Detokenize(modelInfoFilePath);
                this.NewModelInfoPath = modelInfoFilePath.Replace("Z_Z_ProxyPed_MR1_000_Dummy", pedName);

                if (!File.Exists(this.NewModelInfoPath))
                {
                    File.Copy(modelInfoFilePath, this.NewModelInfoPath, false);
                    FileInfo fileInfo = new FileInfo(this.NewModelInfoPath);
                    fileInfo.IsReadOnly = false;
                }
                else
                {
                    RsMessageBox.Show(messageBoxText);
                }

                definitionFilePath = Tokenizer.Detokenize(definitionFilePath);
                this.NewDefinitionPath = definitionFilePath.Replace("Z_Z_ProxyPed_MR1_000_Dummy", pedName);

                if (!File.Exists(this.NewDefinitionPath))
                {
                    File.Copy(definitionFilePath, this.NewDefinitionPath, false);
                    FileInfo fileInfo = new FileInfo(this.NewDefinitionPath);
                    fileInfo.IsReadOnly = false;
                }
                else
                {
                    RsMessageBox.Show(messageBoxText);
                }
            }
            else if (this.IsFemale)
            {
                modelInfoFilePath = Tokenizer.Detokenize(modelInfoFilePath);
                this.NewModelInfoPath = modelInfoFilePath.Replace("Z_Z_ProxyPed_FR1_000_Dummy", pedName);
                if (!File.Exists(this.NewModelInfoPath))
                {
                    File.Copy(modelInfoFilePath, this.NewModelInfoPath, false);
                    FileInfo fileInfo = new FileInfo(this.NewModelInfoPath);
                    fileInfo.IsReadOnly = false;
                }
                else
                {
                    RsMessageBox.Show(messageBoxText);
                }

                definitionFilePath = Tokenizer.Detokenize(definitionFilePath);
                this.NewDefinitionPath = definitionFilePath.Replace("Z_Z_ProxyPed_FR1_000_Dummy", pedName);
                if (!File.Exists(this.NewDefinitionPath))
                {
                    File.Copy(definitionFilePath, this.NewDefinitionPath, false);
                    FileInfo fileInfo = new FileInfo(this.NewDefinitionPath);
                    fileInfo.IsReadOnly = false;
                }
                else
                {
                    RsMessageBox.Show(messageBoxText);
                }
            }
        }

Let's see: you have the following piece of code which comes back constantly:让我们看看:您有以下不断返回的代码:

if (!File.Exists(New_Information))
{
    File.Copy(Existing_Information, New_Information, false);
    FileInfo fileInfo = new FileInfo(New_Information);
    fileInfo.IsReadOnly = false;
}
else
{
    RsMessageBox.Show(messageBoxText);
}

This means that you need to write a function, using the two parameters Existing_Information and New_Information , something like:这意味着您需要使用两个参数Existing_InformationNew_Information ,例如:

void Do_Something(var Existing_Information, var New_Information)
{
    if (!File.Exists(New_Information))
    {
        File.Copy(Existing_Information, New_Information, false);
        FileInfo fileInfo = new FileInfo(New_Information);
        fileInfo.IsReadOnly = false;
    }
    else
    {
        RsMessageBox.Show(messageBoxText);
    }
}

Inside your code, you just replace your if -loop by that function, using the correct parameters:在您的代码中,您只需使用正确的参数将您的if -loop 替换为 function:

Do_Something(modelInfoFilePath, this.NewmodelInfoFilePath);
...
Do_Something(definitionFilePath, this.DefinitionFilePath);
...

First you should analyze what parts of your code are the same, and which variables can be substituted for parameters.首先你应该分析你的代码的哪些部分是相同的,哪些变量可以代替参数。

Looking at your code, I see 4 parts that are somewhat similar.查看您的代码,我看到 4 个部分有些相似。

2 of them are for this.IsMale , and two of them are this.IsFemale (which could also be .this.IsMale considering a binary biological standpoint).其中 2 个是this.IsMale ,其中两个是this.IsFemale (考虑到二元生物学观点,也可能是.this.IsMale )。 We can use this to change the string itself, instead of using two if statements.我们可以使用它来更改字符串本身,而不是使用两个if语句。

2 of them use modelInfoFilePath , while the other 2 use definitionFilePath .其中 2 个使用modelInfoFilePath ,另外 2 个使用definitionFilePath If we declare these first, we can use them as parameters for a CopyFile method.如果我们先声明这些,我们可以将它们用作CopyFile方法的参数。

Take a look at this code below, you might see what I mean.看看下面的这段代码,你可能会明白我的意思。

public void CopyPasteFiles(string modelInfoFilePath, string definitionFilePath, string pedName)
{
    modelInfoFilePath = Tokenizer.Detokenize(modelInfoFilePath);
    definitionFilePath = Tokenizer.Detokenize(definitionFilePath);

    string pedReplaceName = this.IsMale : "Z_Z_ProxyPed_MR1_000_Dummy" ? "Z_Z_ProxyPed_FR1_000_Dummy";

    // replace names
    this.NewModelInfoPath = 
    modelInfoFilePath.Replace(pedReplaceName, pedName);
    this.NewDefinitionPath = 
    definitionFilePath.Replace(pedReplaceName, pedName);

    // copy files
    CopyFile(modelInfoFilePath, this.NewModelInfoPath);
    CopyFile(definitionFilePath, this.NewDefinitionPath);
}

public void CopyFile(filePath, newFilePath) {
    if (!File.Exists(newFilePath))
    {
        File.Copy(filePath, newFilePath, false);
        FileInfo fileInfo = new FileInfo(newFilePath);
        fileInfo.IsReadOnly = false;
    }
    else {
        RsMessageBox.Show("Looks like a ped with this name already exists. Please try a new Name.");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中删除相同的代码块? - How can i remove the same code block in c#? 我怎么能延迟C#? - How can i put a delay with C#? 我如何将我的OnItemDataBound C#代码放在我的代码后面,并将其绑定到asp:repeater - How can I put my OnItemDataBound c# code in my code behind and bind it to the asp:repeater CodePen markdown editor中,可以设置C# Code Block吗? - In the CodePen markdown editor, can I set a C# Code Block? 在 C# 中,如何提取产生返回值的代码块? - In C#, how can I extract a block of code which yields the return? 将vb中的代码块转换为c#时,应该将其放在循环的哪一部分? - What part from the block of code in vb when converting it to c# should i put in the loop? 我想发表评论,让我在 C# 中获得不同的代码块 - I want to put a comment that will let me get to a different block of code in c# 使用C#进行Docusign移动信封-如何将XML / JSON请求和“ PUT”方法转换为有效的C#代码? - Docusign Move Envelope using C# - How can I turn XML/JSON requests and “PUT” methods into working C# code? 我如何在C#自动构造函数块中破解? - How can I break inside an C# automatic constructor block? 如何在 C# 中阻止键盘和鼠标输入? - How can I block keyboard and mouse input in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM