System.Type - structure

  • Thread starter Thread starter Qwert
  • Start date Start date
Q

Qwert

Hello,

is it correct that if a type (System.Type) is a value (.IsValueType=True)
and not primitive (.IsPrimitive=False), that type is a structure?

Thanks.
 
is it correct that if a type (System.Type) is a value (.IsValueType=True)
and not primitive (.IsPrimitive=False), that type is a structure?

Easy enough to find out.

*Every* developer should have a dummy VS.NET Solution called Hacks or Tests
or something that looks like this:

Hacks.sln
DBHacks.dbp
VBConsole.vbproj
VBWindows.vbProj
VBWebHacks.vbProj
CSConsole.csproj
JSWindows.vbProj

(you get the idea)

It took me less than five minutes to add a structure definition, test method
and modify my Sub Main() in VBConsole:
-----------------------------------------------
Module Module1

' This is the Module1.vb I got from creating a VB Console Application
' so I can experiment with stuff without having other form related
' stuff getting in the way of my attention span.

Private Structure Item
Public Field1 As String
Public Field2 As String
End Structure

Sub Main()

whatType()

Console.WriteLine(vbCrLf & "Press a key to exit.")
Console.ReadLine()

End Sub

Private Sub whatType()

Dim x As Item

x.Field1 = "This"
x.Field1 = "That"

Console.WriteLine("IsValueType = " & x.GetType().IsValueType.ToString())
Console.WriteLine("IsPrimitive = " & x.GetType().IsPrimitive.ToString())

End Sub

' Lots of other test methods for other experiment's I've done

End Module

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"
 
Mike Labosh said:
Easy enough to find out.

*Every* developer should have a dummy VS.NET Solution called Hacks
or Tests or something that looks like this:

Hacks.sln
DBHacks.dbp
VBConsole.vbproj
VBWindows.vbProj
VBWebHacks.vbProj
CSConsole.csproj
JSWindows.vbProj

(you get the idea)

It took me less than five minutes to add a structure definition,
test method and modify my Sub Main() in VBConsole:


I think "Qwert"'s question was not how to get these properties but if the
value combination IsValueType=true and isprimitive=false is the guarantee
that the type is a structure (or if there are other properties that must be
taken into account).


Armin
 
Qwert said:
Indeed, that is my question. And the answer is?

Did you already read the "Remarks" section of the 'Type.IsValueType'
documentation?
 
Qwert,
In addition to the other comments.

The way I see it if IsValueType = True & IsEnum = False you have a
Structure, as Int32 is a Structure, but an Enum is not a Structure per se.

Int32 is a special case Structure in that IsPrimitive=True, however
IsPrimitive=True does not diminish the fact that Int32 is also a structure.

Remember that Structures inherit directly from System.ValueType. Although
System.Enum inherits from System.ValueType it is a Class, as the defined
Enums themselves are the Value types, I would not consider the defined Enums
as Structures per se, as they are Enums. However! Based on the requirements
of what I am attempting to do I would consider, considering Enums as
Structures also as they are Value types!

Hope this helps
Jay

| Indeed, that is my question. And the answer is?
|
| > ...
| > I think "Qwert"'s question was not how to get these properties but if
the
| > value combination IsValueType=true and isprimitive=false is the
guarantee
| > that the type is a structure (or if there are other properties that must
| > be taken into account).
| >
| >
| > Armin
|
|
 
"Value types are those that are represented as sequences of bits; value
types are not classes or interfaces. These are referred to as "structs" in
some programming languages."

Yes, but a primitive type is a value type too but not a structure, so
checking 'IsValueType=True' does not automaticly mean that it is a
structure. So that is why I check 'IsPrimitive=False'.
But now I wonder if there are more things I should check?

'ValueType=True' can be:
Primitive type.
Structure.
Enum ('Type.IsEnum' for that)
Anything else?

Thanks.
 
Qwert,
| Yes, but a primitive type is a value type too but not a structure, so
Odd! Every thing I've read on .NET has indicated the opposite, that Int32 is
a primitive type & that Int32 is a Structure.

It sounds like you really need to decide what a "Structure" in the context
of your project is first, possibly without looking at the Type class. Then
we can take *your* definition of what a Structure is & possibly apply it to
the Type class.

Hope this helps
Jay


| "Value types are those that are represented as sequences of bits; value
| types are not classes or interfaces. These are referred to as "structs" in
| some programming languages."
|
| Yes, but a primitive type is a value type too but not a structure, so
| checking 'IsValueType=True' does not automaticly mean that it is a
| structure. So that is why I check 'IsPrimitive=False'.
| But now I wonder if there are more things I should check?
|
| 'ValueType=True' can be:
| Primitive type.
| Structure.
| Enum ('Type.IsEnum' for that)
| Anything else?
|
| Thanks.
|
|
| "Herfried K. Wagner [MVP]" <[email protected]> schreef in bericht
| | >> Indeed, that is my question. And the answer is?
| >>
| >>> I think "Qwert"'s question was not how to get these properties but if
| >>> the value combination IsValueType=true and isprimitive=false is the
| >>> guarantee that the type is a structure (or if there are other
properties
| >>> that must be taken into account).
| >
| > Did you already read the "Remarks" section of the 'Type.IsValueType'
| > documentation?
| >
| > --
| > M S Herfried K. Wagner
| > M V P <URL:http://dotnet.mvps.org/>
| > V B <URL:http://classicvb.org/petition/>
|
|
 
Qwert said:
"Value types are those that are represented as sequences of bits;
value types are not classes or interfaces. These are referred to as
"structs" in some programming languages."

Yes, but a primitive type is a value type too but not a structure,
so checking 'IsValueType=True' does not automaticly mean that it is
a structure. So that is why I check 'IsPrimitive=False'.
But now I wonder if there are more things I should check?

'ValueType=True' can be:
Primitive type.
Structure.
Enum ('Type.IsEnum' for that)
Anything else?


No answer, but maybe helpful: Have a look at "Partition I Architecture.doc"
in the directory "Microsoft Visual Studio .NET 2003\SDK\v1.1\Tool Developers
Guide\docs". On page 31, there is a picture about the type system.

Armin
 
| Yes, but a primitive type is a value type too but not a structure, so
Odd! Every thing I've read on .NET has indicated the opposite, that Int32
is
a primitive type & that Int32 is a Structure.

Aha, yes I did not know that (allthought indeed it clearly says so).

The documentation says:
"A structure is a generalization of the user-defined type (UDT) supported by
previous versions of Visual Basic."
Reading this made me to believe that primitive types are not structures.

Anyways, this means my question is wrong, it should have been (I think):

"is it correct that if a type (System.Type) is a value (.IsValueType=True)
and not primitive (.IsPrimitive=False) and not a enum (.IsEnum=False), that
type is a composite data type?"
It sounds like you really need to decide what a "Structure" in the context
of your project is first, possibly without looking at the Type class. Then
we can take *your* definition of what a Structure is & possibly apply it
to
the Type class.

What I need to do is get and store the values of all public properties of
any class or structure..euhm...I mean composite data type, which is no
problem. Somewhere in the process my question appeared.
Hope this helps
Jay

It does. Thanks everyone.
 
Qwert,
| What I need to do is get and store the values of all public properties of
| any class or structure..euhm...I mean composite data type, which is no
| problem. Somewhere in the process my question appeared.
You can use Type.GetProperties to get the list of public properties
(System.Reflection.PropertyInfo), then use PropertyInfo.GetValue &
PropertyInfo.SetValue to get or set the value of said property.

Something like:

Imports System.Reflection

Dim something As SomeType

Dim properties() As PropertyInfo =
something.GetType().GetProperties()

For Each pi As PropertyInfo In properties
Debug.WriteLine(pi.GetValue(something, Nothing))
Next

For Each pi As PropertyInfo In properties
dim value as Object = ...
pi.SetValue(something, value, Nothing)
Next



Alternatively you can use System.ComponentModel.PropertyDescriptor in a
similar manor, something like:

Imports System.ComponentModel

Dim something As SomeType

Dim properties As PropertyDescriptorCollection =
TypeDescriptor.GetProperties(something)

For Each pd As PropertyDescriptor In properties
Debug.WriteLine(pd.GetValue(something))
Next

For Each pd As PropertyDescriptor In properties
dim value as Object = ...
pd.SetValue(something, value)
Next

Seeing as Primitives & Enums don't have public instance properties, the
above code will not return any properties...

Hope this helps
Jay

|> | Yes, but a primitive type is a value type too but not a structure, so
| > Odd! Every thing I've read on .NET has indicated the opposite, that
Int32
| > is
| > a primitive type & that Int32 is a Structure.
|
| Aha, yes I did not know that (allthought indeed it clearly says so).
|
| The documentation says:
| "A structure is a generalization of the user-defined type (UDT) supported
by
| previous versions of Visual Basic."
| Reading this made me to believe that primitive types are not structures.
|
| Anyways, this means my question is wrong, it should have been (I think):
|
| "is it correct that if a type (System.Type) is a value (.IsValueType=True)
| and not primitive (.IsPrimitive=False) and not a enum (.IsEnum=False),
that
| type is a composite data type?"
|
| > It sounds like you really need to decide what a "Structure" in the
context
| > of your project is first, possibly without looking at the Type class.
Then
| > we can take *your* definition of what a Structure is & possibly apply it
| > to
| > the Type class.
|
| What I need to do is get and store the values of all public properties of
| any class or structure..euhm...I mean composite data type, which is no
| problem. Somewhere in the process my question appeared.
|
| > Hope this helps
| > Jay
|
| It does. Thanks everyone.
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top