Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm converting my cards game to C#

In one case which appears very often I get error

Fehler CS1061 "ItemCollection" enthält keine Definition für "Item", und es konnte keine zugängliche Item-Erweiterungsmethode gefunden werden, die ein erstes Argument vom Typ "ItemCollection" akzeptiert (möglicherweise fehlt eine using-Direktive oder ein Assemblyverweis).

Error CS1061 "ItemCollection" does not contain a definition for "Item" and could not find an accessible item extension method that accepts an initial argument of type "ItemCollection" (a using directive or assembly reference may be missing).

The problem appears for "withBlock.Item(n)"

VB:
<Extension()>
      Public Function SelectFirstCardCommonCheck_ALL(hc As HandCards,
                                               Wnd As MainWindow, ByVal CardsPanel As Object,
                                               TrumpID As Integer, LeadSuitID As Integer) As PlayingCard

          Dim sHand As String
          Dim FindCard As PlayingCard
          Dim ACP As New AutoCardPlay(Wnd.sk.trumpCard, Wnd)

          sHand = hc.GetHandString

          With CardsPanel.Items
              sHand = String.Empty
              If .Count > 0 Then
                  For n = 0 To .Count - 1
                      If Wnd.iTricks > 5 Then
                          If NonTrumpCards(hc, TrumpID) IsNot Nothing Then
                              If NonTrumpCards(hc, TrumpID).Contains(GetEichelHighestPlayableCard(Wnd.TrickHistory)) Then
                                  If .Item(n).ToString = GetEichelHighestPlayableCard(Wnd.TrickHistory) Then FindCard = .Item(n)
                              End If
                              If NonTrumpCards(hc, TrumpID).Contains(GetLaubHighestPlayableCard(Wnd.TrickHistory)) Then
                                  If .Item(n).ToString = GetLaubHighestPlayableCard(Wnd.TrickHistory) Then FindCard = .Item(n)
                              End If
                              If NonTrumpCards(hc, TrumpID).Contains(GetSchellHighestPlayableCard(Wnd.TrickHistory)) Then
                                  If .Item(n).ToString = GetSchellHighestPlayableCard(Wnd.TrickHistory) Then FindCard = .Item(n)
                              End If
                          Else
                              If hc.GetHighestCard(LeadSuitID, TrumpID) IsNot Nothing Then
                                  If .Item(n).ToString = hc.GetHighestCard(LeadSuitID, TrumpID).ToString Then
                                      FindCard = CardsPanel.Items.Item(n)
                                  End If
                              End If
                          End If
                          If FindCard Is Nothing Then
                              Dim curCard As PlayingCard =
                                  ACP.SelectBestFirstCard(CardsPanel, hc.PlayerID, Wnd.sk.declarer, Wnd.GameStatus,
                                                      sHand, TrumpID, hc, Wnd, LeadSuitID)
                              If curCard IsNot Nothing AndAlso curCard.CardType <> TrumpID Then
                                  FindCard = curCard
                                  If FindCard.CardValue = 2 Or FindCard.CardValue = 3 Then Wnd.sk.leadSuit = TrumpID
                                  If FindCard.CardValue = 2 Or FindCard.CardValue = 3 Then LeadSuitID = TrumpID
                              End If
                          End If

                      Else

                          If FindCard Is Nothing Then
                              Dim curCard As PlayingCard =
                                  ACP.SelectBestFirstCard(CardsPanel, hc.PlayerID, Wnd.sk.declarer, Wnd.GameStatus,
                                                      sHand, TrumpID, hc, Wnd, LeadSuitID)
                              If curCard IsNot Nothing AndAlso curCard.CardType <> TrumpID Then
                                  FindCard = curCard
                                  'If FindCard IsNot Nothing Then Debug.Print("ALL 77 FindCard: " & FindCard.ToString)
                                  If FindCard.CardValue = 2 Or FindCard.CardValue = 3 Then Wnd.sk.leadSuit = TrumpID
                                  If FindCard.CardValue = 2 Or FindCard.CardValue = 3 Then LeadSuitID = TrumpID
                              End If
                          End If

                      End If
                  Next
              End If
          End With

          If FindCard IsNot Nothing Then Return FindCard

      End Function


What I have tried:

public static PlayingCard SelectFirstCardCommonCheck_ALL(this HandCards hc, MainWindow Wnd, ListBox CardsPanel, int TrumpID, int LeadSuitID)
 {

     string sHand;
     var FindCard = default(PlayingCard);
     var ACP = new AutoCardPlay(Wnd.sk.trumpCard, Wnd);

     sHand = hc.GetHandString();

     {
         var withBlock =  CardsPanel.Items;
         sHand = string.Empty;
         if (Conversions.ToBoolean(Operators.ConditionalCompareObjectGreater(withBlock.Count, 0, false)))
         {
             for (int n = 0, loopTo = Conversions.ToInteger(Operators.SubtractObject(withBlock.Count, 1)); n <= loopTo; n++)
             {
                 if (Wnd.iTricks > 5)
                 {
                     if (NonTrumpCards(hc, TrumpID) is not null)
                     {
                         if (NonTrumpCards(hc, TrumpID).Contains(Extensions_TrickMonitoring.GetEichelHighestPlayableCard(Wnd.TrickHistory)))
                         {
                             if ((withBlock.Item(n).ToString() ?? "") == (Extensions_TrickMonitoring.GetEichelHighestPlayableCard(Wnd.TrickHistory) ?? ""))
                                 FindCard = (PlayingCard)withBlock.Item(n);
                         }
                         if (NonTrumpCards(hc, TrumpID).Contains(Extensions_TrickMonitoring.GetLaubHighestPlayableCard(Wnd.TrickHistory)))
                         {
                             if ((withBlock.Item(n).ToString() ?? "") == (Extensions_TrickMonitoring.GetLaubHighestPlayableCard(Wnd.TrickHistory) ?? ""))
                                 FindCard = (PlayingCard)withBlock.Item(n);
                         }
                         if (NonTrumpCards(hc, TrumpID).Contains(Extensions_TrickMonitoring.GetSchellHighestPlayableCard(Wnd.TrickHistory)))
                         {
                             if ((withBlock.Item(n).ToString() ?? "") == (Extensions_TrickMonitoring.GetSchellHighestPlayableCard(Wnd.TrickHistory) ?? ""))
                                 FindCard = (PlayingCard)withBlock.Item(n);
                         }
                     }
                     else if (hc.GetHighestCard(LeadSuitID, TrumpID) is not null)
                     {
                         if ((withBlock.Item(n).ToString() ?? "") == (hc.GetHighestCard(LeadSuitID, TrumpID).ToString() ?? ""))
                         {
                             FindCard = (PlayingCard)CardsPanel.Items.Item(n);
                         }
                     }
                     if (FindCard is null)
                     {
                         var curCard = ACP.SelectBestFirstCard(CardsPanel, hc.PlayerID, Wnd.sk.declarer, Wnd.GameStatus, sHand, TrumpID, hc, Wnd, LeadSuitID);
                         if (curCard is not null && (int)curCard.CardType != TrumpID)
                         {
                             FindCard = curCard;
                             if ((int)FindCard.CardValue == 2 | (int)FindCard.CardValue == 3)
                                 Wnd.sk.leadSuit = TrumpID;
                             if ((int)FindCard.CardValue == 2 | (int)FindCard.CardValue == 3)
                                 LeadSuitID = TrumpID;
                         }
                     }
                 }


                 else if (FindCard is null)
                 {
                     var curCard = ACP.SelectBestFirstCard(CardsPanel, hc.PlayerID, Wnd.sk.declarer, Wnd.GameStatus, sHand, TrumpID, hc, Wnd, LeadSuitID);
                     if (curCard is not null && (int)curCard.CardType != TrumpID)
                     {
                         FindCard = curCard;
                         // If FindCard IsNot Nothing Then Debug.Print("ALL 77 FindCard: " & FindCard.ToString)
                         if ((int)FindCard.CardValue == 2 | (int)FindCard.CardValue == 3)
                             Wnd.sk.leadSuit = TrumpID;
                         if ((int)FindCard.CardValue == 2 | (int)FindCard.CardValue == 3)
                             LeadSuitID = TrumpID;
                     }

                 }
             }
         }
     }

     if (FindCard is not null)
         return FindCard;
     return default;

 }
Posted
Updated 20-Feb-23 6:22am
Comments
Richard MacCutchan 20-Feb-23 12:48pm    
Which line causes the error?

Quote:
C#
withBlock.Item(n)
Item is usually the name of the indexer property. Try:
C#
withBlock[n]
ItemCollection.Item[Int32] Property (System.Windows.Controls) | Microsoft Learn[^]
 
Share this answer
 
Comments
Jo_vb.net 20-Feb-23 13:02pm    
That works fine - thank you.
 
Share this answer
 
Comments
Jo_vb.net 20-Feb-23 12:21pm    
What I posted was done by Code Converter C# to VB and VB to C# – Telerik

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900