
Trabalhando com Constantes, Variáveis e Tipos de Dados - Avançado
VB.NET: Conversão de Valores
Sempre que utilizamos diversos tipos de dados (datatypes) temos que nos preocupar com as conversões de valores, segue algumas dicas e tabelas em relação a este assunto:
No .NET temos todos estes tipos de dados (Data Type):
Type Common language runtime type structure Nominal storage allocation Value range Boolean System.Boolean 2 bytes True or False. Byte System.Byte 1 byte 0 through 255 (unsigned). Char System.Char 2 bytes 0 through 65535 (unsigned). Date System.DateTime 8 bytes 0:00:00 on January 1, 0001 through 11:59:59 PM on December 31, 9999. Decimal System.Decimal 16 bytes 0 through +/-79,228,162,514,264,337,593,543,950,335 with no decimal point;
0 through +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest nonzero number is
+/-0.0000000000000000000000000001 (+/-1E-28).Double
(double-precision floating-point)System.Double 8 bytes -1.79769313486231570E+308 through
-4.94065645841246544E-324 for negative values; 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values.Integer System.Int32 4 bytes -2,147,483,648 through 2,147,483,647. Long
(long integer)System.Int64 8 bytes -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807. Object System.Object (class) 4 bytes Any type can be stored in a variable of type Object. Short System.Int16 2 bytes -32,768 through 32,767. Single
(single-precision floating-point)System.Single 4 bytes -3.4028235E+38 through -1.401298E-45 for negative values; 1.401298E-45 through 3.4028235E+38 for positive values. String
(variable-length)System.String (class) Depends on implementing platform 0 to approximately 2 billion Unicode characters. User-Defined Type
(structure)(inherits from System.ValueType) Depends on implementing platform Each member of the structure has a range determined by its data type and independent of the ranges of the other members.
Fonte: Msdn VS 2003Não podemos converter tudo, temos alguns tipo que não podemos converter. Não podemos por exemplo converter uma string "Olá Mundo" para integer. Temos que sempre lembrar que os tipos que vamos converter sejam de um mesmo tipo, ou seja, numeros com numeros, caracteres com caracteres e data com data, somente no caso de caracter que podemos converter praticamente todos os tipos de dados. Podemos converter um número 123 para string, por exemplo.
No caso de valores, temos que tomar uma grande cuidado, nunca podemos converter um tipo de dado Long para Integer, isto porque, o tipo Integer possue 4 Bytes e o Long 8 Bytes, assim, quando tentamos converter estes tipos de dados se o campo for maior do que 4 bytes teremos um grande problema. No caso contrário de conversão de um tipo Integer para um Long, não existe nenhum problema, ou seja nunca converter um tipo numério que a quantidade de Bytes do valor a ser convertido seja maior do que queremos converter.
Segue abaixo uma tabela com todas as possibilidades de conversões:
Data type Widens to data types Byte Byte, Short, Integer, Long, Decimal, Single, Double Short Short, Integer, Long, Decimal, Single, Double Integer Integer, Long, Decimal, Single, Double Long Long, Decimal, Single, Double Decimal Decimal, Single, Double Single Single, Double Double Double Any enumerated type Its underlying integer type and any type to which that widens Char Char, String Any type Object Any derived type Any base type from which it is derived Any type Any interface it implements Nothing Any data type or object type
Fonte: Msdn VS 2003Existem diversas funções que devemos utilizar para convertermos valores. Veja lista:
Function Data type CBool Boolean CByte Byte CChar Char CDate Date CDbl Double CDec Decimal CInt Integer CLng Long CObj Object CShort Short CSng Single CStr String
Fonte: Msdn VS 2003Exemplo:
Convertendo um valor Integer para String
Dim intValor as Integer Dim strValor as String intValor = 1234 'Converte o valor para string strValor = CStr(intValor)Convertendo um valor Double para Decimal
Dim dblValor As Double Dim decValor As Decimal dblValor = 35.29 'Converte o valor para decimal decValor = CDec(dblValor)
Dicas:
Ir para: Aula anterior | Proxima aula | Indice de Aulas
|
|
||
© 1999-2009 POWER
Informática - Rua João Manoel, 912 - Centro - Santana
do Livramento - RS
Fones: (55) 3 2 4 2 - 5 4 2 7 / (55) 9 9 9 4 - 8 7
8 2
Serviços Manutenção Hardware, programação, páginas Web