Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For Loop Query within Method -sorry for asking guys!! 1

Status
Not open for further replies.

UKLAD1980

Programmer
Dec 6, 2008
28
GB
Hi all,

Firstly sorry for asking you genius programmers with help on something which is probably trivial to you.

I have just started to learn c# and was doing really well until I got to a section within the book which I simply can't get my head around.

The code...

class Talker {
public static int BlahBlahBlah (string thingToSay), int numberOfTimes) {
string finalString = "";
for (int count = 1; count <= numberOfTimes; count++) {
finalString = finalString + thingToSay + "\n";
}
MessageBox.Show(finalString);
return finalString.Length;

I know how this connects to the external front end but really need some expert help on what is going on in the method. I know it is a lot to ask but I just need a idiots guide, step by step, so I can follow whats going on.

Any help guy would really be appreciated.

Cheers
 
If you are not sure, what this code should do, then try it self in your Visual Studio or other IDE you use. I don't understand why you don't try it first self and post instead the code here.
Without trying the code self you will learn nothing!

If you look at the method it has two entry arguments: thingToSay and numberOfTimes.
The method result is a integer number.
However you have typo here:
Code:
public static int BlahBlahBlah (string thingToSay[COLOR=red])[/color], int numberOfTimes)

If you call your method like this:
Code:
int result = BlahBlahBlah ('XY', 3);
it first composes the finalString, which consists of 3 thingToSay-strings concatenated together. This does the for-loop.
That is, after finishing the for-loop finalString = 'XYXYXY'.
Then the method shows the finaString and returns the length of it.
The result in this case would be 6.
 
Mikrom

Thank you for your reply. Very much appreciated. However, I do not feel it is correct for you to assume I am lazy and haven't tried and tested this code to understand it.

The code is from a book I am learning from and runs perfectly but it still would not mean I understand it just because it does what it is supposed to!!

If I wasn't desperate then I would not post in here. I see you have asked many questions in this forum because obviously you did not understand.

The above is not a typo as it should be (string thingtosay, int numberoftimes);

I'm sorry if I sound rude or I wasted your time but if you had nos pare time then you shouldn't have replied.

I hope some of the other guys can help me.

cheer
 
Hi UKLAD1980,

Sorry, if my previous answer seems to be impolite.
I don't thing, that you are lazy and I don't wrote something like this, did I? If I wrote "Without trying the code self you will learn nothing!" I thought nothing bad about you, I thout only that you are a beginner and this was only an advice to help you :)

The fragment of code you posted was
Code:
public static int BlahBlahBlah (string thingToSay[COLOR=red])[/color], int numberOfTimes)
...
and it should be
Code:
public static int BlahBlahBlah (string thingToSay, int numberOfTimes)
You have one more closing parenthesis after thingToSay.

Look, it's sunday and I am at home and don't have C# and VS installed at my home computer, but I have Java and Eclipse IDE.
As known, C# si very close to Java so I modified your example a little bit to show you the result.
Instead of MessageBox.Show() I use System.out.println()
Here is the code
Code:
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] Talker {
  [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] BlahBlahBlah (String thingToSay, [COLOR=#2e8b57][b]int[/b][/color] numberOfTimes)  {
    String finalString = [COLOR=#ff00ff]""[/color];
    [COLOR=#804040][b]for[/b][/color] ([COLOR=#2e8b57][b]int[/b][/color] count = [COLOR=#ff00ff]1[/color]; count <= numberOfTimes; count++)   {
      finalString = finalString + thingToSay + [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
    }
    System.out.println(finalString);
    [COLOR=#804040][b]return[/b][/color] finalString.length();
    }        

  [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] main(String args[]){
  [COLOR=#2e8b57][b]int[/b][/color] len = BlahBlahBlah([COLOR=#ff00ff]"XY"[/color], [COLOR=#ff00ff]3[/color]);
    System.out.println([COLOR=#ff00ff]"The result is: "[/color]+len);
  }
}

The output is
Code:
XY
XY
XY
The result is: 9
Yes the result is 9, because the finalString consists of 3 'XY' and 3 '\n' - i.e summa summarum 9 characters.

Most IDEs have build-in debugger. Learn how to use it.
So if you don't understand what a piece of code should do, IMHO one of the best ways would be to step thru the code and watch how the variables change.

I wish you happy coding.
 
If you want see what happens in the for-loop you can insert some printouts for debugging purpose like here
Code:
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] Talker {
  [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] BlahBlahBlah (String thingToSay, [COLOR=#2e8b57][b]int[/b][/color] numberOfTimes)  {
    String finalString = [COLOR=#ff00ff]""[/color];
    [COLOR=#0000ff]// for debugging[/color]
    System.out.println([COLOR=#ff00ff]"before for-loop: "[/color]+finalString);
    [COLOR=#804040][b]for[/b][/color] ([COLOR=#2e8b57][b]int[/b][/color] count = [COLOR=#ff00ff]1[/color]; count <= numberOfTimes; count++)   {
      finalString = finalString + thingToSay + [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
      [COLOR=#0000ff]// for debugging[/color]
      System.out.println([COLOR=#ff00ff]"count="[/color]+count+[COLOR=#ff00ff]", "[/color]+finalString);
    }
    [COLOR=#0000ff]// for debugging[/color]
    System.out.println([COLOR=#ff00ff]"after for-loop: "[/color]+finalString);
    System.out.println(finalString);
    [COLOR=#804040][b]return[/b][/color] finalString.length();
    }        

  [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] main(String args[]){
  [COLOR=#2e8b57][b]int[/b][/color] len = BlahBlahBlah([COLOR=#ff00ff]"XY"[/color], [COLOR=#ff00ff]3[/color]);
    System.out.println([COLOR=#ff00ff]"The result is: "[/color]+len);
  }
}
Output
Code:
before for-loop: 
count=1, XY

count=2, XY
XY

count=3, XY
XY
XY

after for-loop: XY
XY
XY

XY
XY
XY

The result is: 9
 
Mikrom,

Firstly let me say sorry for being a 'bit' rude. Your help is most welcome sir.

As a new programmer any help is appreciated.

It is late her in the U.k but I will get back to you asap.

Again thank you and sorry. :)
 
Here is the C# example
Code:
[COLOR=#804040][b]using[/b][/color] System;

[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] Talker {
  [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]int[/b][/color] BlahBlahBlah ([COLOR=#2e8b57][b]string[/b][/color] thingToSay, [COLOR=#2e8b57][b]int[/b][/color] numberOfTimes)  {
    [COLOR=#2e8b57][b]string[/b][/color] finalString = [COLOR=#ff00ff]""[/color];
    [COLOR=#0000ff]// for debugging[/color]
    System.Console.Write([COLOR=#ff00ff]"before for-loop: "[/color]+finalString);
    [COLOR=#804040][b]for[/b][/color] ([COLOR=#2e8b57][b]int[/b][/color] count = [COLOR=#ff00ff]1[/color]; count <= numberOfTimes; count++)   {
      finalString = finalString + thingToSay + [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
      [COLOR=#0000ff]// for debugging[/color]
      System.Console.Write([COLOR=#ff00ff]"count="[/color] + count + [COLOR=#ff00ff]", "[/color] + finalString);
    }
    [COLOR=#0000ff]// for debugging[/color]
    System.Console.Write([COLOR=#ff00ff]"after for-loop: "[/color]+finalString);
    System.Console.Write(finalString);
    [COLOR=#804040][b]return[/b][/color] finalString.Length;
  }        

  [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] Main(){
  [COLOR=#2e8b57][b]int[/b][/color] len = BlahBlahBlah([COLOR=#ff00ff]"XY"[/color], [COLOR=#ff00ff]3[/color]);
  System.Console.Write([COLOR=#ff00ff]"The result is: "[/color] + len);
  }
}

Output
Code:
before for-loop: count=1, XY
count=2, XY
XY
count=3, XY
XY
XY
after for-loop: XY
XY
XY
XY
XY
XY
The result is: 9
 
Hi Mikrom,

Many thanks for your help again big guy. Have you got anyway I can contact you? Maybe msn?

Cheers

D
 
Hi UKLAD1980,

This forum is here for helping you and helping me.
While helping to solve the small problems of others I'm learning new interesting things and it's a little exercise for my bad english.
I'm not a C# specialist and you thanked enough, when you gave me a star.
:)
But if you want, you can contact me at the email address specified at my old web page of my former TKD club.
The photo is about 10 years old, but the email address is still valid.
:)
But because I was 10 years ago so stupid and posted my email address in clear text at the web page, I get tons of spam daily, which will be outfiltered by a spam filter.
So I hope that your email will not be filtered out as a spam.
 
Hi Mikrom,

Remember me bro? I forgot to mention your english is good my friend. It is most certainly better than my programming:)

Listen up I have a little query for you if you have time. No problem if you haven't, maybe someone else can help??

I am currently learning from a book about database programming in c#. I have loaded the data into my dataset etc.


just for example data set is.....


PersonName Age

John 34

James 23

Simon 45

Peter 15


Using the above example dataset how does the following code work step-by-step in each stage.

foreach (DataRow dRow in dt.Rows)

{

foreach (DataColumn datacol in dt.Columns)

Console.WriteLine(dRow[datacol]);

Console.WriteLine("================");

}




How does the loop work in each stage?? How is a for loop different to a foreach loop?
Any help would be amazing. Sorry If this question is very simple. I guess we all have to start from somewhere.

As I said I would appreciate as much help as possible.

Cheers :)


p.s I have also emailed you on your taekwon do email you gave me.



 
I would also recommend that you attempt to make your questions as specific as possible. Learn what you can, ask about the details that you are having trouble with. Questions such as "can you give me any help at all as to how this code works" make it hard for us to narrow things down to address your real needs. Rather than asking how the above code works step by step, ask about specific lines you don't understand.

HTH

Bob
 
just to get you thinking, you can refactor this block. each code block below is a progression of refactorings on how to simplify the code. Written from memory, so synstax and method names may cause errors :)
Code:
public static int BlahBlahBlah (string thingToSay), int numberOfTimes)  
{
   string finalString = "";
   for (int count = 1; count <= numberOfTimes; count++)   
   {
      finalString = finalString + thingToSay + "\n";
   }
   MessageBox.Show(finalString);
   return finalString.Length; 
}
Code:
public static int BlahBlahBlah (string thingToSay), int numberOfTimes)  
{
   string finalString = "";
   for (int count = 1; count <= numberOfTimes; count++)   
   {
      finalString += thingToSay + "\n";
   }
   MessageBox.Show(finalString);
   return finalString.Length; 
}
Code:
public static int BlahBlahBlah (string thingToSay), int numberOfTimes)  
{
   StringBuilder finalString = new StringBuilder();
   for (int count = 1; count <= numberOfTimes; count++)   
   {
      finalString.AppendList(thingToSay);
   }
   MessageBox.Show(finalString.ToString());
   return finalString.Length; 
}
Code:
public static int BlahBlahBlah (string thingToSay), int numberOfTimes)  
{
   string newThingToSay = thingToSay.Concat("\n");
   string finalString = string.Repeat(newThingToSay , numberOfTimes);
   MessageBox.Show(finalString);
   return finalString.Length; 
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top