0

I'm trying to Skip a line in a Visual C++ managed String^ or String^ array but I haven't found any easy way to do so. In-fact, I've spent two days on something that would take less than 30 seconds in C#. There is a method .Skip() within C# Enumerable

https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.skip?view=netframework-4.7.2

and I'd like something similar for VC++ if possible.

Here's what I've tried:

    auto a = gcnew cli::array<String ^>{ "Alpha", "Bravo", "Charlie", "Delta" };

    auto xs = gcnew System::Collections::Generic::List<String^>(a);

    System::Collections::Generic::IEnumerator<String^>^ e = xs->GetEnumerator();

    e->MoveNext();

^^ that throws exception class System::EventArgs has no member "MoveNext"

EDIT: I know what's causing the exception System::EventArgs has no member "MoveNext" .. using ' e ' in Visual Studio causes the compiler to think I'm referring to the e for EventArgs as in e) click_method .. I switched to another name and it populates as this: System.Collections.Generic.List`1+Enumerator[System.String]

I also tried referencing System::Linq and then

    System::Collections::Generic::List<String ^>^ _list = gcnew System::Collections::Generic::List<String ^>(System::IO::File::ReadAllLines(filename));

    System::Collections::Generic::List<String ^>^ _list2 = _list->Skip(1);

which seems like it would work in C# but get the following exception

class System::Collections::Generic::List has no member skip

so the .NET library doesn't work in CLI / C++

Another thing I attempted was:

    System::Collections::Generic::List<String ^>^ _list = gcnew System::Collections::Generic::List<String ^>(System::IO::File::ReadAllLines(filename));

    System::Collections::Generic::List<String ^>^ _list2 = _list->RemoveAt(0);

but got exception: a value of type "void" cannot be used to initialize an entity of type System::Collections::Generic::List^

I'm trying to do it without using marshal_string if possible, but I'm open for any suggestions, as I've been pulling my hair out on this, not sure what else to try =[

hexagod
  • 449
  • 3
  • 15
  • Extension methods are just static methods, so my guess is you could call it manually: `Enumerable::ToList(Enumerable::Skip(_list, 1));` but not suggesting this is the best way. (Note that the return value from `Skip` is not a `List<>` - watch your types and that will help you a lot.) See [this answer](https://stackoverflow.com/a/5645267/2557128). – NetMage Dec 18 '18 at 22:57
  • Note this seems like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me - cloning a `List^` to drop the first element is a pretty inefficient way to process a list. – NetMage Dec 18 '18 at 23:30
  • @NetMage /* nice... that definitely helped but I'm still not skipping a line*/ – hexagod Dec 19 '18 at 00:06
  • It is unclear to me what "skipping a line" means in the context of a `String^` or a `List^`? – NetMage Dec 19 '18 at 00:30
  • @NetMage /* yeah that's because I originally wanted to achieve this with a single String^ but I later switched to a String^ array due to not being able to find enough resources. I figured out how to do it as an array but not as a single String^ */ – hexagod Dec 19 '18 at 20:55
  • Resources? I guess that should be a separate question... I still think there isn't enough information right now. – NetMage Dec 19 '18 at 20:59
  • yeah that's understandable. This thread is dead. I'm done with CLI/C++, huge waste of time – hexagod Dec 20 '18 at 00:00
  • when I say resources I mean free tutorials and prior questions on CLI .. not system resources – hexagod Dec 20 '18 at 00:02

1 Answers1

0

EDIT: this reads as an array not a String^ but I couldn't figure out how to do it with a String^ on it's own.

definitely not the quickest way but this skips x number of lines.

I am sick of CLI/C++/VC++ and can't wait to stop using it (I should have never used it in the first place and kick myself daily for not making this in C#).

    //read the file to a CLI array (each line is a member)
    auto a = System::IO::File::ReadAllLines("test66.txt");

    //create a List from the array
    auto xs = gcnew System::Collections::Generic::List<String^>(a);

    //create a collection from the List
    System::Collections::Generic::IEnumerator<String^>^ test66 = xs->GetEnumerator();

    //the number of lines we want to skip 
    int x = 0;

    //for loop stopping @ x
    for (int nxt; nxt <= x; nxt++)
    {
        test66->MoveNext();
    }

    //present selected array item to user or you could feed this into a String^
    textBox11->Text = test66->Current;

feel free to give a better answer. I couldn't find much information since CLI/C++ is absolutely awful compared to C# and just about every other modern language. (IMO)

hexagod
  • 449
  • 3
  • 15