[英]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_Information
和New_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.