Author Topic: [C#] Passing variables and arguments  (Read 5426 times)

0 Members and 1 Guest are viewing this topic.

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
[C#] Passing variables and arguments
« on: December 09, 2007, 03:07:16 pm »
So for whatever reason, I can't get my variables to pass to another class.


Trivia Manager

Code: [Select]
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

Code: [Select]
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.

« Last Edit: December 09, 2007, 03:11:13 pm by Dale »

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [C#] Passing variables and arguments
« Reply #1 on: December 09, 2007, 03:17:40 pm »
Where exactly are you passing the screen name in?  Cause I don't see it;
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Re: [C#] Passing variables and arguments
« Reply #2 on: December 09, 2007, 03:25:11 pm »
Where exactly are you passing the screen name in?  Cause I don't see it;

Code: [Select]
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
           

        }

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: [C#] Passing variables and arguments
« Reply #3 on: December 09, 2007, 05:25:49 pm »
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

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Re: [C#] Passing variables and arguments
« Reply #4 on: December 09, 2007, 05:33:01 pm »
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
Code: [Select]
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?
« Last Edit: December 09, 2007, 05:35:49 pm by Dale »

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: [C#] Passing variables and arguments
« Reply #5 on: December 09, 2007, 05:38:54 pm »
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

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Re: [C#] Passing variables and arguments
« Reply #6 on: December 09, 2007, 05:40:39 pm »
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.

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: [C#] Passing variables and arguments
« Reply #7 on: December 09, 2007, 05:55:43 pm »
So is the problem solved?
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

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Re: [C#] Passing variables and arguments
« Reply #8 on: December 09, 2007, 06:04:19 pm »
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 :)

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Re: [C#] Passing variables and arguments
« Reply #9 on: December 09, 2007, 06:23:20 pm »
How would I get the next line using Stream Reader?

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [C#] Passing variables and arguments
« Reply #10 on: December 09, 2007, 08:16:16 pm »
How would I get the next line using Stream Reader?
reader.ReadLine();
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Re: [C#] Passing variables and arguments
« Reply #11 on: December 09, 2007, 08:25:12 pm »
Weird, I did that in my last project, and it worked, but now it's not processing the next line.

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Re: [C#] Passing variables and arguments
« Reply #12 on: December 09, 2007, 10:23:03 pm »
Code: [Select]
     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?

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: [C#] Passing variables and arguments
« Reply #13 on: December 10, 2007, 09:41:44 pm »
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!

Offline Sidoh

  • x86
  • Hero Member
  • *****
  • Posts: 17634
  • MHNATY ~~~~~
    • View Profile
    • sidoh
Re: [C#] Passing variables and arguments
« Reply #14 on: December 10, 2007, 10:14:52 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#.