Thread: ToArray and Type Casting

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    ToArray and Type Casting

    I have an ArrayList and a string which I want to find chars of ArrayList in it. My ArrayList is named punctuations.
    Code:
          tatic System::Collections::ArrayList ^punctuations =  gcnew System::Collections::ArrayList;
          punctuations->Add('/');
          punctuations->Add('\n');
          punctuations->Add(' ');
          punctuations->Add('\t');
          punctuations->Add('\"');
          punctuations->Add('\'');
          
          array<wchar_t>^myArr =safe_cast<array<wchar_t>^>(punctuations->ToArray(wchar_t::typeid));
    
          int firstToken = text->IndexOfAny(myArr, startpos);
    It fails to cast and says at least one source element can't be cast to dest array type.
    But don't know why...
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    You have objects of type "char" and you try to get an array of type wchar_t. Maybe it's easier to insert wchar_t characters from the start. You could also use a generic list so you are typesafe. Or you could use an array without using an arraylist in between.

    Why do you use C++/CLI if I may ask? If you want .NET, C# is better suited, interfacing between legacy C++ code and a C# application is the only valid excuse for using C++/CLI. Or Masochism. C++/CLI combines the readability of C++ and the speed of .NET ...
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Thread in MSDN...
    Quote Originally Posted by nvoigt
    Why do you use C++/CLI if I may ask? If you want .NET, C# is better suited, interfacing between legacy C++ code and a C# application is the only valid excuse for using C++/CLI. Or Masochism. C++/CLI combines the readability of C++ and the speed of .NET ...
    lol, Masochism it seems to be. The reason I am using it is that once I decided to fully learn C++/CLI. As a result I started a project to practice it. Now I am back to that project.

    Thanks
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Quote Originally Posted by siavoshkc View Post
    The reason I am using it is that once I decided to fully learn C++/CLI.
    That sounds like hitting yourself with a hammer just out of curiosity and to learn how it feels. If you don't know the .NET Framework yet, try to learn C#, it's way easier and C++/CLI is just 3 additional keywords once you know .NET and C++.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I practiced C#, VB.net and C++/CLI and I have an acceptable knowledge about .NET now (at least from my own view point). But I decided to focus on C++.

    I am also thinking about F# right now. Do you know it?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed