无法隐式转换类型“字符串”到“长”有其他问题吗?

发布于 2024-10-10 16:23:06 字数 4393 浏览 4 评论 0原文

我有一个小问题,我不知道如何解决。我将给出类中的 who;e 代码,以便您可以看到它在做什么:

{

class CompetitorDataFile
{
    //collection of DataFileRecords - people
    public List<DataFileRecord> DataFileRecords;
    public string FileName = "";

    //called when DataFile created
    public CompetitorDataFile(string FileName)
    {
        //creates the collection
        DataFileRecords = new List<DataFileRecord>();
        this.FileName = FileName;
    }

    //fill DataFileRecords with stuff from text file
    public void ReadDataFile()
    {
        foreach (string s in File.ReadAllLines(FileName))
        {
            if (s == "") continue;
            DataFileRecord dfr = new DataFileRecord();
            dfr.Name = s.Split(',')[0];
            dfr.CPSA = s.Split(',')[1];
            dfr.PostCode = s.Split(',')[2];
            dfr.Rank = s.Split(',')[3];
            dfr.Score1 = s.Split(',')[4]; //new for score system
            dfr.Score2 = s.Split(',')[5]; //new for score system 
            dfr.Score3 = s.Split(',')[6]; //new for score system 
            dfr.Score4 = s.Split(',')[7]; //new for score system 
            dfr.Score5 = s.Split(',')[8]; //new for score system 
            dfr.Score6 = s.Split(',')[9]; //new for score system 
            dfr.Score7 = s.Split(',')[10]; //new for score system 
            dfr.Score8 = s.Split(',')[11]; //new for score system
            dfr.TotalSingleScore = s.Split(',')[12]; //new for score system
            DataFileRecords.Add(dfr);
        }
    }

    public int FindByCPSA(string CPSA)
    {
        //set index to 0
        int index = 0;
        //go through each record looking for CPSA number match
        foreach (DataFileRecord dfr in DataFileRecords)
        {
            if (dfr.CPSA.ToLower() == CPSA.ToLower())
            {
                //if it's found return the current index
                return index;
            }
            //increase index and move to next record
            index++;
        }
        //not found returning -1
        return -1;
    }

    //save DataFile records to text file
    public void SaveDataFile()
    {
        //delete backup file if found
        if (File.Exists(FileName+".bck")) 
            File.Delete(FileName+".bck");
        //make backup of existing
        if (File.Exists(FileName))
            File.Move(FileName, FileName + ".bck");
        //create a temporary array of string
        List<string> stringy = new List<string>(DataFileRecords.Count);
        //go through each DataFile record and create a single string line
        foreach (DataFileRecord dfr in DataFileRecords)
            stringy.Add(dfr.SingleString);
        //saves all strings to file
        File.WriteAllLines(FileName, stringy.ToArray());
    }
}

//a single record - one person
public class DataFileRecord
{
    public string Name;
    public string CPSA;
    public string PostCode;
    public string Rank;
    public string Score1; //new for score system
    public string Score2; //new for score system
    public string Score3; //new for score system
    public string Score4; //new for score system
    public string Score5; //new for score system
    public string Score6; //new for score system
    public string Score7; //new for score system
    public string Score8; //new for score system
    public Int64 TotalSingleScore; // used to get total score for one user

    public string SingleString
    {
        get
        {
            return string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}", Name, CPSA, PostCode, Rank, Score1, Score2, Score3, Score4, Score5, Score6, Score7, Score8); //,{4} and , Score are new for score system
        }
    }

    public string PoshSingleString
    {
        get
        {
            return string.Format("{0,-20}|{1,-10}|{2,-9}|{3,-9}|{4,2}|{5,2}|{6,2}|{7,2}|{8,2}|{9,2}|{10,2}|{11,2}", Name, CPSA, PostCode, Rank, Score1, Score2, Score3, Score4, Score5, Score6, Score7, Score8);
            //return SingleString.Replace(",0", ""); //may be used to replace 0
            //return SingleString.Replace(",", ", ");
        }

我对这行代码有问题

dfr.TotalSingleScore = s.Split(',')[12];

TotalSingleScore 是唯一的 int (所有其他都是字符串),因此我收到此错误: “错误 1 ​​无法将类型‘string’隐式转换为‘long’”

我该如何解决这个问题? 代码是C#,软件是VS 2008 Pro

谢谢

I have a little problem which I have no clue how to solve. I will give the who;e code from the class so you can see what its doing:

{

class CompetitorDataFile
{
    //collection of DataFileRecords - people
    public List<DataFileRecord> DataFileRecords;
    public string FileName = "";

    //called when DataFile created
    public CompetitorDataFile(string FileName)
    {
        //creates the collection
        DataFileRecords = new List<DataFileRecord>();
        this.FileName = FileName;
    }

    //fill DataFileRecords with stuff from text file
    public void ReadDataFile()
    {
        foreach (string s in File.ReadAllLines(FileName))
        {
            if (s == "") continue;
            DataFileRecord dfr = new DataFileRecord();
            dfr.Name = s.Split(',')[0];
            dfr.CPSA = s.Split(',')[1];
            dfr.PostCode = s.Split(',')[2];
            dfr.Rank = s.Split(',')[3];
            dfr.Score1 = s.Split(',')[4]; //new for score system
            dfr.Score2 = s.Split(',')[5]; //new for score system 
            dfr.Score3 = s.Split(',')[6]; //new for score system 
            dfr.Score4 = s.Split(',')[7]; //new for score system 
            dfr.Score5 = s.Split(',')[8]; //new for score system 
            dfr.Score6 = s.Split(',')[9]; //new for score system 
            dfr.Score7 = s.Split(',')[10]; //new for score system 
            dfr.Score8 = s.Split(',')[11]; //new for score system
            dfr.TotalSingleScore = s.Split(',')[12]; //new for score system
            DataFileRecords.Add(dfr);
        }
    }

    public int FindByCPSA(string CPSA)
    {
        //set index to 0
        int index = 0;
        //go through each record looking for CPSA number match
        foreach (DataFileRecord dfr in DataFileRecords)
        {
            if (dfr.CPSA.ToLower() == CPSA.ToLower())
            {
                //if it's found return the current index
                return index;
            }
            //increase index and move to next record
            index++;
        }
        //not found returning -1
        return -1;
    }

    //save DataFile records to text file
    public void SaveDataFile()
    {
        //delete backup file if found
        if (File.Exists(FileName+".bck")) 
            File.Delete(FileName+".bck");
        //make backup of existing
        if (File.Exists(FileName))
            File.Move(FileName, FileName + ".bck");
        //create a temporary array of string
        List<string> stringy = new List<string>(DataFileRecords.Count);
        //go through each DataFile record and create a single string line
        foreach (DataFileRecord dfr in DataFileRecords)
            stringy.Add(dfr.SingleString);
        //saves all strings to file
        File.WriteAllLines(FileName, stringy.ToArray());
    }
}

//a single record - one person
public class DataFileRecord
{
    public string Name;
    public string CPSA;
    public string PostCode;
    public string Rank;
    public string Score1; //new for score system
    public string Score2; //new for score system
    public string Score3; //new for score system
    public string Score4; //new for score system
    public string Score5; //new for score system
    public string Score6; //new for score system
    public string Score7; //new for score system
    public string Score8; //new for score system
    public Int64 TotalSingleScore; // used to get total score for one user

    public string SingleString
    {
        get
        {
            return string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11}", Name, CPSA, PostCode, Rank, Score1, Score2, Score3, Score4, Score5, Score6, Score7, Score8); //,{4} and , Score are new for score system
        }
    }

    public string PoshSingleString
    {
        get
        {
            return string.Format("{0,-20}|{1,-10}|{2,-9}|{3,-9}|{4,2}|{5,2}|{6,2}|{7,2}|{8,2}|{9,2}|{10,2}|{11,2}", Name, CPSA, PostCode, Rank, Score1, Score2, Score3, Score4, Score5, Score6, Score7, Score8);
            //return SingleString.Replace(",0", ""); //may be used to replace 0
            //return SingleString.Replace(",", ", ");
        }

I have a problem with this line of code

dfr.TotalSingleScore = s.Split(',')[12];

TotalSingleScore is the only int (all others are strings) as a result I get this error:
"Error 1 Cannot implicitly convert type 'string' to 'long'"

How do I go about solving this? Code is C# and software is VS 2008 Pro

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

陌若浮生 2024-10-17 16:23:06

使用long.Parselong.TryParse

use long.Parse or long.TryParse.

陈年往事 2024-10-17 16:23:06

由于 TotalSingleScore 被声明为 Int64,因此您必须使用 Int64.Parse。

示例:dfr.TotalSingleScore = Int64.Parse(s.Split(',')[12]);

Since TotalSingleScore is declared as Int64, you have to use Int64.Parse.

Sample: dfr.TotalSingleScore = Int64.Parse(s.Split(',')[12]);

盗琴音 2024-10-17 16:23:06

dfr.TotalSingleScore = Int64.Parse(s.Split(',')[12]);

Int64.TryParse()

dfr.TotalSingleScore = Int64.Parse(s.Split(',')[12]);

Int64.TryParse()

悲歌长辞 2024-10-17 16:23:06

获取您的字符串并将其转换。正如其他人所说,如果数据可能不是转换类型,TryParse 或 Parse 方法会非常有效。但您也可以使用 System.Convert 以这种方式完成此操作。

dfr.TotalSingleScore = Convert.ToInt64(s.Split(',')[12]);

http://msdn.microsoft.com/en-us/library /system.convert.toint64.aspx

Take your string and convert it. Like others stated the TryParse or Parse methods work great if the data might not be the casting type. But you can also do it this way using System.Convert.

dfr.TotalSingleScore = Convert.ToInt64(s.Split(',')[12]);

http://msdn.microsoft.com/en-us/library/system.convert.toint64.aspx

清醇 2024-10-17 16:23:06

只是好奇,为什么每次都要分割字符串?如果只拆分一次并重用数组不是更好吗?

        var myArray = s.split(',');
        dfr.Name = myArray[0];
        dfr.CPSA = myArray[1];
        dfr.PostCode = myArray[2];
        dfr.Rank = myArray[3];
        dfr.Score1 = myArray[4]; //new for score system
        dfr.Score2 = myArray[5]; //new for score system 
        dfr.Score3 = myArray[6]; //new for score system 
        dfr.Score4 = myArray[7]; //new for score system 
        dfr.Score5 = myArray[8]; //new for score system 
        dfr.Score6 = myArray[9]; //new for score system 
        dfr.Score7 = myArray[10]; //new for score system 
        dfr.Score8 = myArray[11]; //new for score system
        :
        etc

不是你问题的答案,但我想我必须建议这个。

Just curious, why are you splitting the string everytime? Wouldn't it be better if you just split it once and reuse the array?

        var myArray = s.split(',');
        dfr.Name = myArray[0];
        dfr.CPSA = myArray[1];
        dfr.PostCode = myArray[2];
        dfr.Rank = myArray[3];
        dfr.Score1 = myArray[4]; //new for score system
        dfr.Score2 = myArray[5]; //new for score system 
        dfr.Score3 = myArray[6]; //new for score system 
        dfr.Score4 = myArray[7]; //new for score system 
        dfr.Score5 = myArray[8]; //new for score system 
        dfr.Score6 = myArray[9]; //new for score system 
        dfr.Score7 = myArray[10]; //new for score system 
        dfr.Score8 = myArray[11]; //new for score system
        :
        etc

Not an answer for your question, but I thought I had to suggest this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文