Re: Trouble Using System.Array.ForEach
- From: "Nathan Sokalski" <njsokalski@xxxxxxxxxxx>
- Date: Mon, 3 Sep 2007 12:56:46 -0400
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/
.
- References:
- Trouble Using System.Array.ForEach
- From: Nathan Sokalski
- Re: Trouble Using System.Array.ForEach
- From: Nathan Sokalski
- Re: Trouble Using System.Array.ForEach
- From: ModelBuilder
- Re: Trouble Using System.Array.ForEach
- From: Nathan Sokalski
- Re: Trouble Using System.Array.ForEach
- From: Stephany Young
- Trouble Using System.Array.ForEach
- Prev by Date: Re: Trouble Using System.Array.ForEach
- Next by Date: Re: Ping Function for vb2003
- Previous by thread: Re: Trouble Using System.Array.ForEach
- Next by thread: Re: Trouble Using System.Array.ForEach
- Index(es):
Relevant Pages
|