2016-06-12 14 views
0

Diese Methode Teil meiner abgeleiteten Klasse von DataGridViewComboBoxColumn ist:ein DataGridViewComboBoxColumn Element in ein anderes Objekt Casting nicht

public ComboboxColourItem InsertColour(ushort iColourIndex) 
{ 
    ComboboxColourItem ocbItem = ComboboxColourItem.Create(iColourIndex); 

    bool bAppend = true; 
    if (Items.Count > 15) 
    { 
     // There are other colours, need to find right index 
     for(int i = 15; i < Items.Count; i++) 
     { 
      //if(ocbItem.Index < (ComboboxColourItem)Items[i].Index) 
      //{ 
      //} 
      ComboboxColourItem ocbItem2 = (ComboboxColourItem)Items[i]; 
      if (ocbItem.Index < ocbItem2.Index) 
      { 
       bAppend = false; 
       Items.Insert(i, ocbItem); 
       break; 
      } 
     } 
    } 
    if (bAppend) 
     Items.Add(ocbItem); 

    return ocbItem; 
} 

Die Items enthalten ComboboxColourItem Objekte. Hier ist die Definition dieser Elemente:

public class ComboboxColourItem 
{ 
    public string Name { get; set; } 
    public ushort Index { get; set; } 
    public Color Value { get; set; } 

    public ComboboxColourItem(string Name, ushort Index, Color Value) 
    { 
     this.Name = Name; 
     this.Index = Index; 
     this.Value = Value; 
    } 
    public override string ToString() 
    { 
     return Name; 
    } 

    static public ComboboxColourItem Create(ushort iColourIndex) 
    { 
     OdCmColor oColour = new OdCmColor(); 

     oColour.setColorIndex(iColourIndex); 

     CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture; 
     TextInfo textInfo = cultureInfo.TextInfo; 

     String strColour = textInfo.ToTitleCase(oColour.colorNameForDisplay()); 
     if (iColourIndex < 8) 
      strColour = String.Format("{0} ({1})", strColour, iColourIndex); 
     else if (iColourIndex == 8 || iColourIndex == 9 || iColourIndex >= 250) 
      strColour = String.Format("Grey Shade ({0})", iColourIndex); 
     else 
      strColour = String.Format("Other ({0})", iColourIndex); 
     ComboboxColourItem oColourItem = new ComboboxColourItem(
      strColour, 
      iColourIndex, 
      Color.FromArgb(oColour.red(), oColour.green(), oColour.blue())); 

     oColour.Dispose(); 

     return oColourItem; 
    } 
} 

Ich weiß, dass ich foreach(ComboboxColourItem ocbItem2 in Items) verwenden können, aber ich muss ab einem bestimmten Index starten. Also entschied ich mich für eine normale for Schleife.

Ich dachte, dass ich dies tun könnte das Element aus object würfen

if(ocbItem.Index < (ComboboxColourItem)Items[i].Index) 
{ 
} 

nicht wie die Besetzung. Aber wenn ich das tue:

ComboboxColourItem ocbItem2 = (ComboboxColourItem)Items[i]; 
if (ocbItem.Index < ocbItem2.Index) 
{ 
} 

Das funktioniert perfekt. Warum konnte ich nicht casten? Habe ich es falsch gemacht? Ich glaube nicht, dass ich foreach in dieser Situation verwenden kann.

Antwort

1

Da das Mitglied Zugriff hat höhere Priorität als die Besetzung (C# operator precedence), ist die folgende

(ComboboxColourItem)Items[i].Index 

entspricht

(ComboboxColourItem)(Items[i].Index) 

natürlich die ungültig ist.

verwenden stattdessen

((ComboboxColourItem)Items[i]).Index 
+0

Excellent! Danke für die kurze Erklärung. –

Verwandte Themen