2011-01-05 30 views
1

私は解決する手がかりがない小さな問題があります。'string'を暗黙的に 'long'に変換できません。

{

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がある:あなたがそのやっはものを見ることができるように、クラスからの電子コード;私は与えるだろう "エラー1暗黙的に 'string'型を 'long'に変換できません"

これを解決するにはどうすればよいですか? コードは、C#で、ソフトウェアは、VS 2008 Proの

おかげ

答えて

3

TotalSingleScoreがInt64型として宣言されているので、あなたはInt64.Parseを使用する必要があります。

サンプル:、なぜあなたは毎回分割文字列dfr.TotalSingleScore = Int64.Parse(s.Split(',')[12]);

10

使用long.Parseまたはlong.TryParseです。

+1

これは、32ビットと64ビットのマシンで実行した場合とは異なる結果をもたらします。 Int64.parseがlongを返すInt64を明示的に使用しています。 int.Parse()!= long.Parse() –

+0

もちろんです。私は彼の説明でintを読むだけです。 – Femaref

+0

@Andrew、あなたは間違っています。 'int'は32ビットマシンであれ64ビットマシンであれ32ビットなので、結果は同じになります。 – svick

2

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

Int64.TryParse()

+0

Int63;)............. –

0

あなたの文字列を取り、それを変換します。 TryParseやParseのメソッドは、データがキャスト型でない場合にはうまく機能します。しかし、System.Convertを使ってこのようにすることもできます。

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

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

+0

Convert.ToInt64(string)は内部的にInt64.Parse(string)を呼び出します。ただし、Int64.Parse(または同等のint.Parse)を使用する場合は、解析時に使用されるグローバリゼーションと書式設定を指定できます。 – HABJAN

+0

Trueただし、Int64.Parseは文字列のみを受け取り、Convert.ToInt64はType.Parseをラップして他の型を受け入れます。しかし、私はParseまたはTryParseのmoresoを好む、ちょうど他の方法を言及する。 –

0

興味があるだけですか?それを一度split'edして配列を再利用すれば良いのではないでしょうか?

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 

私はこれを提案しなければならないと考えました。

関連する問題