convert from c to vb please

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?
 
C

Cor Ligthert [MVP]

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
 
H

Herfried K. Wagner [MVP]

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.
 
C

Cor Ligthert [MVP]

And what happens as the file is not there or already in use?

Nobody knows.
 
C

Chris Dunaway

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top