Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use generics for this:</p> <pre><code>Sub createSystem(Of T As {New, systemSettings})() Dim newSystem As New T systemCollection.add(newSystem) End Sub </code></pre> <p>And call it with:</p> <pre><code>createSystem(Of windSystem) </code></pre> <p>To explain:</p> <p>The term <code>Of T</code> lets you create a method that can be used for any type. Every time you call it for a new value of T, a new method is created in memory.</p> <p>The term <code>As {New, systemSettings}</code> constrains <code>T</code>. It says that T must represent a type that is or derives from <code>systemSettings</code>. It also says that <code>T</code> must contain a default constructor: <code>New()</code> which is required for the command <code>New T</code>. Note that you cannot specify a more elaborate constructor as a generics constraint.</p> <p>If you require parameters in your constructor, you can create an <code>Initialise</code> method in the base class. Because <code>T</code> is constrained to <code>systemSettings</code>, it is guaranteed that the <code>Initialise</code> method exists.</p> <pre><code>Class systemSettings Public Overridable Sub Initialise(arg1 As Object, arg2 As Object) 'initialise properties End Sub 'some properties End class Class fireSystemSettings Inherits systemSettings Public Overrides Sub Initialise(arg1 As Object, arg2 As Object) 'initialise properties End Sub 'some additional properties End Class Class windSystemSettings Inherits systemSettings Public Overrides Sub Initialise(arg1 As Object, arg2 As Object) 'initialise properties End Sub 'some additional properties End Class Sub createSystem(Of T As {New, systemSettings})(arg1 As Object, arg2 As Object) Dim newSystem As New T newSystem.Initialise(arg1, arg2) systemCollection.add(newSystem) End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload