0

i'm not sure i'm asking this correctly, so if i'm making a mistake please please let me know.

  1. I've tried googling and searching stack for a few hours and have not found a result I could understand well enough to impliment (but i did try, honest).

  2. I am trying to make a helper method to insert in to several different places in my code.

This is the method

//Tried several methods (Do, Do While, For, et al) to make an insert code
    public Boolean insertFormat()//Method stub
    {
        Console.Clear(); //Clears out gunk, i hate gunk. I want to know I am looking at
        Console.WriteLine(title);//prints program title   
    }

I thought I could call it like so:

 while (true)
        {
            insertFormat;// This is where i'm trying to repeat the lines - i do this several times so i want to include them somehow (conditions vary) 

            Console.Clear(); //Clears out gunk, i hate gunk. I want to know I am looking at
            Console.WriteLine(title);//prints program title

            Console.WriteLine("For Breakfast may we suggest:" + bSelections[selectRandomArrayPosition(0, 4)] + "\n");
            Console.WriteLine("Please enter \"N\" for a new selection, or any other key to exit \n");
            suggestAgain = Console.ReadLine().ToLower();

            if (suggestAgain != "n") break;
        }

I've tried for loop, do, do while, etc. Tried to do as a variable but didn't do any correctly so as it worked. Generally I ended up with an error saying

'not all code paths return a value'.

I do the clear and reprint the title about 6 times and don't want to have redundant code in my program as I'm told that is not good practice.

2
  • 'not all code paths return a value' is because you declare your function as Boolean but don't return anything, make it void. However I have no idea what you are actually trying to accomplish Oct 27, 2013 at 15:46
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Oct 27, 2013 at 15:46

2 Answers 2

0

Remove the return type (Boolean) from insertFormat method, since you are not returning anything and compiler gives an error. Change it to void

public void insertFormat()//Method stub
{
    Console.Clear(); //Clears out gunk, i hate gunk. I want to know I am looking at
    Console.WriteLine(title);//prints program title   
}

// Calling code

        while (true)
        {
            insertFormat();// This is where i'm trying to repeat the lines - i do this several times so i want to include them somehow (conditions vary) 

            // Skip them, insertFormat will execute them
            //Console.Clear(); //Clears out gunk, i hate gunk. I want to know I am looking at
            //Console.WriteLine(title);//prints program title

            Console.WriteLine("For Breakfast may we suggest:" + bSelections[selectRandomArrayPosition(0, 4)] + "\n");
            Console.WriteLine("Please enter \"N\" for a new selection, or any other key to exit \n");
            suggestAgain = Console.ReadLine().ToLower();

            if (suggestAgain != "n") break;
        }
5
  • Hello Kashif Khan, thanks for taking a look at what silliness i'm attempting. I don't understand what you are instructing. I thought that it was 'void' since it reads 'public VOID.....' Could you explain further?
    – Chezshire
    Oct 27, 2013 at 15:49
  • the return type of your insertFormat method is Boolean, so the compiler is expecting you to return a Boolean value. Since, you are just clearing up console and writing title in this method then there is no need to return anything, just use void as return type...Also to use this method, call it in this way insertFormat(); Oct 27, 2013 at 15:51
  • Hello again Kashif Khan, and thank you for replying. I have something that works, but i dont' know if i did what you instructed. I typed the following in to my Virtual studio C# sharp and it worked niced: 'public Boolean insertFormat()//Method stub { Console.Clear(); //Clears out gunk, i hate gunk. I want to know I am looking at Console.WriteLine(title);//prints program title return true; }' Is this what you meant for me to do? Thank you for all the help!
    – Chezshire
    Oct 27, 2013 at 15:58
  • I have updated my answer with working code. Compare it with yours Oct 27, 2013 at 16:01
  • Thank you very much. I learned a bit from this exercise and I really truly apprecaite your help Kashif Khan. I hope you have as wonderful a day as am I. Again, thank you very sincerely!
    – Chezshire
    Oct 27, 2013 at 16:28
0

You had several errors in your code. Here is working version.

public void insertFormat()
{
    Console.Clear();
    Console.WriteLine(title);
}

while (suggestAgain != "n")
{
    insertFormat();

    Console.WriteLine("For Breakfast may we suggest:" + bSelections[selectRandomArrayPosition(0, 4)] + "\n");
    Console.WriteLine("Please enter \"N\" for a new selection, or any other key to exit \n");

    suggestAgain = Console.ReadLine().ToLower();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.