[英]How can I put the code block into a function in c#?
我想用 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);
}
}
}
讓我們看看:您有以下不斷返回的代碼:
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);
}
這意味着您需要使用兩個參數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);
}
}
在您的代碼中,您只需使用正確的參數將您的if
-loop 替換為 function:
Do_Something(modelInfoFilePath, this.NewmodelInfoFilePath);
...
Do_Something(definitionFilePath, this.DefinitionFilePath);
...
首先你應該分析你的代碼的哪些部分是相同的,哪些變量可以代替參數。
查看您的代碼,我看到 4 個部分有些相似。
其中 2 個是this.IsMale
,其中兩個是this.IsFemale
(考慮到二元生物學觀點,也可能是.this.IsMale
)。 我們可以使用它來更改字符串本身,而不是使用兩個if
語句。
其中 2 個使用modelInfoFilePath
,另外 2 個使用definitionFilePath
。 如果我們先聲明這些,我們可以將它們用作CopyFile
方法的參數。
看看下面的這段代碼,你可能會明白我的意思。
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.