Error using File.ReadAllLines

Error using File.ReadAllLines

Postby Leeizazombie » 17 Jul 2013, 14:13

Here is the command:
Code: Select all
using System;
using System.IO; 
    namespace MCDzienny
{
   public class CmdConstants : Command
   {
      public override string name { get { return "constants"; } }
      public override string shortcut { get { return ""; } }
      public override string type { get { return "other"; } }
      public override bool museumUsable { get { return false; } }
      public override LevelPermission defaultRank { get { return LevelPermission.Banned; } }
        public override void Use(Player p, string message)
        {
            Player.SendMessage(p, "&7Fetching Data...");
            Player.SendMessage(p, "&0-----------------------");
            try
            {

                string text = File.ReadAllLines("text/chat_constant$.txt");
                Player.SendMessage(p, text);

            }
            catch
            {
                Player.SendMessage(p,"Failed");

            }
        }
      public override void Help(Player p)
      {
         Player.SendMessage(p, "/constants - Does stuff.  Example command.");
      }
   }
}


It gives me the following error in errors.txt:
Code: Select all

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

Error CS0029
Message: Cannot implicitly convert type 'string[]' to 'string'
Line: 20
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.

Re: Error using File.ReadAllLines

Postby ismellike » 17 Jul 2013, 14:43

File.ReadAllLines(path) splits the content of a text file into different lines, or string[] (array).
An array is a collection of strings.
So, there is more than one string in string[].
You can't make something more than what it is supposed to be explicitly.
Take for example chars and strings.
A string is just a collection of characters.
You can't make a character equal to a string.
So what you can do is a foreach loop.

Ex:
Code: Select all
   foreach(string text in File.ReadAllLines(path))
     {
         Player.SendMessage(p,text);
     }


What that is saying is, for each string in the string[], it will tell you one string at a time.
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: Error using File.ReadAllLines

Postby Leeizazombie » 18 Jul 2013, 04:05

Thank you so much!
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.


Return to Help in Coding

Who is online

Users browsing this forum: No registered users and 2 guests

cron