2017-01-18 2 views
0

다음 코드에서 거의 동일한 두 개의 Select Cases가 있으며 모든 경우가 거의 동일합니다. 그들은 서로 다른 매개 변수를 사용하여 오버로드 된 생성자를 사용하기 때문에 단일 함수에 넣을 수있는 방법을 찾아 낼 수 없습니다. 모두에서이 작업을 수행 할 수있는 방법은 (이상적으로 엄격한 제네릭 또는 반사 및 옵션을 사용하지 않고?) 거기에반복적 인 선택 선택

Sub Main() 'for testing, this currently converts csv to csv 
    inputPath = Environment.GetCommandLineArgs(1) 
    inputType = Environment.GetCommandLineArgs(2).ToUpper 
    outputPath = Environment.GetCommandLineArgs(3) 
    outputType = Environment.GetCommandLineArgs(4).ToUpper 

    'Grab Input 
    Select Case inputType 
     Case "CSV" 
      inputSpreadSheet = New CSV(inputPath) 
     Case "XLS" 
      inputSpreadSheet = New XLS(inputPath) 
     Case "XLSX" 
      Throw New NotImplementedException() 
     Case "PIPE" 
      Throw New NotImplementedException() 
     Case Else 
      Throw New Exception(inputType & " Is not a valid input type.") 
    End Select 

    'Convert Input to Intermediate Format 
    intermediateSpreadSheet = inputSpreadSheet.ToIntermediate() 

    'Convert Intermediate to output Format 
    Select Case outputType 
     Case "CSV" 
      outputSpreadSheet = New CSV(intermediateSpreadSheet) 
     Case "XLS" 
      outputSpreadSheet = New XLS(intermediateSpreadSheet) 
     Case "XLSX" 
      Throw New NotImplementedException() 
     Case "PIPE" 
      Throw New NotImplementedException() 
     Case Else 
      Throw New Exception(outputType & " Is not a valid output type.") 
    End Select 

    'Generate output file 
    outputSpreadSheet.Export(outputPath) 
End Sub 

생성자의 매개 변수가 각각의 선택 블록의 서로 다른 유형의 때문에, 나는 어떤 방법을 알고 그들을 결합하기.

+1

실제로는 반복적이지 않으며, 2 가지 다른 것들을 테스트하고 다른 결과를 생성합니다. – Plutonix

답변

1

다른 것들을 테스트하고 다른 출력을 형성하기 때문에 실제로 그것을 압축하는 방법은 없습니다. "더 예쁜"코드의 경우 GetOutputSpreadSheet(outputType)GetInputSpreadSheet(inputType) 메서드를 만들어서 더 이상 볼 필요가 없습니다.

Public Static Function CreateSpreadSheet(ByVal path As String, ByVal type As String) As SpreadSheet 
    Select Case type 
    Case "CSV" 
     Return New CSV(path) 
    Case "XLS" 
     Return New XLS(path) 
    Case "XLSX" 
     Return NotImplementedException() 
    Case "PIPE" 
     Return NotImplementedException() 
    Case Else 
     Throw New Exception(type & " is not a valid output type") 
    End Select 
End Function 

: 두 블록이 입력으로 경로를 가지고가는 경우에

1

(이 문자열있어 가정) 및 출력 시트에 대한 입력 한 후 일부 개체를 반환 형식 스프레드 시트의 말, 당신은 다음과 같은 기능을 가질 수있다 다음과 같이 사용

이 CSV, XLS의 공통 조상 일부라는 클래스 스프레드 시트 등

는 것을, 또한이 있다고 가정

Sub Main() 'for testing, this currently converts csv to csv 
    inputPath = Environment.GetCommandLineArgs(1) 
    inputType = Environment.GetCommandLineArgs(2).ToUpper 
    outputPath = Environment.GetCommandLineArgs(3) 
    outputType = Environment.GetCommandLineArgs(4).ToUpper 

    'Grab Input 
    inputSpreadSheeet = CreateSpreadSheet(inputPath, inputType); 

    'Convert Input to Intermediate Format 
    intermediateSpreadSheet = inputSpreadSheet.ToIntermediate(); 

    'Convert Intermediate to output Format 
    outputSpreadSheet = CreateSpreadSheet(intermediateSpreadSheet.GetPath(), outputType); 

    'Generate output file 
    outputSpreadSheet.Export(outputPath) 
End Sub 

SP readSheet 클래스에는 경로를 반환하는 GetPath 메서드가 있으므로 중간 스프레드 시트의 파일 위치를 요청할 수 있습니다. 즉, ToIntermediate가 새 CSV 등이로드하도록 이해하는 중간 형식으로 디스크에 저장한다고 가정합니다. 귀하의 코드에서 동일한 생성자가 경로를 벗어난 객체를 받아들이는 것처럼 보였습니다. 그 때문에 두 개의 select 문을 병합 할 수 없었습니다. 첫 번째 매개 변수는 경로 매개 변수로 객체를 구성했고 두 번째 매개 변수는 스프레드 시트 객체 매개 변수.

임시 스프레드 시트를 파일로 저장하지 않으려면 분명히 두 개의 select 문을 병합 할 수 없습니다 (코드 블록에 공통 입력 및 출력 유형이 있어야 단일 재사용 가능 문자열로 리팩토링 할 수 있음). 함수)