Click here to Skip to main content
15,896,269 members
Home / Discussions / C#
   

C#

 
AnswerRe: Adding to project Pin
AspDotNetDev3-May-10 16:01
protectorAspDotNetDev3-May-10 16:01 
GeneralRe: Adding to project Pin
Gene_Sheppard3-May-10 16:14
Gene_Sheppard3-May-10 16:14 
QuestionPlaceholders with SQL statement Pin
Terence van Schalkwyk3-May-10 9:49
Terence van Schalkwyk3-May-10 9:49 
AnswerRe: Placeholders with SQL statement Pin
SomeGuyThatIsMe3-May-10 9:55
SomeGuyThatIsMe3-May-10 9:55 
AnswerRe: Placeholders with SQL statement Pin
Luc Pattyn3-May-10 10:03
sitebuilderLuc Pattyn3-May-10 10:03 
GeneralRe: Placeholders with SQL statement Pin
Terence van Schalkwyk3-May-10 10:09
Terence van Schalkwyk3-May-10 10:09 
GeneralRe: Placeholders with SQL statement Pin
Pete O'Hanlon3-May-10 11:21
mvePete O'Hanlon3-May-10 11:21 
QuestionClass Instantiation when to use/or not Pin
mprice2143-May-10 9:00
mprice2143-May-10 9:00 
Hi all,

I've been trying to become more familiar with classes and instantiating them and the different times one would need to do so. I came across the following tutorial (http://www.csharp-station.com/Tutorials/Lesson05.aspx[^]
and it has the code below. Albeit, this code doesn't do anything useful, but again, just raises a question. The question I have is what would drive instantiating the class versus declaring getChoice() as static? As I'm a ME, please use plain english. Big Grin | :-D
using System;

class Address
{
    public string name;
    public string address;
}

class MethodParams
{
    public static void Main()
    {
        string myChoice;

        MethodParams mp = new MethodParams();

        do
       {
            // show menu and get input from user
            myChoice = mp.getChoice();

            // Make a decision based on the user's choice
            mp.makeDecision(myChoice);

            // Pause to allow the user to see the results
            Console.Write("press Enter key to continue...");
            Console.ReadLine();
            Console.WriteLine();
        } while (myChoice != "Q" && myChoice != "q"); // Keep going until the user wants to quit
    }

    // show menu and get user's choice
    string getChoice()
    {
        string myChoice;

        // Print A Menu
        Console.WriteLine("My Address Book\n");

        Console.WriteLine("A - Add New Address");
        Console.WriteLine("D - Delete Address");
        Console.WriteLine("M - Modify Address");
        Console.WriteLine("V - View Addresses");
        Console.WriteLine("Q - Quit\n");

        Console.WriteLine("Choice (A,D,M,V,or Q): ");

        // Retrieve the user's choice
        myChoice = Console.ReadLine();

        return myChoice; 
    }

    // make decision
    void makeDecision(string myChoice)
    {
        Address addr = new Address();

        switch(myChoice)
        {
            case "A":
            case "a":
                addr.name = "Joe";
                addr.address = "C# Station";
                this.addAddress(ref addr);
                break;
            case "D":
            case "d":
                addr.name = "Robert";
                this.deleteAddress(addr.name);
                break;
            case "M":
            case "m":
                addr.name = "Matt";
                this.modifyAddress(out addr);
                Console.WriteLine("Name is now {0}.", addr.name);
                break;
            case "V":
            case "v":
                this.viewAddresses("Cheryl", "Joe", "Matt", "Robert");
                break;
            case "Q":
            case "q":
                Console.WriteLine("Bye.");
                break;
            default:
                Console.WriteLine("{0} is not a valid choice", myChoice);
                break;
        }
    }

    // insert an address
    void addAddress(ref Address addr)
    {
        Console.WriteLine("Name: {0}, Address: {1} added.", addr.name, addr.address); 
    }

    // remove an address
    void deleteAddress(string name)
    {
        Console.WriteLine("You wish to delete {0}'s address.", name); 
    }

    // change an address
    void modifyAddress(out Address addr)
    {
        //Console.WriteLine("Name: {0}.", addr.name); // causes error!
        addr = new Address();
        addr.name = "Joe";
        addr.address = "C# Station";
    }

    // show addresses
    void viewAddresses(params string[] names)
    {
        foreach (string name in names)
        {
            Console.WriteLine("Name: {0}", name);
        }
    }
}

AnswerRe: Class Instantiation when to use/or not Pin
Richard MacCutchan3-May-10 9:45
mveRichard MacCutchan3-May-10 9:45 
GeneralRe: Class Instantiation when to use/or not Pin
mprice2143-May-10 10:41
mprice2143-May-10 10:41 
GeneralRe: Class Instantiation when to use/or not PinPopular
Luc Pattyn3-May-10 12:44
sitebuilderLuc Pattyn3-May-10 12:44 
GeneralRe: Class Instantiation when to use/or not Pin
mprice2144-May-10 3:19
mprice2144-May-10 3:19 
GeneralRe: Class Instantiation when to use/or not Pin
Luc Pattyn4-May-10 3:29
sitebuilderLuc Pattyn4-May-10 3:29 
GeneralRe: Class Instantiation when to use/or not Pin
Richard MacCutchan3-May-10 22:29
mveRichard MacCutchan3-May-10 22:29 
Questiongraphic examples in C # Pin
jack_cia3-May-10 7:43
jack_cia3-May-10 7:43 
AnswerRe: graphic examples in C # Pin
#realJSOP3-May-10 7:54
professional#realJSOP3-May-10 7:54 
AnswerRe: graphic examples in C # Pin
Luc Pattyn3-May-10 8:42
sitebuilderLuc Pattyn3-May-10 8:42 
GeneralRe: graphic examples in C # Pin
dj20783-May-10 17:22
dj20783-May-10 17:22 
GeneralRe: graphic examples in C # Pin
Luc Pattyn3-May-10 17:33
sitebuilderLuc Pattyn3-May-10 17:33 
AnswerRe: graphic examples in C # Pin
yu-jian4-May-10 3:37
yu-jian4-May-10 3:37 
QuestionGet XmlElement of a property in a XmlSerializable-Class Pin
flitzpiepe23-May-10 5:46
flitzpiepe23-May-10 5:46 
AnswerRe: Get XmlElement of a property in a XmlSerializable-Class Pin
T M Gray3-May-10 11:59
T M Gray3-May-10 11:59 
GeneralRe: Get XmlElement of a property in a XmlSerializable-Class Pin
flitzpiepe23-May-10 20:47
flitzpiepe23-May-10 20:47 
QuestionLeft Top Ruler C# Pin
jojoba20113-May-10 5:27
jojoba20113-May-10 5:27 
AnswerRe: Left Top Ruler C# Pin
Pete O'Hanlon3-May-10 6:01
mvePete O'Hanlon3-May-10 6:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.