How to return multiple results/strings from a function

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi
I have the following code that reads the contents of a file into memory, splits the file into statements, manipulates each statement, appends to a string, then outputs the results into a new file:


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; string fileContents = <span style="color:Blue; null;

<span style="color:Blue; using (StreamReader fileReader = <span style="color:Blue; new StreamReader(inputFile))
{
fileContents = fileReader.ReadToEnd();
}

<span style="color:Green; // Create an array and insert each line from the string fileContents splitting on each GO statement
<span style="color:Blue; string[] sqlStatements = fileContents.Split(separators, StringSplitOptions.RemoveEmptyEntries);
StringBuilder builder = <span style="color:Blue; new StringBuilder();

<span style="color:Blue; foreach (<span style="color:Blue; string sqlStatement <span style="color:Blue; in sqlStatements)
{
ICompare compare = CompareFactory.Get(sqlStatement);

<span style="color:Blue; string result = compare.Execute(sqlStatement);
builder.Append(result);
}
[/code]
<br/>
Depending on the object type, a different function is used to manipulate the statement. At the moment, I return a string. however, I want to return the resulting sql, and also the PRINT statements:


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;

<span style="color:Blue; namespace SchemaComparison.Comparers
{
<span style="color:Blue; class AlterColumnComparer : ComparerBase
{
<span style="color:Blue; public <span style="color:Blue; override <span style="color:Blue; string Execute(<span style="color:Blue; string sql)
{
<span style="color:Blue; string columnName = GetArrayValue(sql, 4);
<span style="color:Blue; string tableName = GetObjectName(sql);

<span style="color:Blue; string printStatement = <span style="color:#A31515; "PRINT NAltering column " + columnName + <span style="color:#A31515; " on table " + tableName + <span style="color:#A31515; "...;";
<span style="color:Blue; string goStatement = <span style="color:#A31515; "GO";

sql = printStatement + Environment.NewLine + goStatement + Environment.NewLine + sql + Environment.NewLine + goStatement + Environment.NewLine;
<span style="color:Blue; return sql;
}
}
}

[/code]
<br/>
How do I return both sql and printStatement?? A colleague said to change the interface to return a class..... But I dont know how to do that.

Thanks (Im very new to c#) <hr class="sig Rebecca Database Developer

View the full article
 
Back
Top