Click here to Skip to main content
15,915,508 members
Home / Discussions / C#
   

C#

 
GeneralRe: Forms opening other forms Pin
Member 112336118-Sep-18 0:03
Member 112336118-Sep-18 0:03 
GeneralRe: Forms opening other forms Pin
Member 112336118-Sep-18 0:06
Member 112336118-Sep-18 0:06 
GeneralRe: Forms opening other forms Pin
OriginalGriff8-Sep-18 0:07
mveOriginalGriff8-Sep-18 0:07 
GeneralRe: Forms opening other forms Pin
Matias Lopez11-Sep-18 8:50
Matias Lopez11-Sep-18 8:50 
QuestionC# project - SMS Alert Pin
Member 124675946-Sep-18 21:51
Member 124675946-Sep-18 21:51 
AnswerRe: C# project - SMS Alert Pin
OriginalGriff6-Sep-18 22:05
mveOriginalGriff6-Sep-18 22:05 
AnswerRe: C# project - SMS Alert Pin
DerekT-P13-Sep-18 1:01
professionalDerekT-P13-Sep-18 1:01 
QuestionCrypto in C#/4.0 using PKCS#10 and SHA256 Pin
ninodago6-Sep-18 5:28
ninodago6-Sep-18 5:28 
QuestionProblems with Rx EventMessager [Solved] Pin
Kenneth Haugland5-Sep-18 4:10
mvaKenneth Haugland5-Sep-18 4:10 
AnswerRe: Problems with Rx EventMessager Pin
Pete O'Hanlon5-Sep-18 4:49
mvePete O'Hanlon5-Sep-18 4:49 
AnswerRe: Problems with Rx EventMessager Pin
Richard Deeming5-Sep-18 4:54
mveRichard Deeming5-Sep-18 4:54 
GeneralRe: Problems with Rx EventMessager Pin
Kenneth Haugland5-Sep-18 9:49
mvaKenneth Haugland5-Sep-18 9:49 
GeneralRe: Problems with Rx EventMessager Pin
Kenneth Haugland5-Sep-18 9:56
mvaKenneth Haugland5-Sep-18 9:56 
QuestionUnable to Access Method in Referenced .dll (C#/VS2017) Pin
Member 139418904-Sep-18 22:55
Member 139418904-Sep-18 22:55 
AnswerRe: Unable to Access Method in Referenced .dll (C#/VS2017) Pin
Richard MacCutchan4-Sep-18 23:10
mveRichard MacCutchan4-Sep-18 23:10 
GeneralRe: Unable to Access Method in Referenced .dll (C#/VS2017) Pin
Member 139418904-Sep-18 23:19
Member 139418904-Sep-18 23:19 
AnswerRe: Unable to Access Method in Referenced .dll (C#/VS2017) Pin
Member 139418905-Sep-18 0:24
Member 139418905-Sep-18 0:24 
QuestionConstructor Injection Pin
jones298474-Sep-18 7:33
jones298474-Sep-18 7:33 
AnswerRe: Constructor Injection Pin
Mc_Topaz4-Sep-18 10:15
Mc_Topaz4-Sep-18 10:15 
You have many questions in one message.
I have created a "Zelda" example with lot of constructors. I hope you find it useful.

Hint: Start a new console application in C#, called ConsoleApplication1. Paste the code, set break points and debug.

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var maxArrows = Link.ArrowMaximum;      // Calls static construxtor.
            var link = new Link();                  // Calls default construtor.

            IWeapon weapon = new KokiriSword();
            link = new Link(10);                    // Calls parameterized constructor without injection.
            link = new Link(weapon);                // Calls parameterized constructor with injection;
            link = new Link("Go to Lost woods");    // Calls internal constructor within this assembly only.

            var fierceDeityLink = new FierceDeityLink();    // Calls the protected constructor through the public constructor of the derivied class.
        }
    }

    public interface IWeapon
    {
        int Damage { get; }
    }

    public class NoWeapon : IWeapon
    {
        public int Damage { get; private set; }
        public NoWeapon()
        {
            Damage = 0;
        }
    }

    public class KokiriSword : IWeapon
    {
        public int Damage { get; private set; }
        public KokiriSword()
        {
            Damage = 1;
        }
    }

    public class BigGoronSword : IWeapon
    {
        public int Damage { get; private set; }
        public BigGoronSword()
        {
            Damage = 100;
        }
    }

    public class Link
    {
        public static int ArrowMaximum { get; private set; }
        public int Rupies { get; private set; }
        public bool RecuredZelda { get; private set; }
        public string HintFromNavi { get; private set; }
        public IWeapon Weapon { get; private set; }

        static Link()
        {
            // Static construxctor: Will run only run once when the class is first refereed in the code.
            ArrowMaximum = 100;
        }

        public Link()
        {
            // Default constructor: Initialize the property with the default value 5.
            Rupies = 5;
            Weapon = new NoWeapon();
        }

        private Link(bool recusedZelda)
        {
            // Private constructor: Must be called within the class itself.
            // Have never used this "feature" at all.
            RecuredZelda = recusedZelda;
        }

        public Link(int rupies)
        {
            // Parameterized constructor: Initialize the property with some value.
            Rupies = rupies;
        }

        public Link(IWeapon weapon)
        {
            // Parameterized constructor with injection.
            // Link will have a weapon.
            Weapon = weapon;
        }

        protected Link(int rupies, bool recuedZelda, IWeapon weapon) : this(rupies)
        {
            // Protected constructor: Will call the parametrized constructor with some values defined in the derivided class.
            // Showing two ways to initialize the properties in the class.
            RecuredZelda = recuedZelda;
            Weapon = weapon;
        }

        internal Link(string hintFromNavi)
        {
            // Internal constructor: This constructor is only available inside the assembly.
            HintFromNavi = hintFromNavi;
        }
    }

    public class FierceDeityLink : Link
    {
        public FierceDeityLink() : base (int.MaxValue, true, new BigGoronSword())
        {
            // You don't mess with Fierce Deity Link :)
        }
    }
}


Answers for your other questions:

Where would Injection be used?
Lots of places. Depends on the logic of the code. Read here.

What are the risks of modifications to the constructor, does it break the "target" code?
Impossible to say without reviewing the code.
Try... and learn from mistakes.

How do constructors allow for unit testing?
Constructors are used to create objects from classes. That has nothing to do with unit testing.

What is unit testing?
Something which is very useful and you should adapt immediately. Read here.
QuestionAforge Video-Picture quality Pin
User 136751144-Sep-18 1:08
User 136751144-Sep-18 1:08 
QuestionC# convert image format to dpx Pin
Member 135795963-Sep-18 23:28
Member 135795963-Sep-18 23:28 
AnswerRe: C# convert image format to dpx Pin
OriginalGriff4-Sep-18 1:10
mveOriginalGriff4-Sep-18 1:10 
SuggestionRe: C# convert image format to dpx Pin
Richard Deeming4-Sep-18 1:48
mveRichard Deeming4-Sep-18 1:48 
Question"Hiding" functions from StackTrace Pin
Bernhard Hiller3-Sep-18 2:37
Bernhard Hiller3-Sep-18 2:37 
AnswerRe: "Hiding" functions from StackTrace Pin
OriginalGriff3-Sep-18 4:12
mveOriginalGriff3-Sep-18 4:12 

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.