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. |
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 |
Source code |
|
1 2 3 |
if ( o instanceof String ) { } |
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 |
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 |
Intermediate
Date of registration: Jan 1st 2009
Location: Hanover, Germany
Occupation: Software Engineer
Hobbies: Hilbert Curves
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 |
Intermediate
Date of registration: Jan 1st 2009
Location: Hanover, Germany
Occupation: Software Engineer
Hobbies: Hilbert Curves
Quoted
My suggestion is to return "Nothing" as result of TypeOf(Nothing) and let the calling routine handle the situation
Jabaco Source |
|
1 |
If TypeOf(myobj, "Nothing") Then dosomething |
Jabaco Source |
|
1 2 3 |
If myobj = Nothing then dosomething 'or also If myobj <> Nothing then dosomethingwithmyobj |
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 |
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 |
Intermediate
Date of registration: Jan 1st 2009
Location: Hanover, Germany
Occupation: Software Engineer
Hobbies: Hilbert Curves
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 |
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 |
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 |
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 |
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 |
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.
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 |
This post has been edited 1 times, last edit by "OlimilO" (Jul 24th 2010, 8:47pm)