2011-06-28 14 views
0

私はこの質問を最後にしたときに解決されなかったので、これを投稿しています。これを私自身で解決することはできません。私はC#とプログラミングに非常に新しいので、自分自身を教えようとし、あなたが与えることができれば偉大になるように助けようとしています。エラーのように無効なキャスト例外

タイプ のオブジェクトをキャストすることができません 'System.Collections.Generic.List 1[test.Form1+comoditys]' to type 'System.Collections.Generic.List 1 [test.Form2 + mycost]'。

フォーム2コード:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 
using System.Runtime.Serialization.Formatters.Binary; 

namespace test 
{ 
public partial class Form2 : Form 
{ 
    public float Alligor; 
    public float Briochit; 
    public float Chollonin; 
    public float Espitium; 
    public float Hydrobenol; 
    public float Isopropenetol; 
    public float Metachropin; 
    public float Phlobotil; 
    public float Plasteosine; 
    public float Polynitrocol; 
    public float Polynucleit; 
    public float Prilumium; 
    public float Statchanol; 
    public float Titanium; 
    public float Vitricyl; 

    public float AlligorInput; 
    public float BriochitInput; 
    public float CholloninInput; 
    public float EspitiumInput; 
    public float HydrobenolInput; 
    public float IsopropenetolInput; 
    public float MetachropinInput; 
    public float PhlobotilInput; 
    public float PlasteosineInput; 
    public float PolynitrocolInput; 
    public float PolynucleitInput; 
    public float PrilumiumInput; 
    public float StatchanolInput; 
    public float TitaniumInput; 
    public float VitricylInput; 

    public float costofAlligor; 
    public float costofBriochit; 
    public float costofChollonin; 
    public float costofEspitium; 
    public float costofHydrobenol; 
    public float costofIsopropenetol; 
    public float costofMetachropin; 
    public float costofPhlobotil; 
    public float costofPlasteosine; 
    public float costofPolynitrocol; 
    public float costofPolynucleit; 
    public float costofPrilumium; 
    public float costofStatchanol; 
    public float costofTitanium; 
    public float costofVitricyl; 

    public double totalCost; 

    [Serializable] 
    public class mycost 
    { 
     public float secondPrice; 
    } 

    private List<mycost> costList = new List<mycost>(); 

    private mycost o1 = null; 
    private mycost o2 = null; 
    private mycost o3 = null; 
    private mycost o4 = null; 
    private mycost o5 = null; 
    private mycost o6 = null; 
    private mycost o7 = null; 
    private mycost o8 = null; 
    private mycost o9 = null; 
    private mycost o10 = null; 
    private mycost o11 = null; 
    private mycost o12 = null; 
    private mycost o13 = null; 
    private mycost o14 = null; 
    private mycost o15 = null; 

    public Form2() 
    { 
     InitializeComponent(); 

     o1 = new mycost(); 
     o2 = new mycost(); 
     o3 = new mycost(); 
     o4 = new mycost(); 
     o5 = new mycost(); 
     o6 = new mycost(); 
     o7 = new mycost(); 
     o8 = new mycost(); 
     o9 = new mycost(); 
     o10 = new mycost(); 
     o11 = new mycost(); 
     o12 = new mycost(); 
     o13 = new mycost(); 
     o14 = new mycost(); 
     o15 = new mycost(); 

     costList.Add(o1); 
     costList.Add(o2); 
     costList.Add(o3); 
     costList.Add(o4); 
     costList.Add(o5); 
     costList.Add(o6); 
     costList.Add(o7); 
     costList.Add(o8); 
     costList.Add(o9); 
     costList.Add(o11); 
     costList.Add(o12); 
     costList.Add(o13); 
     costList.Add(o14); 
     costList.Add(o15); 

     FileStream df = new FileStream("comoditys.dat", FileMode.Open); 
     BinaryFormatter abf = new BinaryFormatter(); 
     costList = (List<mycost>)abf.Deserialize(df); 
     df.Close(); 

     if (costList != null) 
     { 
      if (costList.Count > 0) 
       Alligor = costList[0].secondPrice; 

      if (costList.Count > 1) 
       Briochit = costList[1].secondPrice; 

      if (costList.Count > 2) 
       Chollonin = costList[2].secondPrice; 

      if (costList.Count > 3) 
       Espitium = costList[3].secondPrice; 

      if (costList.Count > 4) 
       Hydrobenol = costList[4].secondPrice; 

      if (costList.Count > 5) 
       Isopropenetol = costList[5].secondPrice; 

      if (costList.Count > 6) 
       Metachropin = costList[6].secondPrice; 

      if (costList.Count > 7) 
       Phlobotil = costList[7].secondPrice; 

      if (costList.Count > 8) 
       Plasteosine = costList[8].secondPrice; 

      if (costList.Count > 9) 
       Polynitrocol = costList[9].secondPrice; 

      if (costList.Count > 10) 
       Polynucleit = costList[10].secondPrice; 

      if (costList.Count > 11) 
       Prilumium = costList[11].secondPrice; 

      if (costList.Count > 12) 
       Statchanol = costList[12].secondPrice; 

      if (costList.Count > 13) 
       Titanium = costList[13].secondPrice; 

      if (costList.Count > 14) 
       Vitricyl = costList[14].secondPrice; 

     } 

    } 

    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 
     // 1st text box input is float 
     float tempFloat; 



     if (float.TryParse(textBox1.Text, out tempFloat)) 
     { 

      AlligorInput = tempFloat; 

     } 
     else 
      MessageBox.Show("uh oh"); 



    } 

    private void textBox2_TextChanged(object sender, EventArgs e) 
    { 
     // 1st text box input is float 
     float tempFloat; 



     if (float.TryParse(textBox1.Text, out tempFloat)) 
     { 

      BriochitInput = tempFloat; 

     } 
     else 
      MessageBox.Show("uh oh"); 



    } 

    private void textBox3_TextChanged(object sender, EventArgs e) 
    { 
     // 1st text box input is float 
     float tempFloat; 



     if (float.TryParse(textBox1.Text, out tempFloat)) 
     { 

      CholloninInput = tempFloat; 

     } 
     else 
      MessageBox.Show("uh oh"); 



    } 

    private void textBox4_TextChanged(object sender, EventArgs e) 
    { 
     // 1st text box input is float 
     float tempFloat; 



     if (float.TryParse(textBox1.Text, out tempFloat)) 
     { 

      EspitiumInput = tempFloat; 

     } 
     else 
      MessageBox.Show("uh oh"); 



    } 

    private void textBox5_TextChanged(object sender, EventArgs e) 
    { 
     // 1st text box input is float 
     float tempFloat; 



     if (float.TryParse(textBox1.Text, out tempFloat)) 
     { 

      HydrobenolInput = tempFloat; 

     } 
     else 
      MessageBox.Show("uh oh"); 



    } 

    private void textBox6_TextChanged(object sender, EventArgs e) 
    { 
     // 1st text box input is float 
     float tempFloat; 



     if (float.TryParse(textBox1.Text, out tempFloat)) 
     { 

      IsopropenetolInput = tempFloat; 

     } 
     else 
      MessageBox.Show("uh oh"); 



    } 

    private void textBox7_TextChanged(object sender, EventArgs e) 
    { 
     // 1st text box input is float 
     float tempFloat; 



     if (float.TryParse(textBox1.Text, out tempFloat)) 
     { 

      MetachropinInput = tempFloat; 

     } 
     else 
      MessageBox.Show("uh oh"); 



    } 

    private void textBox8_TextChanged(object sender, EventArgs e) 
    { 
     // 1st text box input is float 
     float tempFloat; 



     if (float.TryParse(textBox1.Text, out tempFloat)) 
     { 

      PhlobotilInput = tempFloat; 

     } 
     else 
      MessageBox.Show("uh oh"); 



    } 

    private void textBox9_TextChanged(object sender, EventArgs e) 
    { 
     // 1st text box input is float 
     float tempFloat; 



     if (float.TryParse(textBox1.Text, out tempFloat)) 
     { 

      PlasteosineInput = tempFloat; 

     } 
     else 
      MessageBox.Show("uh oh"); 



    } 

    private void textBox10_TextChanged(object sender, EventArgs e) 
    { 
     // 1st text box input is float 
     float tempFloat; 



     if (float.TryParse(textBox1.Text, out tempFloat)) 
     { 

      PolynitrocolInput = tempFloat; 

     } 
     else 
      MessageBox.Show("uh oh"); 



    } 

    private void textBox11_TextChanged(object sender, EventArgs e) 
    { 
     // 1st text box input is float 
     float tempFloat; 



     if (float.TryParse(textBox1.Text, out tempFloat)) 
     { 

      PolynucleitInput = tempFloat; 

     } 
     else 
      MessageBox.Show("uh oh"); 



    } 

    private void textBox12_TextChanged(object sender, EventArgs e) 
    { 
     // 1st text box input is float 
     float tempFloat; 



     if (float.TryParse(textBox1.Text, out tempFloat)) 
     { 

      PrilumiumInput = tempFloat; 

     } 
     else 
      MessageBox.Show("uh oh"); 



    } 

    private void textBox13_TextChanged(object sender, EventArgs e) 
    { 
     // 1st text box input is float 
     float tempFloat; 



     if (float.TryParse(textBox1.Text, out tempFloat)) 
     { 

      StatchanolInput = tempFloat; 

     } 
     else 
      MessageBox.Show("uh oh"); 



    } 

    private void textBox14_TextChanged(object sender, EventArgs e) 
    { 
     // 1st text box input is float 
     float tempFloat; 



     if (float.TryParse(textBox1.Text, out tempFloat)) 
     { 

      TitaniumInput = tempFloat; 

     } 
     else 
      MessageBox.Show("uh oh"); 



    } 

    private void textBox15_TextChanged(object sender, EventArgs e) 
    { 
     // 1st text box input is float 
     float tempFloat; 



     if (float.TryParse(textBox1.Text, out tempFloat)) 
     { 

      VitricylInput = tempFloat; 

     } 
     else 
      MessageBox.Show("uh oh"); 



    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     costofAlligor = Alligor * AlligorInput; 
     costofBriochit = Briochit * BriochitInput; 
     costofChollonin = Chollonin * CholloninInput; 
     costofEspitium = Espitium * EspitiumInput; 
     costofHydrobenol = Hydrobenol * HydrobenolInput; 
     costofIsopropenetol = Isopropenetol * IsopropenetolInput; 
     costofMetachropin = Metachropin * MetachropinInput; 
     costofPhlobotil = Phlobotil * PhlobotilInput; 
     costofPlasteosine = Plasteosine * PlasteosineInput; 
     costofPolynitrocol = Polynitrocol * PolynitrocolInput; 
     costofPolynucleit = Polynucleit * PolynucleitInput; 
     costofPrilumium = Prilumium * PrilumiumInput; 
     costofStatchanol = Statchanol * StatchanolInput; 
     costofTitanium = Titanium * TitaniumInput; 
     costofVitricyl = Vitricyl * VitricylInput; 

     totalCost = costofAlligor + costofBriochit + costofChollonin + costofEspitium + costofHydrobenol + costofIsopropenetol + costofMetachropin + costofPhlobotil + costofPlasteosine + costofPolynitrocol + costofPolynucleit + costofPrilumium + costofStatchanol + costofTitanium + costofVitricyl; 

    } 

    private void textBox16_TextChanged(object sender, EventArgs e) 
    { 
     textBox13.Text = totalCost.ToString(); 

     } 
    } 
    } 

form1 can be found here

答えて

1
FileStream df = new FileStream("comoditys.dat", FileMode.Open); 
    BinaryFormatter abf = new BinaryFormatter(); 
    costList = (List<mycost>)abf.Deserialize(df); 

あなたがcomoditiys.datを開いているように見えますし、それがList<comoditys>にデシリアライズができますが、List<mycost>に間違ってそれらをキャストしています。

EDIT:

はシリアライズさList<comoditys>が含まれており、List<mycost>

EDITに直列化復元結果をキャストしようとしているcomoditys.datそれの顔には、この

List<comodity> comodities = (List<comodity>)abf.Deserialize(df); 
costList = comodities.Select(c => new mycost{secondPrice = c.comodityCost}).ToList(); 
+0

はい、私はcomoditys.datの内容をリストに行く必要があります私はこれを行う方法はありますか? – doc

+0

@doc私の編集を試してください。 –

+0

私はそれを修正する方法のアイデアを与えるので、多くの芽ありがとう – doc

0

私はここで推測を作ってるんだ:

abf.Deserialize(df)リターンを何?

List<mycost>などを返しますか?

+0

はいそれはファイルに保存されているものであるので、それはリストを返す必要があり、それは – doc

+0

一覧または他の何らかのリスト Viv

+0

を読み込み、それが何を返すのですか?その点までデバッグしてみてください...私は復帰リストではないと推測しています Viv

0

comoditysのリストをmycostのリストにキャストしようとしているようです。あなたはそれをすることはできません。あなたのコードのサイズを考慮して、他の支援をするのは難しいです。発生しているエラーを最小限に抑えるように最小化しようとする必要があります。

+0

私はそれが起こっていると思います...リストcomoditysは、リストmycostは、同じファイルから読み取りますが、リストを別の人が読むことができない場合、それを修正する方法を知っているファイルを保存する? – doc

1

をお試しください

コメントあたりのLinqの例では、comoditysオブジェクトがにどのようにマップされているのかわかりませんオブジェクトのように、プロパティ名は単なる例に過ぎません。

MyComoditysList.Select(x => new mycost() 
{ 
    myCostProp1 = comoditysProp1, 
    myCostProp2 = comoditysProp2, 
    myCostProp3 = comoditysProp3 
}).ToList(); 
+0

はい私は何をしようとしているが表示されているエラーが表示されている – doc

+0

あなたはできないので、キャストの例外。あなたはそれをリスト化する必要があります次にLinqを使用してリストに変換する

+0

これは例外を出すことはありませんか?任意のヒントや情報は素晴らしいでしょう – doc

関連する問題