Public Function GetTransformation(ByVal xmldata As String, ByVal Xsltpath As String) As String Dim writer As StringWriter = New StringWriter() Dim strXsl As String = String.Empty Try Dim xslt As New XslCompiledTransform(False) xslt.Load(Path.Combine(Application.StartupPath, Xsltpath)) Dim XmlReader As XPathDocument = New XPathDocument(New StringReader(xmldata)) xslt.Transform(XmlReader, Nothing, writer) strXsl = writer.ToString() writer.Flush() writer.Dispose() Return strXsl Catch generatedExceptionName As Exception Throw End Try End Function
I have the above function to transform an xml to string.
Can I Use the return statement as like below given
return writer.ToString()
instead of the below given expert of the function
strXsl = writer.ToString() writer.Flush() writer.Dispose() Return strXsl
which is right for minimize use of memory.
what happened with the variables and streams and etc. which are declared in function after executing function?
Yes, you can absolutely use
StringWriterIDisposableimplementations anywayThe better solution is to use a
Usingstatement for the writer, so it always gets disposed. Get rid of the pointlessCatchblock, and just change your code to:You should very rarely be calling
Disposeexplicitly – you should almost always use aUsingstatement instead.