Click here to Skip to main content
15,792,430 members
Home / Discussions / C#
   

C#

 
AnswerRe: Parse complex numbers from string Pin
OriginalGriff12-May-22 10:42
mvaOriginalGriff12-May-22 10:42 
AnswerRe: Parse complex numbers from string Pin
jsc4213-May-22 1:13
professionaljsc4213-May-22 1:13 
GeneralRe: Parse complex numbers from string Pin
Member 1563407116-May-22 9:02
Member 1563407116-May-22 9:02 
GeneralRe: Parse complex numbers from string Pin
jsc4217-May-22 4:19
professionaljsc4217-May-22 4:19 
GeneralRe: Parse complex numbers from string Pin
Richard Deeming17-May-22 4:26
mveRichard Deeming17-May-22 4:26 
GeneralRe: Parse complex numbers from string Pin
jsc4217-May-22 6:05
professionaljsc4217-May-22 6:05 
GeneralRe: Parse complex numbers from string Pin
jsc4217-May-22 23:16
professionaljsc4217-May-22 23:16 
Question"interface hack" to make private nested class instances available to outer class methods ? Pin
BillWoodruff11-May-22 2:25
professionalBillWoodruff11-May-22 2:25 
If you are not familiar with this technique, I will publish a Tip/Trick soon.

Assuming you know how a nested inner class (which implements an Interface) can be declared 'private, and still be accessed by public methods in the enclosing outer class: which does make direct access to inner classes of the OuterClass prohibited.

Do you think the technique shown here is evil, undesirable, or ... ?

usage:
// necessary depending on use
// using NestedClassExample1;

OuterClass1 oc1 = new OuterClass1();

var cat1 = oc1.AddSomething(new Cat("cat1", "meow"));
var cat2 = oc1.AddSomething(new Cat("cat2", "hiss"));
var dog1 = oc1.AddSomething(new Dog("dog1"));

var cat1instance = oc1.GetSomething("cat1");
class:
using System;
using System.Collections.Generic;
using System.Linq;

namespace NestedClassExample1
{
    public interface ISomething1
    {
        string Name { get; }
    }

    public class OuterClass1
    {
        private List<ISomething1> Somethings = new List<ISomething1>();

        internal ISomething1 AddSomething(ISomething1 newsomething)
        {
            Somethings.Add(newsomething);
            return newsomething;
        }

        internal ISomething1 GetSomething(string name)
        {
            return Somethings.FirstOrDefault(smtng => smtng.Name == name);
        }
    }

    public class Cat : ISomething1
    {
        public Cat(string name, string sound = "")
        {
            Name = name;
            CatSound = sound;
        }
        public string Name { get; }
        public string CatSound { set; get; }
    }

    public class Dog : ISomething1
    {
        public Dog(string name, string sound = "")
        {
            Name = name;
            DogSound = sound;
        }

        public string Name { get; }
        public string DogSound { set; get; }
    }

    public class Tiger : ISomething1
    {
        public Tiger(string name) { Name = name; }
        public string Name { get; }
    }
}

«The mind is not a vessel to be filled but a fire to be kindled» Plutarch


modified 12-May-22 6:18am.

AnswerRe: "interface hack" to make private nested class instances available to outer class methods ? Pin
OriginalGriff11-May-22 3:15
mvaOriginalGriff11-May-22 3:15 
GeneralRe: "interface hack" to make private nested class instances available to outer class methods ? Pin
BillWoodruff11-May-22 4:29
professionalBillWoodruff11-May-22 4:29 
GeneralRe: "interface hack" to make private nested class instances available to outer class methods ? Pin
Richard Deeming11-May-22 7:16
mveRichard Deeming11-May-22 7:16 
GeneralRe: "interface hack" to make private nested class instances available to outer class methods ? Pin
BillWoodruff11-May-22 18:20
professionalBillWoodruff11-May-22 18:20 
GeneralRe: "interface hack" to make private nested class instances available to outer class methods ? Pin
Richard Deeming11-May-22 22:37
mveRichard Deeming11-May-22 22:37 
GeneralRe: "interface hack" to make private nested class instances available to outer class methods ? Pin
BillWoodruff12-May-22 0:33
professionalBillWoodruff12-May-22 0:33 
GeneralRe: "interface hack" to make private nested class instances available to outer class methods ? Pin
Richard Deeming12-May-22 1:19
mveRichard Deeming12-May-22 1:19 
GeneralRe: "interface hack" to make private nested class instances available to outer class methods ? Pin
BillWoodruff12-May-22 2:12
professionalBillWoodruff12-May-22 2:12 
GeneralRe: "interface hack" to make private nested class instances available to outer class methods ? Pin
Richard Deeming12-May-22 3:18
mveRichard Deeming12-May-22 3:18 
GeneralRe: "interface hack" to make private nested class instances available to outer class methods ? Pin
BillWoodruff12-May-22 3:32
professionalBillWoodruff12-May-22 3:32 
AnswerRe: "interface hack" to make private nested class instances available to outer class methods ? Pin
Richard MacCutchan11-May-22 4:19
mveRichard MacCutchan11-May-22 4:19 
GeneralRe: "interface hack" to make private nested class instances available to outer class methods ? Pin
BillWoodruff11-May-22 4:50
professionalBillWoodruff11-May-22 4:50 
GeneralRe: "interface hack" to make private nested class instances available to outer class methods ? Pin
harold aptroot11-May-22 11:28
harold aptroot11-May-22 11:28 
GeneralRe: "interface hack" to make private nested class instances available to outer class methods ? Pin
BillWoodruff11-May-22 18:34
professionalBillWoodruff11-May-22 18:34 
GeneralRe: "interface hack" to make private nested class instances available to outer class methods ? Pin
BillWoodruff12-May-22 2:14
professionalBillWoodruff12-May-22 2:14 
QuestionToast notification Collections crash when initializing. Pin
Maximilien9-May-22 4:12
Maximilien9-May-22 4:12 
AnswerRe: Toast notification Collections crash when initializing. Pin
Gerry Schmitz9-May-22 9:26
mveGerry Schmitz9-May-22 9:26 

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.