News:

Wieners, Brats, Franks, we've got 'em all.

Main Menu

[C#] Passing variables and arguments

Started by abc, December 09, 2007, 03:07:16 PM

Previous topic - Next topic

0 Members and 4 Guests are viewing this topic.

abc

So for whatever reason, I can't get my variables to pass to another class.


Trivia Manager


using System;
using System.Collections.Generic;
using System.Text;
using Fluent.Toc;
using System.IO;
using System.Threading;

namespace FyreBot
{
    class TriviaManager : ConnectionManager
    {
        Program prg = new Program();
        public StreamReader Questions = new StreamReader("trivia.txt");
        //public StreamWriter HighScores = new StreamWriter("highscores.txt");

        private string message = null;
        private string from = null;
        private TocClient toc;
       
        public TocClient Toc
        {
            get { return toc; }
            set { toc = value; }
        }
       
        public string Msg
        {
            get { return message; }
            set { message = value; }
        }

        public string From
        {
            get { return from; }
            set { from = value; }
        }

       

        public TriviaManager()
        {
            Console.WriteLine(Msg);
       
        }

        public void Start()
        {
            Console.WriteLine("Trivia Manager: Started!");
            string buffer = Questions.ReadLine();
            char[] delimeter = { '*' };
            string[] Parsed = buffer.Split(delimeter);
            toc.Send(From, Parsed[0]);

         
            Console.WriteLine(Parsed[0]);
            Console.WriteLine(Parsed[1]);
            Console.WriteLine(Msg);
           
        }

        public void Stop()
        {
            Console.WriteLine("Trivia Manager: Trivia now deactivated!");
            // stop trivia
        }
     }
}


Connection Manager


using System;
using System.Collections.Generic;
using System.Text;
using Fluent.Toc;


namespace FyreBot
{
    public class ConnectionManager
    {

        //public Program clsDeclares = new Program();
        public TocClient TOCClient = new TocClient();

        public ConnectionManager()
        {
           
            TOCClient.Error += new ErrorEventHandler(TOCClient_Error);
            TOCClient.Disconnected += new EventHandler(TOCClient_Disconnected);
            TOCClient.Message += new MessageEventHandler(TOCClient_Message);
           
     
        }
       
        public void SignOn()
        {
            TOCClient.SignOn("UName", "PWord");
            Console.WriteLine("Logging in...");
            if (TOCClient.Connected)
            {
                Console.WriteLine("Logged in sucessfully!");
                TOCClient.StartListening();
            }
            Console.ReadKey();
        }

        public void TOCClient_Message(object sender, MessageEventArgs MsgEventArgs)
        {
            string message = TocUtility.StripHtml(MsgEventArgs.Message);
            TriviaManager Trivia = new TriviaManager();
           
            Trivia.Toc = TOCClient;
            Trivia.Msg = message;
            Trivia.From = MsgEventArgs.From;
           

        }

        protected void TOCClient_Error(object sender, ErrorEventArgs ErrorArgs)
        {
            Console.WriteLine("Error! " + ErrorArgs.Message);
        }

        protected void TOCClient_Disconnected(object sender, EventArgs e)
        {
            Console.WriteLine("Disconnected!");
        }

   

    }
}


Some reason, in Trivia manager, I can't get the Screen Name being passed, and the message.

yes, I'm aware my code sucks.


MyndFyre

Where exactly are you passing the screen name in?  Cause I don't see it;
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

abc

Quote from: MyndFyre on December 09, 2007, 03:17:40 PM
Where exactly are you passing the screen name in?  Cause I don't see it;


public void TOCClient_Message(object sender, MessageEventArgs MsgEventArgs)
        {
            string message = TocUtility.StripHtml(MsgEventArgs.Message);
            TriviaManager Trivia = new TriviaManager();
           
            Trivia.Toc = TOCClient;
            Trivia.Msg = message; <--------------------------------- this line
            Trivia.From = MsgEventArgs.From; <------------------- and this one
           

        }

Warrior

I'm trying to analyze it as best I can so here is what I've caught so far:

In your constructor for TriviaManager you Output the contents of message which will always be null because it's contents are initially null.

Remember that a constructor is called the moment a class is instantiated, so you'll need to pass the values as parameters of the constructors if you want them to be ready for use by the constructor method.

Same can be said with your Start() method in TriviaManager, you need to make sure that by the time that method is called that the values have been filled.

You should try setting some breakpoints in your code to figure out exactly what's going on.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

abc

#4
Yeah, I realized that when a user messages me, the TOCClient_Message() event is called, and I grab the values from MessageEventAgs.Message and throw them into Msg, and same for the From variable, then they should be found in the TriviaManager class, in those variables...

EDIT:

I threw in a breakpoint at Console.WriteLine(Msg); in the Start() function in TriviaManager, and the point is called, but it returns null, then I go back over to connection manager, and I see that the message is indeed, in the variable Msg, so some how before it gets to TriviaManager it's returning null?

Warrior

I don't see where you call TriviaManager.Start(). That's key.

I'm almost certain the issue is that Start() is being called before the variable is assigned.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

abc

Quote from: Warriorx86] link=topic=10812.msg137861#msg137861 date=1197239934]
I don't see where you call TriviaManager.Start(). That's key.

I'm almost certain the issue is that Start() is being called before the variable is assigned.

mm, hold on I might have it we'll see, I ended up calling Start() after the arguments where I assign the variables (which I thought I did before) and it's returning my text.

Warrior

One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

abc

I'm having some problems with getting the question, getting the right answer, and then getting the question again, but the other problem is solved. Thank you :)

abc

How would I get the next line using Stream Reader?

MyndFyre

Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

abc

Weird, I did that in my last project, and it worked, but now it's not processing the next line.

abc


     if (Msg.ToLower() == Parsed[1])
            {
                Console.WriteLine("Correct Answer!");
               
                goto Contin;
            }
            else
            {
                Console.WriteLine("Incorrect Answer!");
            }
        Contin:
            buffer = Questions.ReadLine();
            Console.WriteLine("\n" + Parsed[0]);
            Parsed = buffer.Split(delimeter);


For whatever reason it's not going to the next line? any ideas?

Camel

In Java, it is a standard to begin variables with a lower-case letter. If that standard is the same in C# (as it should be!), you should follow it. :)

Why are you using a completely unnecessary goto? If you remove the label and the goto, your code flow will not change.

Do strings in C# overload the == operator? In Java, == checks if the references are to the same object, not whether their values are equal.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

Sidoh

Quote from: Camel on December 10, 2007, 09:41:44 PM
Do strings in C# overload the == operator? In Java, == checks if the references are to the same object, not whether their values are equal.

I'm pretty sure it does.[1]  I use "overload" loosely, though, since strings are considered 'primitives' in C#.