带参数的提取方法(C#、VS 2017)
Extract method with parameters (C#, VS 2017)
我在按钮点击中输入了以下代码:
private void Button_Click_1(object sender, RoutedEventArgs e){
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "CSV File|*.csv";
saveFileDialog.Title = "Save CSV File";
saveFileDialog.FileName = newCSVName;
saveFileDialog.InitialDirectory = System.IO.Path.GetFullPath(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog().GetValueOrDefault(false)){
if (saveFileDialog.FileName != ""){
ParseStream(path, OutputResults.OUTPUT_TYPE_FILE, null, null, saveFileDialog.FileName);
}}}
我正在尝试将它提取到新方法中,因为在第二个按钮中我已经有了相同的代码,只有一个,下面一行是不同的(具有不同的参数):
ParseStream(path, OutputResults.OUTPUT_TYPE_SQLINSERT, tableName.Text, null, saveFileDialog.FileName);
提取到我已经做过的新方法后:
private void SaveFileD(string nFilter, string nTitle, string newCSVName){
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = nFilter;
saveFileDialog.Title = nTitle;
saveFileDialog.FileName = newCSVName;
saveFileDialog.InitialDirectory = System.IO.Path.GetFullPath(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog().GetValueOrDefault(false)){
if (saveFileDialog.FileName != ""){
//.....
}}
以及新按钮点击代码:
private void Button_Click_1(object sender, RoutedEventArgs e){
string nFilter = "CSV File|*.csv";
string nTitle = "Save CSV File";
string newCSVName = newCSVName;
SaveFileD(nFilter, nTitle, newCSVName);}
但我不知道如何使用 ParseStream 方法,因为我将不同的参数传递给两种情况。
有点不清楚你在问什么,但看起来你想根据 SaveFileD
方法中的当前上下文更改传递给 ParseStream
方法的参数。
public void MyAwesomeMethodThatDoesStuff(string path, OutputResults outputResults, string someParamater = null)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "CSV File|*.csv";
saveFileDialog.Title = "Save CSV File";
saveFileDialog.FileName = newCSVName;
saveFileDialog.InitialDirectory = System.IO.Path.GetFullPath(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog().GetValueOrDefault(false))
{
if (saveFileDialog.FileName != "")
{
ParseStream(path, outputResults, someParamater, null, saveFileDialog.FileName);
}
}
}
用法
MyAwesomeMethodThatDoesStuff(@"D:\MyAwesomePath", OutputResults.OUTPUT_TYPE_FILE, tableName.Text)
或者
MyAwesomeMethodThatDoesStuff(@"D:\MyAwesomePath", OutputResults.OUTPUT_TYPE_FILE)
我在按钮点击中输入了以下代码:
private void Button_Click_1(object sender, RoutedEventArgs e){
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "CSV File|*.csv";
saveFileDialog.Title = "Save CSV File";
saveFileDialog.FileName = newCSVName;
saveFileDialog.InitialDirectory = System.IO.Path.GetFullPath(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog().GetValueOrDefault(false)){
if (saveFileDialog.FileName != ""){
ParseStream(path, OutputResults.OUTPUT_TYPE_FILE, null, null, saveFileDialog.FileName);
}}}
我正在尝试将它提取到新方法中,因为在第二个按钮中我已经有了相同的代码,只有一个,下面一行是不同的(具有不同的参数):
ParseStream(path, OutputResults.OUTPUT_TYPE_SQLINSERT, tableName.Text, null, saveFileDialog.FileName);
提取到我已经做过的新方法后:
private void SaveFileD(string nFilter, string nTitle, string newCSVName){
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = nFilter;
saveFileDialog.Title = nTitle;
saveFileDialog.FileName = newCSVName;
saveFileDialog.InitialDirectory = System.IO.Path.GetFullPath(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog().GetValueOrDefault(false)){
if (saveFileDialog.FileName != ""){
//.....
}}
以及新按钮点击代码:
private void Button_Click_1(object sender, RoutedEventArgs e){
string nFilter = "CSV File|*.csv";
string nTitle = "Save CSV File";
string newCSVName = newCSVName;
SaveFileD(nFilter, nTitle, newCSVName);}
但我不知道如何使用 ParseStream 方法,因为我将不同的参数传递给两种情况。
有点不清楚你在问什么,但看起来你想根据 SaveFileD
方法中的当前上下文更改传递给 ParseStream
方法的参数。
public void MyAwesomeMethodThatDoesStuff(string path, OutputResults outputResults, string someParamater = null)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "CSV File|*.csv";
saveFileDialog.Title = "Save CSV File";
saveFileDialog.FileName = newCSVName;
saveFileDialog.InitialDirectory = System.IO.Path.GetFullPath(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog().GetValueOrDefault(false))
{
if (saveFileDialog.FileName != "")
{
ParseStream(path, outputResults, someParamater, null, saveFileDialog.FileName);
}
}
}
用法
MyAwesomeMethodThatDoesStuff(@"D:\MyAwesomePath", OutputResults.OUTPUT_TYPE_FILE, tableName.Text)
或者
MyAwesomeMethodThatDoesStuff(@"D:\MyAwesomePath", OutputResults.OUTPUT_TYPE_FILE)