Re: Trouble Using System.Array.ForEach

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I have nothing against loops, in fact in many cases, I try to use them to if
it can reduce the size of my code, but in this case, I could end up removing
both a loop and an if-then, as well as some string manipulation. So in this
case, I think the ForEach would shrink my code more than a loop. I feel
loops are very important and useful, but in this case I think I could do
better, as well as the fact that I want to learn certain things, and not
everything is included in the books I have (and I have a reasonable number,
and have read them cover to cover). I appreciate any and all help you can
give.
--
Nathan Sokalski
njsokalski@xxxxxxxxxxx
http://www.nathansokalski.com/

"Stephany Young" <noone@localhost> wrote in message
news:uj9EgJg7HHA.3716@xxxxxxxxxxxxxxxxxxxxxxx
What is so evil about using a loop?


"Nathan Sokalski" <njsokalski@xxxxxxxxxxx> wrote in message
news:eQATjud7HHA.4476@xxxxxxxxxxxxxxxxxxxxxxx
I know how to do string manipulation, that is a very simple task. However,
in several of my cases I want to not only add quotes to each element, but
take the substring of it as well (for example, in some cases I want an
array of only the first letter of each day). The techniques people have
mentioned so far are great when I want the entire name, but when I need to
do something like get just the first letter, or if I want it in all
capitals, etc., I would require either a loop of some sort (which is what
I am trying to avoid) or the ForEach method. I appreciate everyone's
suggestions on how to use loops and string manipulation, but unfortunately
(well, fortunately actually) I already know those techniques. So unless
you are going to help me figure out how to use ForEach, save yourself the
effort of showing me string manipulation and help someone else that is
still learning loops and all the string manipulation techniques. But I
still thank you for your time and effort.
--
Nathan Sokalski
njsokalski@xxxxxxxxxxx
http://www.nathansokalski.com/

"ModelBuilder" <ModelBuilder@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:19073C71-3202-4285-BBEB-59A81BDCF386@xxxxxxxxxxxxxxxx
This will avoid the leading comma:


dim n as integer = CurrentInfo.DayNames.Length - 1
dim s as string = CurrentInfo.DayNames(0)

for i = 1 to n
s = string.Format("{0}, {1}", s, CurrentInfo.DayNames(i))
next


"Nathan Sokalski" wrote:

That would get me the quotes around each of the days, but I was hoping
to do
something like this:

String.Join(",",System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames())

so that I could have the DayNames separated by commas. However, the
code
that I have above would not place each day inside quotes. Do I know of
ways
to create this comma-delimited String? Of course, but using a for loop
would
require me to either include an if statement determining when I am at
the
last value or manually remove the comma from the end of the String. I
figured if I could just use the String.Join instead, I wouldn't have to
deal
with so many Strings and If statements. And, even though it may be just
a
thirst for knowledge, I would like to know how to use the ForEach
method for
future reference. Thanks.
--
Nathan Sokalski
njsokalski@xxxxxxxxxxx
http://www.nathansokalski.com/

"ModelBuilder" <ModelBuilder@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message
news:5E4EACCE-CF4D-4FA6-B2BB-51B8082841C8@xxxxxxxxxxxxxxxx
The Action type for the ForEach method requires a subroutine that
takes no
parameters. I've never tried this method, but it doesn't seem useful
for
your situation. How about starting with the following:


For Each dayofweek As String In _

System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames

Console.Out.WriteLine(AddQuotes(dayofweek))
Next


"Nathan Sokalski" wrote:

I am trying to use the System.Array.ForEach method in VB.NET. The
action
that I want to perform on each of the Array values is:

Private Function AddQuotes(ByVal value As String) As String
Return String.Format("'{0}'", value)
End Function

Basically, I just want to surround each value with single quotes.
However, I
am having trouble getting this to work using the
System.Array.ForEach
method. This may be partially because I am not very experienced with
Actions
or Delegates, so if somebody could tell me what I am doing wrong, I
would
appreciate it. Thanks. (NOTE: The Array that I want to modify the
values
of
is not one that I have permission to modify (it is
System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames()), so
if
anybody knows of a way to do this inline without declaring another
Array,
that would be nice as well)
--
Nathan Sokalski
njsokalski@xxxxxxxxxxx
http://www.nathansokalski.com/











.



Relevant Pages

  • Re: Multiplicity, Change and MV
    ... as a String, for example. ... A lecturer teaches more than one course. ... As to your comment about loops, Mr Badour's comments and your question about ... different primary keys) the representation within the file structure is much ...
    (comp.databases.theory)
  • Re: Splitting a large string variable into lines <= 70 chars
    ... I kinda agree with you that perhaps the string search methods and functions like InStrRev and LastIndexOf will not be the fastest way. ... Wait till they grumble it's slow and then throw in a faster routine and your a hero again! ... All he'd done was reduce the number of iterations his code spent in the wait loops. ... Instead, do not modify the original string at all during the loop, ...
    (microsoft.public.dotnet.languages.vb)
  • RE: VBA output to text editor
    ... just running empty loops took 2-3 ... RecA As String * 2 ... Sub BuildFile() ... Dim e As Integer, f As Integer, i As Long ...
    (microsoft.public.excel.programming)
  • Re: text compare takes very long...
    ... b1 = StrConv(String1, vbFromUnicode) ... In the earlier versions (where the loops in the main ... algorithm were equal to the product of the two string lengths) it would be ... loops in the new algorithm are much less than the product of the two string ...
    (microsoft.public.vb.general.discussion)
  • Re: Form Based Cross Tab Query
    ... Allan ... that loops through ... >the function returns a zero length string. ... > Dim strWhere as String ...
    (microsoft.public.access.queries)