C --->Sizeof (Structure) ... the Equivalent in VB for Sizeof(MyType)???

Hello there ...
In C you can get the sizeof a Structure ( A Type in Visual Basic)

How can I get the Sizeof a Type in Visual Basic ??
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OHDev2004Author Commented:
Im Calling A Function from a C DLL ... so one of the Parameters needs to get the size of a Structure (In our case a Type in VB)

So let's say I have the Following Type:

Public Type MyType1
 Name as string
 Age As Integer
 Email as string
End Type

then when i call the Function it should be called like:

Dim Ret as Long
Dim NewPerson as MyType1
Dim ID as Integer,Buffer as Long

Buffer = [ Here i should get the size of the Structure    'in C it was SizeOf(MyType1)]
Ret= MyFunction(MyType1,Buffersize,ID)

PS:Sorry for the Low Points :(
Thank you ...

LenB(NewPerson) to return the size in bytes of the structure

first write customer no.5's  name to Structure:
BufferLength = writeMyCustomer(Name,Age,eMail,id)

then print his Data (Name,Age,eMail,id) to Form1:
Call Print_DATA_of_user(5)

Try this code:

Private Type MyType1
      Name As String * 19
      Age As Integer
      eMail As String * 19
      id As Integer 'user identity number as integer
End Type
Dim Ret As Integer '   2 Bytes
Dim NewPerson(0 To 100) As MyType1, NewPersonData As MyType1
Private Sub Form_Activate(): AutoRedraw = -1: DoEvents
Move 0, 0: FontSize = 16
'NewPerson.Age = 23 '2 Byte Integer
'NewPerson.eMail = "user@xyz.com'String *19  ---> 123456789@123456789"
'NewPerson.ID = 999 '   Integer
'NewPerson.Name = "Bill Gates" ' String *19  ---> 123456789 123456789

'ID = 5
Ret = writeMyCustomer("Bill Gates", 23, "user@xyz.com", 5)
Print "Buffer Length  :  "; Ret: Print

Call Print_DATA_of_user(5)
End Sub
Private Sub Print_DATA_of_user(ByVal id As Integer)
Print "Id : "; id
Print "Age : "; NewPerson(id).Age
Print "Mail : "; NewPerson(id).eMail
Print "Name : "; NewPerson(id).Name
End Sub
Private Function writeMyCustomer(ByVal custName As String, _
                                 ByVal Age2 As Integer, _
                                 ByVal eMail2 As String, _
                                 ByVal id2 As Integer _
                                 ) As Integer
NewPerson(id2).Age = Age2
NewPerson(id2).eMail = eMail2
NewPerson(id2).Name = custName
writeMyCustomer = Len(NewPersonData)
End Function
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

OHDev2004Author Commented:
Thank you two for Commenting ...

I want to get the size of the Type it self  not of the Variable ...

Thank you :)
OHDev2004Author Commented:
and by the way ...
when i test ... I have a complicated Type ...
and when I get it using Len or LenB i get a 28 which is Impossible ... because it's A Large Type and it even has a Member Called Heap which is declared as Heap(1024) As Byte ...

So the Returned Size is not valid ...
Hi OHDev:

VB does not have a direct equivalent of SizeOf, instead you must use Len() or LenB() on a variable of the type you are interested in.

So if you declared the type as:

Private Type MyType1
    Name As String * 80
    Age As Integer
    Email As String * 60
End Type

Then Len(NewPerson) = 142 '80 characters + 2 bytes Integer + 60 characters
and LenB(NewPerson) = 282 '160 bytes in an 80 char string + 2 bytes Integer + 120 bytes in a 60 char string

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Can you post the Type here?
>>> I want to get the size of the Type it self  LenB(MyType1) Not LenB(NewPerson)

OHDev, that's not possible in VB. You must declare a type X as "MyType1",
ant then you can ask, how long X is .

OHDev2004Author Commented:
Public Enum ConnectionID_Device_t
End Enum

Public Type ConnectionID_t
 CallID As Long
 DeviceID As String * 64
 DevIDType As ConnectionID_Device_t
End Type

Public Type CSTAMakeCallConfEvent_t
 newCall As ConnectionID_t
End Type
Public Type CSTAConfirmationEvent
 invokeID As Long
 makeCall As CSTAMakeCallConfEvent_t
End Type
Public Type ACSEventHeader_t
 acsHandle As Long
 eventClass As Integer
 eventType As Integer
End Type
Public Type CSTAEvent_t
 eventHeader As ACSEventHeader_t
 cstaConfirmation As CSTAConfirmationEvent
 heap(1024) As Byte
End Type

Then I declare a Variable as Dim MyVariable as CSTAEvent_t

OHDev2004Author Commented:
By the way it returns 1170 ...

I have a Small Question ...

I've Converted the Following from C:
typedef char            DeviceID_t[64];
typedef struct ConnectionID_t {
    long            callID;
    DeviceID_t      deviceID;
    ConnectionID_Device_t devIDType;
} ConnectionID_t;

is this :

Public Type ConnectionID_t
 CallID As Long
 DeviceID As String * 64
 DevIDType As ConnectionID_Device_t
End Type

considering the DeviceID part did i convert it right ??
OHDev2004Author Commented:
Ok :) I fixed the Bug :)

i changed it to  DeviceID (64) as Byte

and All was Cool :)

Since Erick37's Answer was the good one because I needed the Binary Length so He will get the Points :)
Thank you too very Much :)
Again Erick ur a Life saver :P
Hi OHDev,

Thanks for the points, glad it worked.

One note regarding arrays:

In C
means 64 elements, 0 to 63

DeviceID(64) as Byte
means 65 elements, 0 to 64

So to duplicate the structure exactly you might want to declare DeviceID(63) so that it has 64 elements as does the C prototype.
OHDev2004Author Commented:
:) Thank you very Much for Pointing me to the right :) ...
But Im glad it worked anyway :)

Working with Old C DLLs is really really Discusting :(....LOL
thank you again ...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.