Solved

Misc Compile errors. That I can't figure out!!

Posted on 2004-10-07
7
163 Views
Last Modified: 2012-05-05
What is this about! is this mutiple errors? Just one big one with many dependecies? Please please help.

___________________________
Service cannot be started. System.InvalidCastException: Cast from type 'StreamWriter' to type 'String' is not valid.
   at Microsoft.VisualBasic.CompilerServices.StringType.FromObject(Object Value)
     
   at PxpParser.PxpParser.WriteToFile(String pr_String) in C:\Thomas Huff\Visual Studio Projects\PxpParser\PxpParser.vb:line 209
 
   at PxpParser.PxpParser.StartWatch() in C:\Thomas Huff\Visual Studio Projects\PxpParser\PxpParser.vb:line 140
   
   at PxpParser.PxpParser.OnStart(String[] args) in C:\Thomas Huff\Visual Studio Projects\PxpParser\PxpParser.vb:line 363
   at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

------------------

1 - Line 209 =
logsw = CObj(logobject.OpenText(CObj(logobject.AppendText(logfile))))

2 - Line 140 =
        WriteToFile("Setting Folder to watch:" & watcher.Path() & "Done" & " @ " & Date.UtcNow & vbNewLine)

3 - Line 363 =
 StartWatch()


Please Assist. I can't solve! Thanks!
0
Comment
Question by:croag
  • 4
  • 3
7 Comments
 
LVL 18

Expert Comment

by:mdougan
ID: 12252565
Hi croag,
> WriteToFile
The writeToFile function does not take a string as a parameter, rather, it takes a StreamWriter object.  


If you just want to write a string to a file, you'd do something like this:
        Dim fWriter As StreamWriter

            fWriter = System.IO.File.CreateText("C:\Code\text.txt")
            fWriter.Write("Hello World")
            fWriter.Close()

Cheers!
0
 
LVL 1

Author Comment

by:croag
ID: 12253727
THANK YOU for your quick response. So here is the WriteToFile Fuction that I"m using and the arguments aren't throwing a syntax error. What should I do different withing the code.....Where it writes to the actual file below, Right now, It says logsw.WriteLine(pr_String) cause thats what the argument that the fuction calls from, do you think If I can change the argument type to streamwriter passed to the function and then change another couple of things in this funciton..it will work? Please let me know. Thank you.


-----------------------------------------------------------------------------------------

    Public Function WriteToFile(ByVal pr_String As String)
        Dim logsw As StreamWriter
        Dim logfile As String = logs & "\PxpParser.log"
        Dim logobject As File

        If logobject.Exists(logfile) Then
            logsw = CObj(logobject.OpenText(CObj(logobject.AppendText(logfile))))
        ElseIf Not logobject.Exists(logfile) Then
            logsw = (CObj(logobject.CreateText(CObj(logobject.OpenText(CObj(logobject.AppendText(logfile)))))))
        End If
   
     'Write to the file
        logsw.WriteLine(pr_String)

       
        'Set the Value of pr_String back to NULL - Just to be safe
                pr_String = ""

       
        'Close the Stream Writer
                logsw.Close()

    End Function
-------------------------------------------------------------------------------------------------
0
 
LVL 18

Expert Comment

by:mdougan
ID: 12254066
Ohhhhh..... that could be the problem... there is a .NET function named WriteToFile, and it's very likely that your compile error is checking your function call against the .NET WriteToFile function and not yours... a quick test would be to rename your MyWriteToFile and change all the calls to call that instead and see if you still get the compile error.

Second of all, it's not clear to me why you have all the cast to CObj statements in your code... I think you could get rid of those completely... secondly, on the Else condition for this if statement:

If logobject.Exists(logfile)

You don't have to say   Elseif.... because if the object.Exists is false, then by definition, it doesn't exist.  

Hope that helps!
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:croag
ID: 12254343
Oh man...Thank you..I'm going to try that one right now!

hmmmmmmmokay darn...same errors. Time to try taking out the Obj casting



NOpe...it loooks like i HAVE to use object casting..is it bad to cast so much like that?



Please hlep me more!!!
0
 
LVL 18

Accepted Solution

by:
mdougan earned 255 total points
ID: 12254642
OK, it always helps if I actually compile and test the solution first ;)  see changes below  

 Public Function WriteToFile(ByVal pr_String As String)
        Dim logsw As StreamWriter
        Dim logfile As String = "C:\PxpParser.log"
        Dim fi As New FileInfo(logfile)
        Dim logobject As File

        If logobject.Exists(logfile) Then
            logsw = fi.AppendText()
        Else
            logsw = System.IO.File.CreateText(logfile)
        End If

        'Write to the file
        logsw.WriteLine(pr_String)

        'Set the Value of pr_String back to NULL - Just to be safe
        'pr_String = ""   
        'This is not necessary, as you're passing pr_String by value so, it's going to disappear at the end of the procedure

        'Flush the buffer
        logsw.Flush()
        'Close the Stream Writer
        logsw.Close()

    End Function
0
 
LVL 1

Author Comment

by:croag
ID: 12261505
Dude....mdougan, You are the man!!!!!!!!! Thank you sooo much man. Thank you!!!!! that was it!
You are THE man!!!!!!!!!!!!!!!!! Thank you ..I may have a more questions for yoU!..Thank you!!!!. YES!..Thank you!..I am sooooo happy
0
 
LVL 18

Expert Comment

by:mdougan
ID: 12262406
sure, my pleasure :)  I've been away from EE for a while, nice to be back
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 Populate DataModel with DataTable 44 38
VB.Net WebRequest Code from JSON curl 7 53
get combo value in class 5 14
MYSQL responding very slow 3 27
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question