Click here to Skip to main content
15,886,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to add functionality to the partial class magic. I used the same "class" program that I built with one of your guys(codeproject expert) help and created a partial class magic. I did it using one of the free C# book reference and not sure if I did it correctly. It didn't give me any error message though. If anyone can look look at it and point me if I did anything wrong or missing, and help me correct the code, I will really appreciate that.
C#
namespace PartialClassMethod
{
    partial class Magic
    {
        private int toads;
        private int spiders;

        public Magic()
        {
            toads = 0;
            spiders = 0;
        }

        public int Toads
        {
            get { return toads; }
            set { toads = value; }
        }
        public int Spiders
        {
            get { return spiders; }
            set { spiders = value; }
        }

        public Magic(int t, int s)
        {
            Toads = t;
            Spiders = s;
        }

        public void doMagic()
        {
            Console.WriteLine("Poof!");
        }

        partial void Print(int t, int s); //Defining partial method

        public void PrintPets(int t, int s)
        {
            Print(t, s);
        }
    }

    partial class Magic
    {
        partial void Print(int t, int s) //Implementing partial method
        {
            Console.WriteLine("Partial class Toads= {0}\nPartial class Spiders= {1}", t, s);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Magic yourMagic = new Magic();
            Console.WriteLine("Spiders = {0} and Toads = {1}", yourMagic.Spiders, yourMagic.Toads);
            Magic mymagic = new Magic(3, 5);
            mymagic.doMagic();
            Console.WriteLine("Spiders = {0} and Toads = {1}\n", mymagic.Spiders, mymagic.Toads);

            Magic partialMagic = new Magic();
            partialMagic.PrintPets(10, 20);
            Console.ReadKey();
        }
    }
}
Posted
Updated 7-Apr-14 12:08pm
v2

1 solution

It looks correct, and using partial types makes a lot of sense and is trivial, but the partial method here is clearly pointless. Non-implemented partial method is a pretty delicate feature which is relatively hard to understand. If has very limited domain of applicability, where it is really useful for something. I explained it in detail in my past answer: Regarding partial method implementation[^].

Please see it with full attention, especially the part after [EDIT]; it's not so easy to understand. Microsoft explanation is not so clear.

In a nutshell: you can easily forget about partial methods without any risk to compromise your productivity, but partial types can greatly enrich your style repertoire. Remember: there is nothing fundamental here, this is all about style and maintainability of your code.

—SA
 
Share this answer
 
v3
Comments
RalvarezHose 7-Apr-14 19:01pm    
Thank you Sergey for confirming the codes. And your past answers are also very helpful.
Sergey Alexandrovich Kryukov 7-Apr-14 19:05pm    
You are very welcome.
Good luck, call again.
—SA
Sergey Alexandrovich Kryukov 7-Apr-14 20:30pm    
By the way, in many cases, when you use "public", "internal" would be better. I prefer not to give more access than it is required.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900