Board index » Visual Studio » Non-Type Safe Example

Non-Type Safe Example

Visual Studio380
I have read the documentation from msdn to try to understanding the concept

of "Type safe". Would someone give me an example of code segment

illustrating what is *Non* type safe?



Many Thanks,

Jeff.


-
 

Re:Non-Type Safe Example

"Jeff Chan" <jeff@macro.com.hk>wrote in message

Quote
I have read the documentation from msdn to try to understanding the concept

of "Type safe". Would someone give me an example of code segment

illustrating what is *Non* type safe?





It's hard since you can't write unsafe code in VB, and VB has always been

Type Safe. You can write terrible code with dozens of hidden

InvalidCastExceptions and NullReferenceExceptions, but you can't violate the

type system. To be Type Safe is to obey the type system, to access memory

only through the methods of declared types.



In the beginning, programs viewed their memory as an undifferentiated extent

of bytes. Then C added types and structs to give names to the bits. But in

C you can always directly access any area of memory, and the compiler will

allow almost any assignment. This direct memory access causes both

dangerous bugs and vectors for malicous code. For instance a "buffer

overrun" is a simple programming error in a non type-safe language which can

be exploited by a malicous client.



VB is verifiably Type Safe. This means that a VB program simply cannot have

a buffer overrun or a memory leak. A VB program cannot generate an access

violation, or touch any area of memory it isn't supposed to. And you can

verify all this at runtime, so you don't have to trust the programmer to use

the right compiler switches or to not make mistakes.



David





-

Re:Non-Type Safe Example

This is a multi-part message in MIME format.



------=_NextPart_000_0080_01C4FD27.55EE02B0

Content-Type: text/plain;

charset="iso-8859-1"

Content-Transfer-Encoding: quoted-printable



Hi Jeff,



Here is an example:



Dim notTypeSafeCollection As New Collection()

notTypeSafeCollection.Add("John")



notTypeSafeCollection.Add(1)



notTypeSafeCollection.Add(New Object())



Notice that I can add any object to the collection. If I decided to use =

it to store account objects I could accidently store a non-account =

object in the collection. The collection is not type safe.



and here is a link to an article on how to create a type safe =

collection:



www.devcity.net/Articles/66/1/typesafe_collection.aspx">www.devcity.net/Articles/66/1/typesafe_collection.aspx





--=20

Mike



Mike McIntyre

Visual Basic MVP

www.getdotnetcode.com



















"Jeff Chan" <jeff@macro.com.hk>wrote in message =

Quote
I have read the documentation from msdn to try to understanding the =

concept

of "Type safe". Would someone give me an example of code segment

illustrating what is *Non* type safe?

=20

Many Thanks,

Jeff.

=20



------=_NextPart_000_0080_01C4FD27.55EE02B0

Content-Type: text/html;

charset="iso-8859-1"

Content-Transfer-Encoding: quoted-printable



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML><HEAD>

<META http-equiv=3DContent-Type content=3D"text/html; =

charset=3Diso-8859-1">

<META content=3D"MSHTML 6.00.2900.2523" name=3DGENERATOR>

<STYLE></STYLE>

</HEAD>

<BODY>

<DIV><FONT face=3DArial size=3D2>Hi Jeff,</FONT></DIV>

<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>

<DIV><FONT face=3DArial size=3D2>Here is an example:</FONT></DIV>

<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>

<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp; Dim notTypeSafeCollection =

As New=20

Collection()</FONT></DIV>

<DIV>

<P><FONT face=3DArial size=3D2>&nbsp;&nbsp;=20

notTypeSafeCollection.Add("John")</FONT></P>

<P><FONT face=3DArial size=3D2>&nbsp;&nbsp; =

notTypeSafeCollection.Add(1)</FONT></P>

<P><FONT face=3DArial size=3D2>&nbsp;&nbsp; =

notTypeSafeCollection.Add(New=20

Object())</FONT></P>

<P><FONT face=3DArial size=3D2>Notice that I can add any object to the=20

collection.&nbsp; If I decided to use it to store account objects I =

could=20

accidently store a non-account object in the collection.&nbsp; The =

collection is=20

not type safe.</FONT></P>

<P><FONT face=3DArial size=3D2>and here is a link to an article on how =

to create a=20

type safe collection:</FONT></P>

<P><A =

href=3D"www.devcity.net/Articles/66/1/typesafe_collection.aspx"><F=">www.devcity.net/Articles/66/1/typesafe_collection.aspx"><F=

ONT=20

face=3DArial=20

size=3D2>www.devcity.net/Articles/66/1/typesafe_collection.aspx</F=">www.devcity.net/Articles/66/1/typesafe_collection.aspx</F=

ONT></A></P><FONT=20

face=3DArial size=3D2>

<P><BR>-- <BR>Mike</P>

<P>Mike McIntyre<BR>Visual Basic MVP<BR><A=20

href=3D"www.getdotnetcode.com">www.getdotnetcode.com</A></P>">www.getdotnetcode.com">www.getdotnetcode.com</A></P>

<P></FONT>&nbsp;</P>

<P><FONT face=3DArial size=3D2></FONT>&nbsp;</P>

<P><FONT face=3DArial size=3D2></FONT>&nbsp;</P>

<P><FONT face=3DArial size=3D2></FONT>&nbsp;</P></DIV>

<DIV><FONT face=3DArial size=3D2>"Jeff Chan" &lt;</FONT><A=20

href=3D"mailto:jeff@macro.com.hk"><FONT face=3DArial=20

size=3D2>jeff@macro.com.hk</FONT></A><FONT face=3DArial size=3D2>&gt; =

wrote in message=20

</FONT><A href=3D"news:OeuEZjT$EHA.2316@TK2MSFTNGP15.phx.gbl"><FONT =

face=3DArial=20

size=3D2>news:OeuEZjT$EHA.2316@TK2MSFTNGP15.phx.gbl</FONT></A><FONT =

face=3DArial=20

size=3D2>...</FONT></DIV><FONT face=3DArial size=3D2>&gt;I have read the =

documentation=20

from msdn to try to understanding the concept<BR>&gt; of "Type safe". =

Would=20

someone give me an example of code segment<BR>&gt; illustrating what is =

*Non*=20

type safe?<BR>&gt; <BR>&gt; Many Thanks,<BR>&gt; Jeff.<BR>&gt;=20

<BR>&gt;</FONT></BODY></HTML>



------=_NextPart_000_0080_01C4FD27.55EE02B0--



-