vbscript static class variables/methods? - oop

Is there a way to have one variable per class in vbscript?
If not what is the best way to emulate it? Prefixing a global variable declared next to the class?
Also is there a way to declare static/class methods(for a static constructor) or am I force to prefix a function?

In languages that support class-level/static data or methods you can
associate/bind data or methods explicitly to the set of objects defined by the class. So you can have Customer.Count and Product.Count and a plain Count (or ##Count) in Customer code will access the right number.
use such data or method without having an instance of the class (yet).
VBScript does not support static data or methods. You have to use global data or functions/subs and do the associating in your mind (perhaps with a little help from a naming convention). Accessing these 'static'=global elements without an object is trivial, but - obviously - should be done with care.
You can embed one or more singleton objects or code references (GetRef()) in your objects to bind them closer to the class, but that will increase the size of the instances.

You can do something like this to sort of emulate a static class:
Class Defines_
Public Sub DoSomethingUseful
End Sub
End Class
Dim Defines : Set Defines = New Defines_
...
Defines.DoSomethingUseful
This can be used to give you something analogous to constructors (really, factory methods):
Class Something
Private mValue
Public Property Get Value : Value = mValue : End Property
Public Property Let Value(x) : mValue = x : End Property
End Class
Class SomethingFactory_
Public Function Create(value)
Set Create = New Something
Create.Value = value
End Function
End Class
Dim SomethingFactory : Set SomethingFactory = New SomethingFactory_
...
Dim something : Set something = SomethingFactory.Create(5)

Related

How can I create a generic class that only works for classes that support certain interface?

Class cacheable(Of T As haveTimeStampandMainStringKey)
Public ReadOnly Property Cache As T
Public ReadOnly Property timestamp As Date
Public Shared Function create(cache1 As T) As cacheable(Of T)
Dim a = New cacheable(Of T)
a._Cache = cache1
a._timestamp = Now
Dim key = T.mainkey 'this things fail to compile
Return a
End Function
End Class
Interface haveTimeStampandMainStringKey
ReadOnly Property TimeStamp As DateTime
ReadOnly Property mainKey As String
End Interface
Basically I want class cacheable to work only with classes that support haveTimeStampandMainStringKey
Yet
Dim key = T.mainkey produces an error
Clearly T supports haveTimeStampandMainStringKey interface. So I should be able to access T.mainkey. I can't. Why? What's wrong with the code?
Why?
It doesn't work because T is a type, not an instance. You need to have an instance to refer to mainKey. You probably want either a.Cache.mainKey or cache1.mainKey.
(If you really want something Shared rather than something attached to an instance, unfortunately, there isn't a good way to do it as it's not supported by .NET except through various reflection-based approaches, see various lamentations about the absence of "static interfaces" over the years.)

What is the purpose of declaring a Class within another Class?

I come from the VBA world where options to breakdown your code into classes, namespaces, and modules is limited. Now I just landed in a world where the options are many, and I feel lost.
I would like to know what is the purpose of declaring a Class within another Class? (see example below)
Class FirstClass
Public OnePropertyInside As String
Class SecondClass
Public AnotherProperty As String
End Class
End Class
If I create a new instance of FirstClass (say myFirstClass), SecondClass is not instantiated.
Even more bizzare (to me at least), is that intelissense offers me myFirstClass.SecondClass. Obviously, because the class is not instantiated, I cannot access any of its members.
So, is that usefull only if the SecondClass contains shared members?
To try answering that question I added a shared member within SecondClass:
Class FirstClass
Public OnePropertyInside As String
Class SecondClass
Public AnotherProperty As String
Public Shared SharedProperty As String
End Class
End Class
I ran a few tests which brought secondary questions (see comments in code)
Sub Main()
Dim myFirstClass As New FirstClass
'Works as expected
Console.WriteLine(myFirstClass.OneProperty)
'What is the difference between the two lines below?
Console.WriteLine(myFirstClass.SecondClass.SharedProperty)
Console.WriteLine(FirstClass.SecondClass.SharedProperty)
'This line cannot be compiled, this demonstrates SecondClass is not instantiated when FirstClass is.
Console.WriteLine(myFirstClass.SecondClass.AnotherProperty)
Dim mySecondClass As New FirstClass.SecondClass
'Works as expected, but I feel this hierarchy should better be dealt with through a namespace statement?
Console.WriteLine(mySecondClass.AnotherProperty)
End Sub
You can think of it as if the inner most class is a helper class of sorts. It may not even need to be used at all. Nesting the inner class(or simply nested class) inside the outer class gives you access to all of the members of the outer one. You can even access the private members inside that initial outer class.
Edit: For clarification, I mean to say that the the inner can access the private members of the outer, not the other way around.
You usually do this because you want to restrict the scope of the nested class.
So, if you only need to use this class from within the "parent" class (in terms of scope), then its usually a good idea to define it as a nested class.
If you might might need to use the class outside of its assembly, then it is better to define it as a completely separate class (in its own file), and then define your relationship accordingly. You will need to instantiate one within the other (this is the same whether its seperate or nested - so its location is largely irrelevant for that point).
When you do that, and the inner class is accessible to other classes (it's accessibility is Public or Friend), the outer class basically just works like a namespace. So for instance, using your example, you could create a new object of the nested class without ever creating one of the outer class:
Dim x As New FirstClass.SecondClass()
The most obvious benefit is the structural organization of the code, much like namespaces and code files. So, for instance, it's not uncommon to use nested classes for constants, to help better organize them:
Public Class Urls
Public Class Processing
Public Const Submit As String = "..."
Public Const Cancel As String = "..."
End Class
Public Class Reporting
Public Const Daily As String = "..."
Public Const Weekly As String = "..."
End Class
End Class
' ...
Dim url As String = Urls.Reporting.Daily
However, outside of the narrow set of situations where things like that are useful, most people would prefer to not nest public classes at all.
However, as others have mentioned, the one place where you really will see nested classes used fairly regularly is for Private ones. If you need some small helper class which will have no use to code outside of your class, there's no reason to expose it. Even if you set it's accessibility to Friend, it will still be visible to all the other classes in the same project. Therefore, if you really want to hide it from everything else, you'll want to make it a nested private class. For instance:
Public Class MyClass
Public Function GetTheIdOfSomething() As Integer
Dim d As Details = GetDetailsAboutSomething()
If d.Value Is Nothing Then
Return d.Id
Else
Throw New Exception()
End If
End Sub
Private Function GetDetailsAboutSomething() As Details
' ... return a Details object
End Function
Private Class Details
Public Property Id As Integer
Public Property Value As String
End Class
End Class

How To Access A Shared Property Of A Class Passed As A Type Parameter

I'm trying to access a shared property of a class passed as a parameter to a type-parametrised procedure. The reason why I'm doing this is so I can embed the various API call endpoints (among other class-specific things) as properties within the class itself. I've read some similar SO posts but nothing is close enough to be sure that it isn’t possible (which I think is likely).
Below is the essence of the structure - there's some pseudo code towards the end:
MustInherit Class BaseClass
Shared Property Endpoint As String
End Class
Class Person
Inherits BaseClass
Property Age As Integer
Property Name As String
Sub New()
_Endpoint = "/GetPerson"
End Sub
End Class
Class Event
Inherits BaseClass
Property When As Date
Property Type As String
Sub New()
_Endpoint = "/GetEvent"
End Sub
End Class
Function Retrieve(T As BaseClass)(Id As String) As T
Dim oResp As HttpResponse = MakeGetCall(T.Endpoint, Id) <- T.Endpoint throws a compile error
Return Deserialize(Of T)(oResp.Content)
End Function
Dim oPerson As Person = Retrieve(Of Person)("123")
Dim oEvent As Event = Retrieve(Of Event)("123")
To my tiny mind, I would have thought that, since T’s base class is BaseClass which contains the property Endpoint, I’d be ok. But seemingly not.
I've tried a fair few things from here on SO and other places to overcome this to no avail. Yes, I realize I could perform some kind of endpoint look-up based on the type of T but the above represents a very clean solution and I’d like to get it to work if possible.
Any ideas?
Assuming you want EndPoint to be different for each subclass, you should use MustOverride instead of Shared...
MustInherit Class BaseClass
Public MustOverride Property EndPoint As String
End Class
Then return a constant in each subclass
Class Person
Inherits BaseClass
Public Overrides Property EndPoint As String
Get
Return "/Person"
End Get
You might want to declare EndPoint as ReadOnly too.
The small limitation is that you'll need an instance of the class to access EndPoint (since it isn't Shared). If you have a parameterless constructor, you could use (New Person).EndPoint where needed.

Why are arguments renamed to RHS when implementing an Interface in VBA?

When you implement an Interface in your Class the arguments are automatically named RHS as shown on MDSN https://msdn.microsoft.com/en-us/library/office/gg264387.aspx
For example, if I create IInterface as so:
Public Property Let Value1(strValue1 As String)
End Property
Public Property Let Value2(strValue2 As String)
End Property
And implement it, the class would look like this:
Implements IInterface
Private Property Let IInterface_Value1(RHS As String)
End Property
Private Property Let IInterface_Value2(RHS As String)
End Property
It's a best practice to name your arguments in such a way as to provide some level of abstraction and make it easier to read and write code. I can actually change the arguments to whatever I want in the class after I've implemented the statements, as shown below, but my question is why does this happen? Is RHS a leftover from another language or is there a particular reason it's named like this?
Implements IInterface
Private Property Let IInterface_Value1(strValue1 As String)
End Property
Private Property Let IInterface_Value2(strValue2 As String)
End Property
The above compiles fine if I manually change it.
rhs stands for right hand side of operator = and lhs for left hand side of =. Why is this named like this here? Maybe its something which comes from c++ conventions. By the properties you have consider this code:
Dim test As IInterface
Set test = New ClassTest
test.Value1 = "rhsVal"
The new string value is actually on the right side of the = so therefor rhs.

VB.net anonymous class with inheritance

Imagine I have class like this :
Class A
Public Function Some(str As String) As String
Return "Some " + str
End Function
End Class
I have consuming code like this :
Public Sub Foo()
Dim thisWorks = New With {.prop = "thing"}
Dim thisDoesntWork = New inherits A with { .prop = Some("thing") }
End Sub
I'm trying to create an anonymous type with inheritance so that I can use the methods within. Is this possible ?
Use case : I'm trying to create a class that has methods like Select, From etc. that would help in cleaner query construction. In the consuming code, I would just create an anonymous type inheriting from the class and use the methods.
What you want is not possible (at least not the way you describe it).
From what I think I understood ; you should try to mimic what has been donne for Linq ; an interface (like IEnumerable) with all the method you want (or maybe an abstract class bu that prevent you to inherit from something else) + something else (probably a module if you want them as extension method) defining the Select etc. acting on the interface.
From that point you can create classes which implement the interface and use your custom Select etc. on them