Interessant. Werde das mal pr�fen. Von Generics habe ich noch nichts (konkretes) geh�rt. Wann gibt�s das? Kannst Du das kurz erl�utern oder sprengt das den selbstzuformulierenden Rahmen?
Elmar -----Urspr�ngliche Nachricht----- Von: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Im Auftrag von Christoph Wille Gesendet: Dienstag, 16. September 2003 15:35 An: [EMAIL PROTECTED] Betreff: [Csharp.net] Re: wo wir grad bei bl�den Fragen sind ;- ) ... hier ist eine zu ArrayList casting eine InvalidCastException handelst Du Dir m�glicherweise dadurch ein, da� er bei Console.ReadLine einen castbaren Datentyp gefunden hat, und er nicht als String im ArrayList drinnen ist. Zum Gl�ck h�ren sich diese Trickersereien mit Generics auf. Chris At 11:25 AM 9/16/2003, you wrote: >Gut kopiert ist halb gewonnen. > >So funktionierts: >// ArrayList convert to Array >string[] checkArr = new string[checkArrList.Count]; > >//checkArr = (string[]) checkArrList.ToArray(checkArr.GetType()); >checkArrList.CopyTo(checkArr,0); > >Mich h�tte trotzdem interessiert, warum er sich beim casten so borstig >gibt. Schlie�lich gibt es die ToArray(type) Methode ja nicht umsonst. > >Vielen Dank, Daniel! > >-----Urspr�ngliche Nachricht----- >Von: [EMAIL PROTECTED] >[mailto:[EMAIL PROTECTED] Im Auftrag von Daniel Fisher >Gesendet: Dienstag, 16. September 2003 11:18 >An: [EMAIL PROTECTED] >Betreff: [Csharp.net] AW: [Csharp.net] wo wir grad bei bl�den Fragen >sind ;-) ... hier ist eine zu ArrayList casting > >using System; >using System.Collections; >public class SamplesArrayList { > > public static void Main() { > > // Creates and initializes the source ArrayList. > ArrayList mySourceList = new ArrayList(); > mySourceList.Add( "three" ); > mySourceList.Add( "napping" ); > mySourceList.Add( "cats" ); > mySourceList.Add( "in" ); > mySourceList.Add( "the" ); > mySourceList.Add( "barn" ); > > // Creates and initializes the one-dimensional target Array. > Array myTargetArray=Array.CreateInstance( typeof(String), 15 ); > myTargetArray.SetValue( "The", 0 ); > myTargetArray.SetValue( "quick", 1 ); > myTargetArray.SetValue( "brown", 2 ); > myTargetArray.SetValue( "fox", 3 ); > myTargetArray.SetValue( "jumped", 4 ); > myTargetArray.SetValue( "over", 5 ); > myTargetArray.SetValue( "the", 6 ); > myTargetArray.SetValue( "lazy", 7 ); > myTargetArray.SetValue( "dog", 8 ); > > // Displays the values of the target Array. > Console.WriteLine( "The target Array contains the following >(before and after copying):" ); > PrintValues( myTargetArray, ' ' ); > > // Copies the second element from the source ArrayList to the >target ArrayList, starting at index 7. > mySourceList.CopyTo( 1, myTargetArray, 7, 1 ); > > // Displays the values of the target Array. > PrintValues( myTargetArray, ' ' ); > > // Copies the entire source ArrayList to the target ArrayList, >starting at index 6. > mySourceList.CopyTo( myTargetArray, 6 ); > > // Displays the values of the target Array. > PrintValues( myTargetArray, ' ' ); > > // Copies the entire source ArrayList to the target ArrayList, >starting at index 0. > mySourceList.CopyTo( myTargetArray ); > > // Displays the values of the target Array. > PrintValues( myTargetArray, ' ' ); > } > > public static void PrintValues( Array myArr, char mySeparator ) { > System.Collections.IEnumerator myEnumerator = >myArr.GetEnumerator(); > int i = 0; > int cols = myArr.GetLength( myArr.Rank - 1 ); > while ( myEnumerator.MoveNext() ) { > if ( i < cols ) { > i++; > } else { > Console.WriteLine(); > i = 1; > } > Console.Write( "{0}{1}", mySeparator, myEnumerator.Current ); > } > Console.WriteLine(); > } >} >/* >This code produces the following output. > >The target Array contains the following (before and after copying): > The quick brown fox jumped over the lazy dog > The quick brown fox jumped over the napping dog > The quick brown fox jumped over three napping cats in the barn > three napping cats in the barn three napping cats in the barn >*/ > >-----Urspr�ngliche Nachricht----- >Von: [EMAIL PROTECTED] >[mailto:[EMAIL PROTECTED] Im Auftrag von elni >Gesendet: Dienstag, 16. September 2003 10:04 >An: [EMAIL PROTECTED] >Betreff: [Csharp.net] wo wir grad bei bl�den Fragen sind ;-) ... hier >ist eine zu ArrayList casting > > > >Hallo Zusammen! >Ich bin neu in der Liste und ebenfalls neu in C#. >Ganz so doof scheint mein Problem aber nicht zu sein, denn ich habe das >Thema auch schon bei csharphelp.com diskutiert und so eine richtige >L�sung kennt dort auch keiner. > >Im Prinzip geht es nur darum eine ArrayList in einen string[] Array >(doppelt gemoppelt) zu dr�cken. Kompilieren geht, nur zur Laufzeit wirft >er einen seltsamen Fehler. Das Umwandeln scheint um einiges >umst�dnlicher als in Java. Warum es jetzt trotzdem noch hakt ist mir ein >bischen schleierhaft... > >[...] >ArrayList checkArrList = new ArrayList(); >int i = 0; >while ((input=sr.ReadLine())!=null) >{ > checkArrList.Add(input + " / " + (i+1)); >this.tb_debug.Text += checkArrList[i]; > i++; >} >sr.Close(); > >// ArrayList convert to Array (hierum dreht es sich) >string[] checkArr = new string[checkArrList.Count]; > >checkArr = (string[]) checkArrList.ToArray(checkArr.GetType()); > >// Array output to CheckedListbox >this.lbx_ergebnis.Items.AddRange(checkArr); >[...] > >Wirft zu Laufzeit folgenden Fehler: >System.InvalidCastException: >At least one element in the source array could not be cast down to the >destination array type. > >at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array >destinationArray, Int32 destinationIndex, Int32 length) at >System.Collections.ArrayList.ToArray(Type type) > >ciao, >Elmar _______________________________________________ Csharp.net mailing list [EMAIL PROTECTED] http://www.glengamoi.com/mailman/listinfo/csharp.net _______________________________________________ Csharp.net mailing list [EMAIL PROTECTED] http://www.glengamoi.com/mailman/listinfo/csharp.net
