ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA.convertFromJava("instanceof")=? (https://www.excelbanter.com/excel-programming/392125-vba-convertfromjava-instanceof-%3D.html)

vivmaha

VBA.convertFromJava("instanceof")=?
 
Hi,

In java, I use the instanceof keyword to check if an object is a certain
type of class.

So you can do things like:
if (s instance of string) then output("String!!!") else output("Needs a
string")

Whats the equivalent in VBA?

Thanks.

Rick Rothstein \(MVP - VB\)

VBA.convertFromJava("instanceof")=?
 
In java, I use the instanceof keyword to check if an object is a certain
type of class.

So you can do things like:
if (s instance of string) then output("String!!!") else output("Needs a
string")

Whats the equivalent in VBA?


I don't know Java, but I think you are looking for either the VarType or
TypeName function (they are similar, but one outputs a number representing
the data type and the other outputs the data type name as a String value).

Rick


Chip Pearson

VBA.convertFromJava("instanceof")=?
 
You can use TypeName to get the name of the variable type. E.g.,

Dim V As String
Debug.Print "TypeName: " & TypeName(V)

For object type variables only (not intrinsic variables), you can use the
TypeOf operator to get the type (as long as the object is not Nothing):

Dim V As Range
Set V = Range("A1")
Debug.Print "TypeName: " & TypeName(V)
Debug.Print "TypeOf Is Range: " & TypeOf V Is Range

When working with object variables, TypeOf is better than TypeName because
you can prefix a typelib to the object type to ensure that you have the
right object type. For example, both the Excel and the MSForms libraries
have an object named CheckBox. With TypeName, you will get only the string
"CheckBox" as the result. You don't know whether it is a Excel CheckBox or
an MSForms CheckBox. With TypeOf, you can specifiy the typelib. E.g.,

If TypeOf V Is MSForms.CheckBox Then
' do something


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)








"vivmaha" wrote in message
...
Hi,

In java, I use the instanceof keyword to check if an object is a certain
type of class.

So you can do things like:
if (s instance of string) then output("String!!!") else output("Needs a
string")

Whats the equivalent in VBA?

Thanks.



vivmaha

VBA.convertFromJava("instanceof")=?
 
Cool.

Thanks man.

"Chip Pearson" wrote:

You can use TypeName to get the name of the variable type. E.g.,

Dim V As String
Debug.Print "TypeName: " & TypeName(V)

For object type variables only (not intrinsic variables), you can use the
TypeOf operator to get the type (as long as the object is not Nothing):

Dim V As Range
Set V = Range("A1")
Debug.Print "TypeName: " & TypeName(V)
Debug.Print "TypeOf Is Range: " & TypeOf V Is Range

When working with object variables, TypeOf is better than TypeName because
you can prefix a typelib to the object type to ensure that you have the
right object type. For example, both the Excel and the MSForms libraries
have an object named CheckBox. With TypeName, you will get only the string
"CheckBox" as the result. You don't know whether it is a Excel CheckBox or
an MSForms CheckBox. With TypeOf, you can specifiy the typelib. E.g.,

If TypeOf V Is MSForms.CheckBox Then
' do something


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)








"vivmaha" wrote in message
...
Hi,

In java, I use the instanceof keyword to check if an object is a certain
type of class.

So you can do things like:
if (s instance of string) then output("String!!!") else output("Needs a
string")

Whats the equivalent in VBA?

Thanks.



vivmaha

VBA.convertFromJava("instanceof")=?
 
This doesnt work for user defined types.

Chip Pearson's typeOf does.

Thanks though.


"Rick Rothstein (MVP - VB)" wrote:

In java, I use the instanceof keyword to check if an object is a certain
type of class.

So you can do things like:
if (s instance of string) then output("String!!!") else output("Needs a
string")

Whats the equivalent in VBA?


I don't know Java, but I think you are looking for either the VarType or
TypeName function (they are similar, but one outputs a number representing
the data type and the other outputs the data type name as a String value).

Rick




All times are GMT +1. The time now is 05:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com