Click here to Skip to main content
15,901,001 members
Home / Discussions / C#
   

C#

 
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 
AnswerRe: "Hiding" functions from StackTrace Pin
Gerry Schmitz4-Sep-18 4:53
mveGerry Schmitz4-Sep-18 4:53 
Questionhow to use array in main that was created in class? Pin
Member 139703232-Sep-18 8:51
Member 139703232-Sep-18 8:51 
AnswerRe: how to use array in main that was created in class? Pin
Luc Pattyn2-Sep-18 16:40
sitebuilderLuc Pattyn2-Sep-18 16:40 
QuestionVB.net dll use with C# windows form Pin
BobbyStrain1-Sep-18 18:09
BobbyStrain1-Sep-18 18:09 
AnswerRe: VB.net dll use with C# windows form Pin
OriginalGriff1-Sep-18 19:55
mveOriginalGriff1-Sep-18 19:55 
GeneralRe: VB.net dll use with C# windows form Pin
BobbyStrain2-Sep-18 4:51
BobbyStrain2-Sep-18 4:51 
GeneralRe: VB.net dll use with C# windows form Pin
OriginalGriff2-Sep-18 4:57
mveOriginalGriff2-Sep-18 4:57 
GeneralRe: VB.net dll use with C# windows form Pin
BobbyStrain3-Sep-18 17:32
BobbyStrain3-Sep-18 17:32 
GeneralRe: VB.net dll use with C# windows form Pin
OriginalGriff3-Sep-18 20:12
mveOriginalGriff3-Sep-18 20:12 
GeneralRe: VB.net dll use with C# windows form Pin
BobbyStrain4-Sep-18 17:17
BobbyStrain4-Sep-18 17:17 
GeneralRe: VB.net dll use with C# windows form Pin
Mycroft Holmes4-Sep-18 19:21
professionalMycroft Holmes4-Sep-18 19:21 

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.