convert from c to vb please

  • Thread starter Thread starter cj
  • Start date Start date
C

cj

Can someone help me convert this to vb please?

private void CreateLogFile()
{
StreamWriter sw = null;

try
{
sw = new StreamWriter(@"D:\Samples\Test.txt",
true, System.Text.UTF8Encoding.UTF8);

sw.WriteLine("This is some text");
}
finally
{
if (sw != null)
{
sw.Close();
}
}
}

I'm stuck on "StreamWriter sw = null;" I think it should be "Dim sw as
streamwriter" but how do I set it to null so I can check to see if it
was opened or not in the finally block?
 
cj,

Did you know that it is bad C# code.
The error from the catch in not catched, I assume that you know that?

StreamWriter sw = null;
Is not necessary in VB.Net
Dim sw as Streamwriter is enough,

However you need it in that finaly block as well and than you can use that
nothing as Johnny showed you..

Cor
 
Cor Ligthert said:
Did you know that it is bad C# code.
The error from the catch in not catched, I assume that you know that?

I don't see any reason why this should be bad code in general. Note that
the 'finally' block is always executed and the exception simply bubbles up.
 
And what happens as the file is not there or already in use?

Nobody knows.

An exception will be thrown and since it is not caught in this method,
it will bubble up to the calling code.

Chris
 
Back
Top