E
eric.burgin
I have not heavily used interfaces in the past so I am looking for
advice. An interface may not be the best solution but it was the first
idea I had and was working at first.
Here is the situation, I have been asked to create an application that
will provide a "script" of things to say, yes/no questions to ask,
information to look up etc.
This could be used by multiple clients to I am creating a class library
to implement the functionality. I have created a class Script that
contains a collection of steps in the script and a property CurrentItem
that returns the current step in the script.
I originally thought I would create an interface iScriptItem, have each
type of item implement it, and make the CurrentItem property of Script
an iScriptItem.
iScriptItem contained three properties: ID, Text, and Type
So from the client, we could move through the items in the script like
this:
Dim myScript as New Script
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNext.Click
myScript.MoveNext()
ConsoleWriteLine(myScript.CurrentItem.Text)
End Sub
This works great. The problem is when I get to the point where my
script steps have different properties, for example,
I have a "Statement" step that displays text to the user. It has the
property "NextItem"
I also have a "Question" step that has two properties called "YesItem"
and "NoItem" and does not need a NextItem property
The problem is, in the code above, these properties are not available
because CurrentItem is defined an an iScriptItem.
So here is my question. Is it better to have the interface contain all
the properties that may be used in the different item types (even
though they won't all be used by all the types), leave the interface
simple and use CType to convert CurrentItem to the correct type of
object (based on the type property), or some better way to do it? Is
my design idea just way off?
Again, I don't have a lot of experience with interfaces so this may
have been the wrong way to go about this. I don't have a lot of code
written as I am really in the early design phase and it's mostly on my
white board
advice. An interface may not be the best solution but it was the first
idea I had and was working at first.
Here is the situation, I have been asked to create an application that
will provide a "script" of things to say, yes/no questions to ask,
information to look up etc.
This could be used by multiple clients to I am creating a class library
to implement the functionality. I have created a class Script that
contains a collection of steps in the script and a property CurrentItem
that returns the current step in the script.
I originally thought I would create an interface iScriptItem, have each
type of item implement it, and make the CurrentItem property of Script
an iScriptItem.
iScriptItem contained three properties: ID, Text, and Type
So from the client, we could move through the items in the script like
this:
Dim myScript as New Script
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNext.Click
myScript.MoveNext()
ConsoleWriteLine(myScript.CurrentItem.Text)
End Sub
This works great. The problem is when I get to the point where my
script steps have different properties, for example,
I have a "Statement" step that displays text to the user. It has the
property "NextItem"
I also have a "Question" step that has two properties called "YesItem"
and "NoItem" and does not need a NextItem property
The problem is, in the code above, these properties are not available
because CurrentItem is defined an an iScriptItem.
So here is my question. Is it better to have the interface contain all
the properties that may be used in the different item types (even
though they won't all be used by all the types), leave the interface
simple and use CType to convert CurrentItem to the correct type of
object (based on the type property), or some better way to do it? Is
my design idea just way off?
Again, I don't have a lot of experience with interfaces so this may
have been the wrong way to go about this. I don't have a lot of code
written as I am really in the early design phase and it's mostly on my
white board