Click here to Skip to main content
14,689,507 members
Home » Discussions » C#
   

C#

 
GeneralRe: How to launch a form only once Pin
OriginalGriff26-Oct-20 22:15
mveOriginalGriff26-Oct-20 22:15 
GeneralRe: How to launch a form only once Pin
Member 1479239327-Oct-20 11:40
MemberMember 1479239327-Oct-20 11:40 
GeneralRe: How to launch a form only once Pin
OriginalGriff27-Oct-20 11:42
mveOriginalGriff27-Oct-20 11:42 
AnswerRe: How to launch a form only once Pin
Gerry Schmitz26-Oct-20 15:50
mveGerry Schmitz26-Oct-20 15:50 
AnswerRe: How to launch a form only once Pin
Richard Deeming26-Oct-20 23:10
mveRichard Deeming26-Oct-20 23:10 
QuestionDesign an elegant solution for choosing a class whose method gets called Pin
Member 1497555126-Oct-20 7:13
MemberMember 1497555126-Oct-20 7:13 
AnswerRe: Design an elegant solution for choosing a class whose method gets called Pin
Gerry Schmitz26-Oct-20 15:58
mveGerry Schmitz26-Oct-20 15:58 
Questionan apparent violation of C#-generic "physics" ? (language issue) Pin
BillWoodruff25-Oct-20 23:18
mveBillWoodruff25-Oct-20 23: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 23:58
professionalpkfox25-Oct-20 23:58 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
BillWoodruff26-Oct-20 0:15
mveBillWoodruff26-Oct-20 0:15 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
pkfox26-Oct-20 0:59
professionalpkfox26-Oct-20 0:59 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
BillWoodruff26-Oct-20 3:13
mveBillWoodruff26-Oct-20 3:13 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
pkfox26-Oct-20 3:48
professionalpkfox26-Oct-20 3:48 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
F-ES Sitecore26-Oct-20 2:15
mveF-ES Sitecore26-Oct-20 2:15 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
BillWoodruff26-Oct-20 3:09
mveBillWoodruff26-Oct-20 3:09 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
Richard MacCutchan26-Oct-20 3:38
mveRichard MacCutchan26-Oct-20 3:38 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
F-ES Sitecore26-Oct-20 3:55
mveF-ES Sitecore26-Oct-20 3:55 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
Richard Deeming26-Oct-20 3:34
mveRichard Deeming26-Oct-20 3:34 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
BillWoodruff26-Oct-20 4:50
mveBillWoodruff26-Oct-20 4:50 
AnswerRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
BillWoodruff27-Oct-20 0:59
mveBillWoodruff27-Oct-20 0:59 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
harold aptroot27-Oct-20 1:45
Memberharold aptroot27-Oct-20 1:45 
GeneralRe: an apparent violation of C#-generic "physics" ? (language issue) Pin
BillWoodruff27-Oct-20 6:46
mveBillWoodruff27-Oct-20 6:46 
QuestionHow to allow numbers and special characters in a textBox in C#? Pin
Alex Dunlop25-Oct-20 6:51
MemberAlex Dunlop25-Oct-20 6:51 
AnswerRe: How to allow numbers and special characters in a textBox in C#? Pin
Richard MacCutchan25-Oct-20 7:19
mveRichard MacCutchan25-Oct-20 7:19 
GeneralRe: How to allow numbers and special characters in a textBox in C#? Pin
Alex Dunlop25-Oct-20 8:00
MemberAlex Dunlop25-Oct-20 8: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.