Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Debugging C# app

Options
  • 01-03-2004 5:19pm
    #1
    Registered Users Posts: 2,758 ✭✭✭


    The following is a code sample taken directly from MSDN online library.
    private void cmdLogin_Click(object sender, System.EventArgs e)
    		{    // Create a file for output named TestFile.txt.
        Stream myFile = File.Create("TestFile.txt");
     
        /* Create a new text writer using the output stream, and add it to
         * the trace listeners. */
        TextWriterTraceListener myTextListener = new 
           TextWriterTraceListener(myFile);
        Trace.Listeners.Add(myTextListener);
     
        /* Create a text writer that writes to the console screen, and add
         * it to the trace listeners */
        TextWriterTraceListener myWriter = new 
           TextWriterTraceListener(System.Console.Out);
        Trace.Listeners.Add(myWriter);
     
        // Write output to the file and to the console screen.
        Trace.Write("Test output ");
     
        // Write only to the console screen.
        myWriter.WriteLine("Write only to the console screen.");
     
        // Flush and close the output.
        Trace.Flush(); 
        myWriter.Flush();
        myWriter.Close();
    

    Howevern when i go to run the code within my app the compiler doesn't recognise Stream/TextWriterTraceListener/some others....

    I get the feeling i'm missing a reference as this code is in a book i am reading as well but neither msdn or my book* remarks on what classes you need to reference in your project to make it work.

    All i'm trying to do is create a way of logging to a file throughout the app incase an error occurs.

    I want to be able to something like Debug.Log("User logged in at"&system time");
    What would be the best way to structure this so its easy to use and can be accessed from all forms/parts of my application?

    A little help over here!?!

    *The book is called "Developing Windows based Applications with MS Vicual C#"
    It is for the MCSD exam...


Comments

  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    add this to the top of your .cs file

    using System.Diagnostics;
    using System.Diagnostics.TraceListener;
    using System.IO.Stream;

    That should take care of the errors


  • Registered Users Posts: 2,758 ✭✭✭Peace


    sjones,

    I think thats most of the way there...but now the compiler is spouting out
    \frmLogin.cs(8): A using namespace directive can only be applied to namespaces; 'System.IO.Stream' is a class not a namespace
    \frmLogin.cs(7): A using namespace directive can only be applied to namespaces; 'System.Diagnostics.TraceListener' is a class not a namespace
    
    

    So the TraceLister and IO.Stream are classes... So if i create an instance of these classes in my .CS file then i'm good to go?


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    aye, instead of
    TextWriterTraceListener myTextListener = new TextWriterTraceListener(myFile);

    do System.Diagnostics.TextWriterTraceListener bleh = new TextWriterTraceListener(myFile);

    and so on... You get the idea!


  • Registered Users Posts: 2,758 ✭✭✭Peace


    Sound man sjones...job done!


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    Not a bother


  • Advertisement
  • Registered Users Posts: 2,758 ✭✭✭Peace


    In need of a little more help here folks...

    I have a class - "Suite" - and i want to create an instance of the class say "MySuite" so that i can use it throughout the application.
    The Suite class logs information to the Windows Event Log so i just want to be able to call MySuite.log() or MySuite.Error() where ever in the application and hey presto i have accsess to it.

    The problem:
    I have the declaration for Suite "Suite MySuite = new Suite();" in the form load even of the main form. I've made the load event public.... (not the best way to do things but i just want to get this thing done and move on!)
    Now the MySuite object can only be accessed inside the Formload event...once i try and use it in say another form (login form) or another event within the main form the compiler says
    "\frmMain.cs(173): The type or namespace name 'Mysuite' could not be found (are you missing a using directive or an assembly reference?)"

    I don't understand why i can't pick up the definition if the functionits declared inside is public....??

    As usual i'm missing something but what!?

    <edit>
    I also tried this...
    public void frmMain_Load(object sender, System.EventArgs e)
    {
    	Init();
    			
    	Mysuite.Log("B.A.S. Loading.");
    			
    }
    public void Init()
    {
    	Suite Mysuite = new Suite();
    			
    }
    

    It gives exactly the same result...


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Your problem is that Suite itself isn't public and module-level. There's several ways of doing this, but here's one anyway....
    public Suite MySuite;
    public void frmMain_Load(object sender, System.EventArgs e)
    {
      Mysuite = new Suite();
      Mysuite.Log("B.A.S. Loading.");
    
    }
    

    Then assuming you have somewhere something like :
    frmMain myFrmMain = new frmMain():
    

    you can then go something like :
    myFrmMain.MySuite.Log(...);
    

    Of course, there's a number of limitations with this - the foremest being that you can only ever use it when you have a reference to frmMain. So if your frmMain not created by your splash (which it would never be the way I structure my code, but thats a personal thing), this won't directly solve your problem....but it gets you most of the way there.

    Generally, I would recommend that your log class (Suite) is better defined using static methods or using a singleton pattern, so that you can then access it from anywhere without having to have a reference the instance instantiated in frmMain.

    But I'm not writing that code :)

    jc


  • Registered Users Posts: 2,758 ✭✭✭Peace


    Well that got it...nice one bonkey.

    I also nearly smashed something when i realise i was creating the object MySuite and then trying to use an instance of the pbject called Mysuite

    Check out the capital S....

    I swear VB programming de-evolves your programming skills. Thank god i'm all growed up now.


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Originally posted by Peace
    I swear VB programming de-evolves your programming skills.

    Oh, I dunno. I'm still pretty convinced that case-sensitivity is a waste of time.

    But maybe I've spent too much time writing SQL and coding VB :)

    jc


Advertisement