{"id":232,"date":"2006-04-14T04:40:54","date_gmt":"2006-04-14T08:40:54","guid":{"rendered":"http:\/\/weblog.soulhuntre.com\/items\/date\/2006\/04\/14\/implementing-delayed-class-type-selection-via-interfaces-in-net\/"},"modified":"2006-04-14T04:40:54","modified_gmt":"2006-04-14T08:40:54","slug":"implementing-delayed-class-type-selection-via-interfaces-in-net","status":"publish","type":"post","link":"http:\/\/legacyiamsenseiken.local\/2006\/04\/14\/implementing-delayed-class-type-selection-via-interfaces-in-net\/","title":{"rendered":"Schr\u00c3\u00b6dinger's box: Implementing delayed class type selection via Interfaces in .NET"},"content":{"rendered":"
Some geek speak here. This are really the notes for a more complete article, but it might help some folks out so I want to put it up before I forget what I learned when I go to sleep!<\/p>\n
In a Asp.Net<\/a> development project I need to be able to call the methods and properties of an object in order to extract data from it. The problem is that I will not know until runtime which of several competing implementations I might need to call. It will depend on a number of factors. <\/p>\n How then can I make calls to an object without knowing what object type it is in advance? The solution in this case is what .Net calls “interfaces”.<\/p>\n Go ahead, read more.<\/p>\n ed note: I code in C#, but these features are available under VB.net. <\/em><\/p>\n <\/p>\n We are going to re-implement Schr\u00c3\u00b6dinger’s box. Basically, we will write some code that can query something about whether it is alive or dead without knowing in advance what type of animal it is.<\/p>\n Basically what we want to be able to do is only<\/i><\/b> care that whatever type of object we have, it will implement the bare minimum of what we need to do the experiment. It needs to do two things…<\/p>\n So, no matter what type of animal we have it has to be a valid one for the test. We will tell this to C# by creating a interface definition<\/a> in the App_Code folder. It seems to be tradtional to start interface definitions with a capital “I”. Here is that file (you can call it anything, but I picked “ItestAnimal.cs”). <\/p>\n As you can see, this file implements nothing<\/i><\/b>. Ever object that wants to be a ItestAnimal needs to implement that functionality itself. BTW = those weird definitions are tied to the .NET 2.0 way to declare class properties.<\/p>\n We have two of them, “Dog” and “Cat” (Dog.cs and Cat.cs of course). I will show you only Dog here but Cat is the same thing, just do a search\/replace of “Dog” with “Cat”. You may also want to check the constructor comment and change that.<\/p>\n It is important to understand<\/b> that these classes do not have to have anything in common at all though these do for simplicity. The can also implement the interface required functions in totally different ways. It won’t change anything – that is the whole point. <\/p>\n All the magic happens at the class declaration<\/b> where the “:” indicates that this class implements a particular interface. You can implement a bunch of them, just use a comma to separate them.<\/p>\n I am not going to try and implement the whole thing here. You can do that for fun. What I will show you is that it doesn’t make any difference which animal you use, so you can delay the choice till runtime.<\/p>\n Notice that we do not need to know or care what type of object something is as long as we only want to work with the properties and methods of the interface.<\/p>\n Here’s what you do to prepare the test page or form:<\/p>\n Once all that is done, here is the callback for the button or link…<\/p>\n Enjoy!\n<\/p>\n<\/p>\n<\/p>\n<\/p>\n Some geek speak here. This are really the notes for a more complete article, but it might help some folks out so I want to put it up before I forget what I learned when I go to sleep! The Problem In a Asp.Net development project I need to be able to call the methods […]<\/p>\n","protected":false},"author":3,"featured_media":53195,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[278],"tags":[],"_links":{"self":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/posts\/232"}],"collection":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/comments?post=232"}],"version-history":[{"count":0,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/posts\/232\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/media\/53195"}],"wp:attachment":[{"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/media?parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/categories?post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/legacyiamsenseiken.local\/wp-json\/wp\/v2\/tags?post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}Problem Scenario<\/h3>\n
\n
The Interface<\/h3>\n
public interface ItestAnimal\n{\n bool isAlive { get; set; }\n string whatType { get; }\n}<\/pre>\n<\/p>\n
The Animal Classes and Interface Declaration<\/h3>\n
public class Dog : ItestAnimal\n{\n private bool alive;\n\n public bool isAlive\n {\n get\n {\n return alive;\n }\n\n set\n {\n alive = value;\n }\n }\n\n public string whatType\n {\n get\n {\n return \"Dog\";\n }\n }\n\n\tpublic Dog()\n\t{\n\t\t\/\/\n\t\t\/\/ Woof!\n\t\t\/\/\n\t}\n}\n<\/pre>\n
Simple Usage Example<\/h3>\n<\/p>\n
\/\/ this section was created for the blog post,\n \/\/ it may have a typo but you get the idea\n \/\/\n ItestAnimal animal_1;\n ItestAnimal animal_2;\n string animal_1_type;\n string animal_2_type;\n\n animal_1 = new Dog();\n animal_2 = new Cat();\n\n animal_1.isAlive = true;\n animal_2.isAlive = false;\n\n animal_1_type = animal_1.whatType;\n animal_2_type = animal_2.whatType;\n<\/pre>\n<\/p>\n
Schr\u00c3\u00b6dinger’s box <\/h3>\n
\n
protected void doPeek_Click(object sender, EventArgs e)\n {\n ItestAnimal thethinginthebox;\n\n switch (animalList.Text)\n {\n case \"Cat\":\n thethinginthebox = new Cat();\n thethinginthebox.isAlive = statusFlag.Checked;\n break;\n case \"Dog\":\n thethinginthebox = new Dog();\n thethinginthebox.isAlive = statusFlag.Checked;\n break;\n default:\n \/\/ I like dogs better\n thethinginthebox = new Dog();\n thethinginthebox.isAlive = statusFlag.Checked;\n break;\n }\n\n if (thethinginthebox.isAlive)\n {\n peek.Text = thethinginthebox.whatType + \" is Alive!!!!\";\n }\n else\n {\n peek.Text = thethinginthebox.whatType + \" is Dead!!!!\";\n }\n }\n<\/pre>\n
References<\/h3>\n
\n