Click here to Skip to main content
15,882,163 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to launch a form only once Pin
OriginalGriff26-Oct-20 21:15
mveOriginalGriff26-Oct-20 21:15 
GeneralRe: How to launch a form only once Pin
Ismael_199927-Oct-20 10:40
Ismael_199927-Oct-20 10:40 
GeneralRe: How to launch a form only once Pin
OriginalGriff27-Oct-20 10:42
mveOriginalGriff27-Oct-20 10:42 
AnswerRe: How to launch a form only once Pin
Gerry Schmitz26-Oct-20 14:50
mveGerry Schmitz26-Oct-20 14:50 
AnswerRe: How to launch a form only once Pin
Richard Deeming26-Oct-20 22:10
mveRichard Deeming26-Oct-20 22:10 
QuestionDesign an elegant solution for choosing a class whose method gets called Pin
Member 1497555126-Oct-20 6:13
Member 1497555126-Oct-20 6:13 
AnswerRe: Design an elegant solution for choosing a class whose method gets called Pin
Gerry Schmitz26-Oct-20 14:58
mveGerry Schmitz26-Oct-20 14:58 
Questionan apparent violation of C#-generic "physics" ? (language issue) Pin
BillWoodruff25-Oct-20 22:18
professionalBillWoodruff25-Oct-20 22:18 
I am baffled by how the code in the 'Human and 'Cat classes can successfully call a generic static method (SetCache<T>(T value)) without supplying the usual <Type> adornment.

Here's a sample usage of the code:
BeingCache cache = BeingCache.CacheInstance;

Human ahuman = new Human("joe", "male");
Cat acat = new Cat("Meow", true);

IBeing b1 = cache[1]; // works

IBeing<Cat> b2 = (IBeing<Cat>) b1; // works

Cat cat = b2.IBInstance; // works

bool st0 = b2 is Cat; // false

bool st1 = b2 is Being<Cat>; // true

bool st2 = b2 is IBeing<Cat>; // true

Cat hepcat = BeingCache.GetBeing<Cat>(cache[1]); // works

IBeing<Cat> b3 = (IBeing<Cat>) cache[1]; // works
The example: note that classes 'Human and 'Cat do not inherit from anything.
using System;
using System.Collections.Generic;

namespace InterfaceInheritance_2020
{
    public class Human
    {
        public Human(string name, string gender)
        {
            Name = name;
            Gender = gender;

            BeingCache.SetCache(this); //  :wtf: 
        }

        public string Gender{ get;}
        public string Name { get; }
    }

    public class Cat
    {
        public Cat(string name, bool haswhiskers = true)
        {
            Name = name;
            HasWhiskers = haswhiskers;

            BeingCache.SetCache(this); //  :wtf: 
        }

        public bool HasWhiskers { get; }
        public string Name { get; }
    }

    public interface IBeing
    {
        Type BeingType { set; get; }
    }

    public interface IBeing<T> : IBeing
    {
        T IBInstance { set; get; }
    }

    public class Being<T> : IBeing<T>
    {
        public Being(T content)
        {
            IBInstance = content;
            BeingType = typeof(IBeing<T>);
        }

        public Type BeingType { get; set; }
        public T IBInstance { get; set; }
    }

    //singleton 
    public sealed class BeingCache : List<IBeing>
    {
        private BeingCache()
        {
        }

        public static BeingCache CacheInstance{ get; } = new BeingCache();

        public static void SetCache<T>(T value)
        {
            CacheInstance.AddBeing(new Being<T>(value), typeof(T));
        }

        public void AddBeing(IBeing being, Type btype)
        {
            being.BeingType = btype;
            CacheInstance.Add(being);
        }

        public static T GetBeing<T>(IBeing being)
        {
            return (T) ((IBeing<T>) being).IBInstance;
        }
    }
}

«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali

AnswerRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
pkfox25-Oct-20 22:58
professionalpkfox25-Oct-20 22:58 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
BillWoodruff25-Oct-20 23:15
professionalBillWoodruff25-Oct-20 23:15 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
pkfox25-Oct-20 23:59
professionalpkfox25-Oct-20 23:59 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
BillWoodruff26-Oct-20 2:13
professionalBillWoodruff26-Oct-20 2:13 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
pkfox26-Oct-20 2:48
professionalpkfox26-Oct-20 2:48 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
F-ES Sitecore26-Oct-20 1:15
professionalF-ES Sitecore26-Oct-20 1:15 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
BillWoodruff26-Oct-20 2:09
professionalBillWoodruff26-Oct-20 2:09 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
Richard MacCutchan26-Oct-20 2:38
mveRichard MacCutchan26-Oct-20 2:38 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
F-ES Sitecore26-Oct-20 2:55
professionalF-ES Sitecore26-Oct-20 2:55 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
Richard Deeming26-Oct-20 2:34
mveRichard Deeming26-Oct-20 2:34 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
BillWoodruff26-Oct-20 3:50
professionalBillWoodruff26-Oct-20 3:50 
AnswerRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
BillWoodruff26-Oct-20 23:59
professionalBillWoodruff26-Oct-20 23:59 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
harold aptroot27-Oct-20 0:45
harold aptroot27-Oct-20 0:45 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
BillWoodruff27-Oct-20 5:46
professionalBillWoodruff27-Oct-20 5:46 
QuestionHow to allow numbers and special characters in a textBox in C#? Pin
Alex Dunlop25-Oct-20 5:51
Alex Dunlop25-Oct-20 5:51 
AnswerRe: How to allow numbers and special characters in a textBox in C#? Pin
Richard MacCutchan25-Oct-20 6:19
mveRichard MacCutchan25-Oct-20 6:19 
GeneralRe: How to allow numbers and special characters in a textBox in C#? Pin
Alex Dunlop25-Oct-20 7:00
Alex Dunlop25-Oct-20 7:00 

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.