CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2010
    Posts
    26

    I want to Pass the number of elements i want from string array stack

    I don't have any special way i'm passing the number of elements I want. I don't really care what each element's string contain. Here is the issue I already created and have the string array with all of the elements created inside it. i just don't know how to create the logic or generate the proper code to either pass the number of element i want out of the number of elements that already exist in this string array. Here is my current code which is incomplete.

    dirString is my String Array varible.
    I want to pass it my random value to pick the
    # of elements that i want. Examply dirString has 1600 elements in it. In my random value variable
    I create a random value and i want to pass this value to return this # of elements from the stack of 1600 elements in the string array. I don't care whats in those elements i only care that it returns that number of element from the stack of 1600.

    foreach (var FileString in dirString)
    {
    //Array.ForEach(dirString,dirString.SelectMany(dirString,

    int MyRndVal = myRand.Next(min, max);

    = dirString[count];
    for (int i = 0; i <= MyRndVal; i++)
    {
    string name = Path.GetFileName(FileString);
    string dest = Path.Combine(folderPath, name);
    File.Move(FileString, dest);

    wr.myfileNam = name;

    wr.count = count++;

    wr.MyTextPad(dirString, count);
    }
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: I want to Pass the number of elements i want from string array stack

    Based on your description it seems pretty straightforward.

    Code:
    // simulate a 'random' value
    var randomValue = 3; 
    
    // create the input list
    var stringList = new List<String> { {"This"}, {"is"}, {"a"}, {"string"}, {"test." }};
    
    // Print out the input list
    Console.WriteLine("Original List");
    
    foreach (var s in stringList)
    {
        Console.WriteLine(String.Format("\t{0}", s));
    }
    
    // Extract the list elements from the original list based on
    // the 'randomValue'. Start at the beginning of the list.
    var subList = stringList.GetRange(0, randomValue);
    
    // Print out the extracted values.
    Console.WriteLine("\nSublist");
    
    foreach (var s in subList)
    {
        Console.WriteLine(String.Format("\t{0}", s));
    }
    Outputs:
    Code:
    Original List
            This
            is
            a
            string
            test.
    
    Sublist
            This
            is
            a

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured