Channel Code (resolved)

Feb 10, 2008 at 9:28 PM
Hi all,
I've been playing with some communication channel code which I saw a request for on the sourceforge page.
I've run into a snag however, I'm trying to access a method that I wrote and put in a file channels.cs from the PlayerCommands.cs file.

Heres the code to clarify what I mean:

In my new file Channels.cs

using System;
using System.Collections;


namespace TigerMUD
{
/// <summary>
/// Description of Channels.
/// </summary>
public class Channel
{
public string name; // Display name of channel
public int type; // Temporary or Permanent
public string password; // Optional password
public ArrayList subscribers = new ArrayList(); // List of users
public ArrayList moderators = new ArrayList(); // users in charge of channel

/* This is the method I am trying to use from the other file, it should search the channels arraylist and return a channel, similar to how the GetActor method works */
public Channel FindChannelByName(string name)
{
foreach (Channel tempchan in Lib.channels)
{
if (tempchan.name.ToString().ToLower() == name.ToLower())
return tempchan;
}
return null;
}
}

In the PlayerCommands.cs file I added a new command called 'Channel' which I wanted to use to setup, subscribe to etc.. the channels.

Note: the method works fine when I paste it into the PlayerCommand.cs file with the new command - but in a desire to keep the channel code in a seperate .cs file and avoid cluttering up PlayerCommand.cs I've run into new problems

Here's how I've tried to access the method from my new command in PlayerCommands.cs:
Channel chan = FindChannelByName( words1.ToString() ); // Error: TigerMUD.Command_channel.FindChannelByName is a 'type' but is used like a 'variable' cs0118

Channel chan = (Channel).FindChannelByName(words1.ToString()); // Error, same as above

I was initially getting 'not part of the current context' errors, but I've 'progressed' to here =)
Any help would be greatly appreciated, oh and pretend you are explaining it to your Mum to make sure I get it right!
Cheers!
Coordinator
Feb 11, 2008 at 2:51 AM
I think the problem is that you don't have an instance of Channel where you can call the Find... method.

To use a class method you have to have create an instance of that class somewhere first using the "new" keyword. That is, unless you create a static method like this:

public static Channel FindChannelByName(string channelname)
{ ...}

Then in your code, you can just do this:

Channel chan=Channel.FindChannelByName(words1.ToString());


A different and non-static way to do this is to create an instance of a channel first so that you can call the method:
Channel chan=new Channel();
then access the method from that instance. You're returning the channel that you're looking for, so you need another channel instance to hold it:
Channel newchan=chan.FindChannelByName(words1.ToString());

Cheers!
Coordinator
Feb 11, 2008 at 3:00 AM
I should note that the preferred way to do this is using a static method as described first.

Normally you use static classes and methods when you just need to call one method and then be done with that class going forward.
For example, when you use String.Compare method. You wouldn't want to create an instance of a string in your code just to feed it two other string instances, but then never need the first instance again after that.

Cheers!
Adam
Feb 11, 2008 at 8:18 AM
Yes!
Thanks that worked perfectly.
Changed the methods to static and called them with the " Channel. " in front.

Now to break something else lol