TypeOf Is
Hello everybody,
in VB there is a special Syntax to ask of which type an object is
the Syntax is:
VB-Code example:
in Java there is a similar syntax with the instanceof -keyword:
Maybe in future versions of Jabaco the TypeOf- or the instanceof-Syntax will be implemented, but in the meanwhile one could use a function like the following:
Jabaco-code:
and it could be used like in this Jabaco example:
other suggestions are welcome
OlimilO
in VB there is a special Syntax to ask of which type an object is
the Syntax is:
|
|
Source code |
1 2 3 4 |
'TypeOf objectname Is objecttype. 'The objectname is any object reference and objecttype is any valid object type. 'The expression is True if objectname is of the object type specified by objecttype; 'otherwise it is False. |
VB-Code example:
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Private Sub Command1_Click() Dim b As Boolean b = TypeOf Picture1 Is PictureBox If TypeOf Picture1 Is PictureBox Then MsgBox "PictureBox" End If If TypeOf Command1 Is CommandButton Then MsgBox "CommandButton" End If Dim obj As Object Set obj = Me.Label1 Select Case True Case TypeOf obj Is PictureBox MsgBox "PictureBox" Case TypeOf obj Is CommandButton MsgBox "CommandButton" Case TypeOf obj Is Label MsgBox "Label" End Select End Sub |
in Java there is a similar syntax with the instanceof -keyword:
|
|
Source code |
1 2 3 |
if ( o instanceof String )
{
}
|
Maybe in future versions of Jabaco the TypeOf- or the instanceof-Syntax will be implemented, but in the meanwhile one could use a function like the following:
Jabaco-code:
|
|
Jabaco Source |
1 2 3 4 |
Public Function TypeOf(obj As Object, cls As java#lang#String) As Boolean cls = cls.toUpperCase TypeOf = obj.getClass.toString.toUpperCase.contains(cls) End Function |
and it could be used like in this Jabaco example:
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Public Sub Command1_Click() Dim b As Boolean b = TypeOf(Picture1, "PictureBox") If TypeOf(Picture1, "picturebox") Then MsgBox "PictureBox" End If If TypeOf(Command1, "COMMANDBUTTON") Then MsgBox "CommandButton" End If Dim obj As Object Set obj = Me.Label1 Select Case True Case TypeOf(obj, "PictureBox") MsgBox "PictureBox" Case TypeOf(obj, "CommandButton") MsgBox "CommandButton" Case TypeOf(obj, "Label") MsgBox "Label" End Select End Sub |
other suggestions are welcome
OlimilO
Hi A1880
thanks for the idea but i am not quite sure how it was meant.
Should the function return false in case of a nothing-object or should it in general return an enum-constant something like:
True, Null, False
the enhanced TypeOf-function:
OlimilO
thanks for the idea but i am not quite sure how it was meant.
Should the function return false in case of a nothing-object or should it in general return an enum-constant something like:
True, Null, False
the enhanced TypeOf-function:
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 9 10 11 |
Public Function TypeOf(obj As Object, cls As java#lang#String) As Boolean 'TypeOf = obj.getClass.toString.toUpperCase.contains(cls): Exit Function If obj = Nothing Then Exit Function If cls = Nothing Then Exit Function Const deldol As java#lang#String = "$" Dim c As java#lang#String = obj.getClass.getName If c.contains(deldol) Then c = c.substring(c.indexOf(deldol) + 1) End If Typeof = cls.toUpperCase.equals(c.toUpperCase) End Function |
OlimilO
TypeOf exception handling
Hi,
VB6 TypoOf raises an error condition if nothing/null objects are interrograted for their type using the TypeOf operator.
The same happens in your original implementation.
My suggestion is to return "Nothing" as result of TypeOf(Nothing) and let the calling routine handle the situation.
I am not sure what happens if you "exit function" without assigning a return value. Therefore I would promote an explicit assignment of a return value.
Greetings!
A1880
VB6 TypoOf raises an error condition if nothing/null objects are interrograted for their type using the TypeOf operator.
The same happens in your original implementation.
My suggestion is to return "Nothing" as result of TypeOf(Nothing) and let the calling routine handle the situation.
I am not sure what happens if you "exit function" without assigning a return value. Therefore I would promote an explicit assignment of a return value.
Greetings!
A1880
Hi A1880,
Yes, i was thinking of this idea, and now i do not know if it is really a good idea. Now the TypeOf-Function returns a boolean, which can only be True or False. If you ask whether an object is nothing or null as you know you can simply do this with the '='-Operator.
maybe something like:
would be the same as:
If you think of a function like this:
we could use it like:
here you can see that if you use it this way, it is not possible to control the comparing-process as long as the name of the class must be a String. Of course the TypeOf-function is only of use at all, as long as there is no typeOf-operator (instanceof-operator).
If sometimes there is a TypeOf-operator in Jabaco, the comparing process can be done by the compiler because the ClassName then is not a String.
you can still go on and convince me
greetings
OlimilO
Quoted
My suggestion is to return "Nothing" as result of TypeOf(Nothing) and let the calling routine handle the situation
Yes, i was thinking of this idea, and now i do not know if it is really a good idea. Now the TypeOf-Function returns a boolean, which can only be True or False. If you ask whether an object is nothing or null as you know you can simply do this with the '='-Operator.
maybe something like:
|
|
Jabaco Source |
1 |
If TypeOf(myobj, "Nothing") Then dosomething |
would be the same as:
|
|
Jabaco Source |
1 2 3 |
If myobj = Nothing then dosomething 'or also If myobj <> Nothing then dosomethingwithmyobj |
If you think of a function like this:
|
|
Jabaco Source |
1 2 3 4 5 6 7 |
Function TypeOf(obj As Object) As String if obj = Nothing then TypeOf = "Nothing" Else TypeOf = obj.getClass.getName End If End Function |
we could use it like:
|
|
Jabaco Source |
1 2 3 4 5 |
Dim myobj As Object myobj = Nothing If TypeOf(myobj) = "Nothing" then 'yes 'but what about: If TypeOf(myobj) = "nothing" then 'no |
here you can see that if you use it this way, it is not possible to control the comparing-process as long as the name of the class must be a String. Of course the TypeOf-function is only of use at all, as long as there is no typeOf-operator (instanceof-operator).
If sometimes there is a TypeOf-operator in Jabaco, the comparing process can be done by the compiler because the ClassName then is not a String.
you can still go on and convince me
greetings
OlimilO
Yes, of course
for what do we have function overloading
example of usage
TypeOf-function:
greetings
OlimilO
for what do we have function overloading
example of usage
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
Public Sub Command1_Click() Dim obj As Object Dim b As Boolean Dim msg As String = "obj is " '=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ b = TypeOf(Picture1, "PictureBox") If TypeOf(Picture1, "picturebox") Then MsgBox msg & GetClassName(Picture1) End If If TypeOf(Command1, "COMMANDBUTTON") Then MsgBox msg & GetClassName(Command1) End If obj = Me.Label1 Select Case True Case TypeOf(obj, "PictureBox") MsgBox msg & "PictureBox" Case TypeOf(obj, "CommandButton") MsgBox msg & "CommandButton" Case TypeOf(obj, "Label") MsgBox msg & "Label" End Select obj = Nothing If Typeof(obj, "Nothing") Then MsgBox "obj is 'Nothing'" If TypeOf(obj, Nothing) Then MsgBox "obj is Nothing" '=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ If TypeOf(Picture1) = "PictureBox" Then MsgBox msg & GetClassName(Picture1) End If If TypeOf(Command1) = "CommandButton" Then MsgBox msg & GetClassName(Command1) End If obj = Me.Label1 Select Case TypeOf(obj) Case "PictureBox" MsgBox msg & "PictureBox" Case "CommandButton" MsgBox msg & "CommandButton" Case "Label" MsgBox msg & "Label" End Select obj = Nothing If Typeof(obj) = "Nothing" Then MsgBox "obj is 'Nothing'" End Sub |
TypeOf-function:
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
Option Explicit Public Function TypeOf(obj As Object, cls As java#lang#String) As Boolean 'TypeOf = obj.getClass.toString.toUpperCase.contains(cls): Exit Function If obj = Nothing Then If cls = Nothing Then TypeOf = True Else TypeOf = cls.toUpperCase.equals("NOTHING") End If Else If cls = Nothing Then TypeOf = False Else Dim c As java#lang#String = GetClassName(obj) Typeof = cls.toUpperCase.equals(c.toUpperCase) End If End If End Function Public Function TypeOf(obj As Object) As String If obj = Nothing Then TypeOf = "Nothing" Else TypeOf = GetClassName(obj) End If End Function Public Function GetClassName(obj As Object) As String Const deldol As java#lang#String = "$" Dim c As java#lang#String = obj.getClass.getName If c.contains(deldol) Then c = c.substring(c.indexOf(deldol) + 1) End If Getclassname = c End Function |
greetings
OlimilO
Major update of the TypeOf-function
Hello everybody,
I am wondering a little bit why there is no protest at all. Because the function was not complete until now.
Of course superclasses (base classes) and interfaces has to be detected also!
As you know each object can have one superclass and several interfaces. The superclass again can have a superclass and several interfaces as well.
With TypeOf it should be possible to check an object if it is of a superclass or interface type.
How can we get there?
We have to walk up the class hierarchy by a loop, define an object of the Class-class. (java#lang#Class) and redefine it in every step. In principle this looks like:
so here is the complete and finished TypeOf-function:
heres how I tested it:
greetings
OlimilO
I am wondering a little bit why there is no protest at all. Because the function was not complete until now.
Of course superclasses (base classes) and interfaces has to be detected also!
As you know each object can have one superclass and several interfaces. The superclass again can have a superclass and several interfaces as well.
With TypeOf it should be possible to check an object if it is of a superclass or interface type.
How can we get there?
We have to walk up the class hierarchy by a loop, define an object of the Class-class. (java#lang#Class) and redefine it in every step. In principle this looks like:
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 |
Dim cls As java#lang#Class cls = obj.getClass Do 'do something with cls '... 'get the next superclass of cls and store it in cls cls = cls.getSuperclass Loop |
so here is the complete and finished TypeOf-function:
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
Public Function TypeOf(obj As Object, clsname As java#lang#String) As Boolean Dim ret As Boolean Dim i As Integer, n As Integer If clsname = Nothing Then ret = (obj = Nothing) Else clsname = clsname.toLowerCase If obj = Nothing Then ret = clsname.equals("nothing") Else Dim clsnames() As String clsnames = GetStrArr(clsname) Dim objcls As java#lang#Class = obj.getClass Dim objclsname As java#lang#String Dim objclsnames() As String Dim oInterfaces() As java#lang#Class Do objclsname = objcls.getName.toLowerCase objclsnames = GetStrArr(objclsname) ret = IsEqualClassName(objclsnames, clsnames) If Not ret Then oInterfaces = objcls.getInterfaces n = Ubound(oInterfaces) If n >= 0 Then i = 0 Do objclsnames = GetStrArr(oInterfaces(i).getName.toLowerCase) ret = IsEqualClassName(objclsnames, clsnames) i = i + 1 Loop While (ret = False) And (i <= n) End If End If objcls = objcls.getSuperclass Loop While (ret = False) And (objclsname.equals("java.lang.object") = False) End If End If TypeOf = ret End Function Private Function IsEqualClassName(clsnames1() As String, clsnames2() As String) As Boolean Dim i As Integer = Ubound(clsnames1) Dim j As Integer = Ubound(clsnames2) Dim ret As Boolean = True Dim name1 As java#lang#String Dim name2 As java#lang#String If i >= 0 And j >= 0 Then Do name1 = clsnames1(i) name2 = clsnames2(j) ret = name1.equals(name2) i = i - 1: j = j - 1 Loop While (ret = True) And ((i >= 0) And (j >= 0)) Else ret = False End If IsEqualClassName = ret End Function Private Function GetStrArr(str As java#lang#String) As String() Const deldol As java#lang#String = "$" Const delpnt AS java#lang#String = "." Dim del As java#lang#String = " " If str.contains(delpnt) Then del = delpnt ElseIf str.contains(deldol) Then del = deldol End If GetStrArr = Split(str, del) End Function |
heres how I tested it:
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Public Sub Command2_Click() If TypeOf(Picture1, "object") Then MsgBox "Yes Picture1 is an Object" End If If Not TypeOf(Picture1, "obj") Then MsgBox "No Picture1 is not an Obj" End If If TypeOf(Picture1, "PictureBox") Then MsgBox "Yes Picture1 is a PictureBox" End If If TypeOf(Picture1, "IJabacoControl") Then MsgBox "Yes Picture1 is a IJabacoControl" End If If TypeOf(Picture1, "java.lang.Object") Then MsgBox "Yes Picture1 is a java.lang.Object" End If If TypeOf(Picture1, "VB.IJabacoControl") Then MsgBox "Yes Picture1 is a VB.IJabacoControl" End If If Not TypeOf(Picture1, "anything.IJabacoControl") Then MsgBox "No Picture1 is not a anything.IJabacoControl" End If End Sub |
greetings
OlimilO
Addendum
Of Course I know Scott Meyers:
you can find an article here: Beware of instanceof operator
and i will not gonna slap myself.
the TypeOf-function can be useful in Jabaco because of beta-version reasons!
greetings OlimilO
Quoted
"Anytime you find yourself writing code of the form "if the object is of type T1, then do something, but if it's of type T2, then do something else," slap yourself.
you can find an article here: Beware of instanceof operator
and i will not gonna slap myself.
the TypeOf-function can be useful in Jabaco because of beta-version reasons!
greetings OlimilO
Documentaion for the function IsClass can be found in the Jabaco-Wiki:
http://www.jabaco.org/wiki/Jabaco_type_c…n_(typecasting)
here is another example-code:
regards
OlimilO
http://www.jabaco.org/wiki/Jabaco_type_c…n_(typecasting)
here is another example-code:
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Public Sub Form_Load() Dim aButton As New CommandButton Dim aPicBox As New PictureBox If IsClass(aButton, CommandButton) Then MsgBox "OK" End If Dim ctrl As IJabacoControl ctrl = aButton MessInstanceOfControl ctrl ctrl = aPicBox MessInstanceOfControl ctrl End Sub Private Sub MessInstanceOfControl(actrl As IJabacoControl) Select Case True Case IsClass(actrl, CommandButton) MsgBox "ctrl is InstanceOf a CommandButton" Case IsClass(actrl, PictureBox) MsgBox "ctrl is InstanceOf a PictureBox" End Select End Sub |
regards
OlimilO
This post has been edited 1 times, last edit by "OlimilO" (Jul 24th 2010, 8:47pm)
