views:

95

answers:

4

I'm new. I'm trying to take the pigTalk() and transfer it into its own class Thanks for any help

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FunctionTest
{



    public class Program
    {
        public static void pigTalk(string sentence)
        {


            try
            {
                while (sentence != "exit")
                {

                    string firstLetter;
                    string afterFirst;
                    string pigLatinOut = "";
                    int x;
                    string vowel = "AEIOUaeiou";


                    string[] pieces = sentence.Split();

                    foreach (string piece in pieces)
                    {
                        afterFirst = piece.Substring(1);
                        firstLetter = piece.Substring(0, 1);
                        x = vowel.IndexOf(firstLetter);

                        if (x == -1)
                        {
                            pigLatinOut = (afterFirst + firstLetter + "ay ");
                        }
                        else
                        {
                            pigLatinOut = (firstLetter + afterFirst + "way ");
                        }

                        Console.Write(pigLatinOut);

                    }

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

        }

          public static void Main(string[] args)
          {
              Console.WriteLine("Enter a sentence to convert into PigLatin");
              string sentence = "";

              sentence = Console.ReadLine();

              pigTalk(sentence);  

          }

     }

}
+3  A: 

Create a new class and move the pigTalk to it,

namespace FunctionTest 
{ 
    class YourClassName
    {
        // move the pigTalk here
    }
}

in the Main method change the calling of pigTalk to this,

YourClassName.pigTalk(sentence)
A_Nablsi
Thanks Ill give it a try!
nuclearpro
You are welcome, and good luck.
A_Nablsi
+1 It's really that easy, and it really should be. It's good to learn how to do this and to get comfortable doing it, because your code develops a better structure if you apply techniques like this - refactorings - with ease. Note that if this had not been a static method, you would have to create an instance of YourClassName before you could call the method, and you would need to call it somewhat differently.
Carl Manaster
Ok, I did it but I do have one error. The name 'PigLatinClass' does not exist in the current context. The line I added was PigLatinClass.pigTalk(sentence);
nuclearpro
Post your code please, I think you missed the namespace I mean you might not wrote that class in the namespace of your application.
A_Nablsi
I updated the code check it out.
A_Nablsi
+1  A: 

Create a new class file, and then Cut (Ctrl+X) and Paste (Ctrl+V) the method from program.cs to your new class file.

Then call the method with {YourClassName}.pigTalk()

davisoa
Ok, I did it but I do have one error. The name 'PigLatinClass' does not exist in the current context. The line I added was PigLatinClass.pigTalk(sentence);
nuclearpro
+1  A: 

Is this a homework assignment? Here's what you have to do. First, create a new class file named "Pig.cs" in the same directory as your main file. Every class should have its own file, but if you put class files in a separate directory within your solution, those classes will be in a different namespace. You'll have to learn to deal with namespaces as you learn C#, but we'll put that aside for now.

Pig.cs should contain a class called Pig, defined as follows:

public class PigLatin
{
    public string TranslateFromEnglish(string message)
    {
        string result = "";

        // your code here

        return result;
    }
}

Then, in your main program, you should have something like this:

public static void Main(string[] args)
{
    PigLatin pl = new PigLatin();
    string englishSentence = "";
    string pigLatinSentence = "";

    Console.WriteLine("Enter a sentence to convert into Pig Latin.\n");

    englishSentence = Console.ReadLine();

    pigLatinSentence = pl.TranslateFromEnglish(englishSentence);

    console.WriteLine(string.Format("{0}\n", pigLatinSentence));  
}
Matthew Graybosch
+1 for "Is this a homework assignment" ;)
drachenstern
I honestly couldn't resist. It reminded me of assignments from my own introductory programming classes, only I didn't get to use C# and .NET; I was stuck with C and the standard library on a SPARCstation running Solaris. Good times.
Matthew Graybosch
no its not a homework assignment. its been a while since i did any coding and im trying to get back into it. hey its better than "hello world" hehe.
nuclearpro
Yes, it *is* better than "Hello World".
Matthew Graybosch
+1  A: 

Everyone else seems to have answered your question, and so I'm sort of speculating here but one thing that might be causing confusion is the fact that as an operational method contained within your main class, it makes sense to have the existing method return void (because everything is done within the context of "Program", so there's no extra information to pass around), but as a separate method, it would probably make more sense to return the resultant text rather writing it out to the console directly from the helper method; That way, the calling object can be responsible for knowing what to do with the results.

In a limited context, it isn't necessaraily wrong to have the method print the resluts to the console and leave it as that, but you have more flexibiility if your method simply performs the operation as advertised and leaves it up to a higher authority to determine what to do with the results. So you might consider changing the method from something like this

public static void pigTalk(string sentence)
{
 <perform operation>
 Console.WriteLine(pigTalkOut);
}

to something more like this

public static string pigTalk(string sentence)
{
 <perform operation>
 return pigTalkOut;
}

Where the issuer of the method sends the output to the console (if that's what you want to do) by doing something like this:

Console.WriteLine([HelperClassName].Pigtalk(sentence));

Also, this may be more complex than you're looking for (though it's not really hard once you understand the concept), but this looks like a good example for using an extension method, which kind of masks the need for utility classes like the one you would probably need here. To do something like that, you would just change your first incoming argument (in this case your only one) to apply the "this" qualifier...so you would end up with something like this:

public static class [HelperClassName]
{  
 public static string Pigtalk(this string sentence) 
 { 
  <perform current operation and return PigtalkOut> 
 }
}

by doing that, the method becomes available as an extended method of the string object itself, so you could do soemthing like this in your main program :

Console.WriteLine(sentence.PigTalk());

instead of having to do something like this:

Console.WriteLine([HelperClassName].Pigtalk(sentence));

As I said, though, that may be overkill for what you need, so I hope I haven't caused confusion with that little rabbit trail.

Steven
Thanks so much! Thats EXACTLY what i want to do. Ill give it a try.
nuclearpro