File.AppendText problem

bungpeng

Well-known member
Joined
Sep 10, 2002
Messages
906
Location
Malaysia
I was wrote a simple function to write info to text file, but there is a error message: "Referenced object has a value of Nothing." in line 4, this is what I followed exactly the book... anyone can help?

Public Function WriteLog(ByVal sVal As String) As Boolean
WriteLog = False
Try
Dim oWriter As StreamWriter = File.AppendText(sFilePath)
oWriter.WriteLine(sVal)

WriteLog = True
Catch e As Exception

End Try
End Function
 
Try changing it to this:
Code:
        Dim oWriter As New IO.StreamWriter(sFilePath, True)
        oWriter.WriteLine(sVal)
Im not sure exactly what File.AppendText does, but my way
should work if you just want to append regular text.
 
It still the same, cannot work....

I wrote this function in a class and call by another class, it should not be the problem right? since everything is a class...
 
Are you first declaring the class as an object? For example,
Code:
Dim cls As New MyClass()

cls.WriteLog("Hey")
Where, MyClass is the class that the function you posted above
is in? Youre not just trying to do
Code:
MyClass.WriteLog("Hey")
Are you?
 
of course, I did declaring the class as an object.

I found the same function can be run in Windows project...

It cannot run (I use 3 tiers architecture):
1. This function is under a class project (dll file)
2. I use ASP.NET to call this function

I still dont understand why it cannot run
 
Back
Top