Is there anyway to do something like classes in vbscript. I ain't so good in classic ASP.
Or does anybody have a C# vbscript conversion FAQ.
My problem is that I must consume a webservice in classic ASP and the returntype is an array of a class. In asp.net with C# it's a piece of cake, because I know how to do it, but how do you do it in classic ASP?
You can, but just bear in mind that there is no inheritance.
within your class, the following are the contructor and destructors.
Class_Initialize()
Class_Terminate()
See http://msdn.microsoft.com/en-us/library/4ah5852c%28VS.85%29.aspx
I did something like this to simulate properties, but they are functions. I'm not sure how to do properties in vbscript. Help anyone?
Class Fubar
Private m_var
Public Function set_one_type(stringtype)
m_var = stringtype
End Function
Public Function get_one_type
get_one_type = m_var
End Function
Public Function myBox(strMsg)
myBox = "Hej " & strMsg
End Function
End Class
And you use it like this:
Set myFubar = new Fubar
myFubar.set_one_type("Volvo")
Response.Write(myFubar.get_one_type())
You can create classes in VBScript, much the same way you might in VB (with the obviously more limited syntax of VBScript).
Have a look at the Downloads page for the Wrox VBScript reference (which is an excellent reference, BTW). In it, you'll find source code for a complete chapter's worth of VBScript classes and examples.
Specifically, you'll want Chapter 8.
Related
I'm trying to rebuild some old QBASIC (yeah, you read that right) programs for use on more modern systems (because for some reason kids these days don't like DOS).
I understand the basic principles of classes and objects (I think) but I'm obviously missing something.
I have a number of instruments which are controlled using GPIB, using VISA COM libraries. I can make it work, but the code is very ugly :(
In order to use an instrument, I have the following in my Public Class Main:
Public ioMgr As Ivi.Visa.Interop.ResourceManager
Dim myInstrument As New Ivi.Visa.Interop.FormattedIO488
Dim myInstOpen As Boolean
Then, when I come to initializing the instrument (in the 'Initialize' button click sub), I use:
Try
myInstrument.IO = ioMgr.Open("GPIB0::17")
Catch exOpen As System.Runtime.InteropServices.COMException
myInstOpen = False
End Try
Pretty straightforward stuff; if the instrument can't be opened at address 17 on GPIB0, it throws an exception, which gets caught and sets the 'myInstOpen' flag to false.
Then, I can communicate with the instrument using commands from the Ivi.Visa.Interop.FormattedIO488 interface such as:
myInstrument.IO.ReadSTB()
result = myInstrument.ReadString()
myInstrument.WriteString("GPIB Command Here")
And all of it works.
What I want to do is, create a generic 'Instrument' class, that allows me access to all the functions from the Ivi.Visa.Interop.FormattedIO488 interface, and from the Ivi.Visa.Interop.ResourceManager interface, but also allows me to build my own class.
For instance:
Public Class GPIBInst
Implements Ivi.Visa.Interop.FormattedIO488
Public Address As Integer
Public Sub setAddress(ByVal Addr As Integer)
Address = Addr
End Sub
Public Function getAddress() As Integer
Return Address
End Function
Public Function readIO() As String
Dim Data As String = me.ReadString()
Dim Result As String = mid(Data,2,7)
Return Result
End Function
End Class
This would allow me to use the functions from the interface, but also customize the instruments for other useful things inside the program. For instance, the GPIBInst.Address needs to be used in other places, and the GPIBInst.readIO() can be used instead of just the generic ReadString() so that I can customize the output a little.
BUT when I try to do this, I can't inherit from the interface (because it's an interface) and I can't implement the interface because it says my class needs to implement every single function which the interface provides. I don't want all these functions, and also, I can't work out how to write them all into my class anyway (they have heaps of random stuff in them which I don't understand lol).
If anyone can see where I'm coming from and can offer some advice, I'd really appreciate it =)
An interface is supposed to represent a coherent set of functionality; implementing part of it but not all of it violates the intent of the concept. That being said, it is very common for APIs in object-oriented languages that wrap non-OO systems to just define one massive interface rather than breaking the functionality into logical sub-groups and defining an interface for each group. If that is your circumstance, and you want to implement the interface, you have no choice but to implement every method from the interface (although you can throw a NotImplementException for any method that you don't want to fully implement, as long as that will not prevent your class from functioning properly).
We are attempting to create a FogBugz plugin & have started naturally with the Hello World example [Wiki 38].
We are using Visual Studio 2005 and VB.Net.
However, whenever we add "Implements IPluginExtraMenus" to our class AND implement the appropriate function, Visual Studio reports that
Class 'xxxx' must implement Function ExtrasMenuLinks() as UI.CNavMenuLink()
for interface FogCreek.FogBugz.Plugins.InterfacesIPluginExtrasMenu
Here is an example:
Public Class DaysRemaining
Inherits Plugin
Inherits IPluginPagedisplay
Inherits IPluginExtrasMenu
Public Function ExtrasMenuLinks() As UI.CNavMenuLink
dim vMenu as CNavMenuLink
vMenu = new CNavMenuLink("", "", "", "")
Return vMenu
End Function
End Class
Also, if we try and add an "Implements IPluginExtrasMenu.ExtrasMenuLinks" keyword in the function definition, Visual Studio reports that
'ExtrasMenuLinks' cannot implement 'ExtrasMenuLinks' because there is
no matching function on interface
FogCreek.FogBugz.Plugins.InterfacesIPluginExtrasMenu
We are importing all of the correct namespaces etc.
Any assistance will be greatly appreciated e.g. pointing where we have gone wrong, pointing us in the direction of other VB.Net examples, etc.
It looks like Visual Studio is complaining because interface expects ExtrasMenuLinks to return an array of UI.CNavMenuLink objects, whereas your implementation only returns a single UI.CNavMenuLink.
I believe the modification you need to make to match the interface is:
Public Function ExtrasMenuLinks() As UI.CNavMenuLink()
You'll also need to modify the function body to return an array.
I'm in the process of writing a heap of tests around some custom controllers using Moq in VB. Up until now, I've not had to deal with VB Lambda shortcomings since I've only moqed properties or methods.
That is until this morning when I try also running integration tests using Cassini against my code. I had code to add headers using Response.Headers.Add. I did this so I could easily get the headers collection in unit tests using Moq(Of HttpResponseBase) and a SetupGet for Headers->NameValueCollection. Of course, the code chokes in anything other than IIS7 in Integrated Pipeline mode.
So, I changed my code to use Response.AddHeader, which means my unit tests fail. And since I'm in VB, I can' see a sane way to map the call to AddHeader to the Headers collection since Function() needs a return value in VB.
I see a few entries here about Moq and VB, but no one really has the problem of mapping Subs to something else in Moq.
Has anyone tackled this particular situation in VB using Moq?
Ugh. Why do the solutions always become apparently AFTER you post. :-)
This is ugly, but it works.
Subclass HttpResponseBase.
Mock that and set the CallBase to True.
Then override Add/AppendHeader to do Headers.Add. Now you catch any variation people use in code as they all fall into Response.Headers collection. The realy code works regardless of which method you use.
Not as clean as just Moqing Add/Append in C# with callbacks, but it does work.
Dim response As New Mock(Of CustomHttpResponse)
response.SetupGet(Function(r As HttpResponseBase) r.Headers).Returns(New NameValueCollection)
response.CallBase = True
Public Class CustomHttpResponse
Inherits HttpResponseBase
Public Overrides Sub AddHeader(ByVal name As String, ByVal value As String)
Me.Headers.Add(name, value)
End Sub
Public Overrides Sub AppendHeader(ByVal name As String, ByVal value As String)
Me.Headers.Add(name, value)
End Sub
End Class
I am working on a project that has several different codes. These codes all basically are used this way:
CodeKey
Description
GetList
GetSpecific
SetProperties
All of my classes implement this. I am hesitent, however, to use an interface because of one problem--the type Codes vary by type. Some are strings, some are integers, some are bytes. The only way I could see using an interface would be to make the typeCode an object in the interface and then cast whenever I needed to use it, but that seems a bit silly. Any ideas? This is in VB.NET.
You could use a generic interface for it as I read it.
Interface IYourType(Of T)
Property CodeKey As T
Property Description As String
Sub GetList...
Sub GetSpecific...
Sub SetProperties...
End Interface
I am not sure I understand your question completely, but your type issue is an obvious use of generics.
Good luck,
- Dan
Here is a VB.NET code snippet
Public Class OOPDemo
Private _strtString as String
Public Function Func(obj as OOPDemo) as boolean
obj._strString = "I can set value to private member using a object"
End Function
End Class
I thought we cannot access the private members using the object, but perhaps CLR allows us to do that. So that means that access modifiers are based on the type and not on the instance of that type. I have also heard that c++ also allows that..
Any guesses what could be the reason for this?
Edit:
I think this line from the msdn link given by RoBorg explains this behaviour
"Code in the type that declares a private element, including code within contained types, can access the element "
Your question is quite confusing but I think I've understood it as:
"Why can I access another instance (of my class)'s private variables?"
And you're right - in all OOP languages I've used you can access private variables from other instances, precisely because access permissions are based on where the code is, rather than to which object instance it 'belongs'.
It might be hard to implement copy constructors or equality operators otherwise.
Here's the section about access levels in MSDN.