Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

TIPUS DE DADES I VARIABLES

TIPUS DE DADES
Visual Basic Common Nominal Value range
type language storage
runtime type allocation
structure
[system.*]
Boolean Boolean Depends on True or False
implementing
platform
Byte Byte 1 byte 0 through 255 (unsigned)
SByte SByte 1 byte -128 through 127 (signed)
Char (single Char 2 bytes 0 through 65535 (unsigned)
character)
Short (short Int16 2 bytes -32,768 through 32,767 (signed)
SENCERS
integer)
Integer Int32 4 bytes -2,147,483,648 through 2,147,483,647 (signed)
Long (long Int64 8 bytes -9,223,372,036,854,775,808 through
integer) 9,223,372,036,854,775,807 (9.2...E+18 †) (signed)
UInteger UInt32 4 bytes 0 through 4,294,967,295 (unsigned)
ULong UInt64 8 bytes 0 through 18,446,744,073,709,551,615 (1.8...E+19 †)
(unsigned)
UShort UInt16 2 bytes 0 through 65,535 (unsigned)
Single Single 4 bytes -3.4028235E+38 through -1.401298E-45 † for negative
(single- values;
precision 1.401298E-45 through 3.4028235E+38 † for positive
floating- values
point)
Double Double 8 bytes -1.79769313486231570E+308 through -
(double- 4.94065645841246544E-324 † for negative values;
precision 4.94065645841246544E-324 through
REALS
floating- 1.79769313486231570E+308 † for positive values
point)
Decimal Decimal 16 bytes 0 through +/-79,228,162,514,264,337,593,543,950,335
(+/-7.9...E+28) † 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) †
String String (class) Depends on 0 to approximately 2 billion Unicode characters
(variable- implementing
length) platform
Date DateTime 8 bytes 0:00:00 (midnight) on January 1, 0001 through 11:59:59
ALTRES PM on December 31, 9999
Object Object (class) 4 bytes on 32- Any type can be stored in a variable of type Object
bit platform
8 bytes on 64-
bit platform

DECLARACIÓ DE VARIABLES
Dim aprovat as Boolean = True
Dim valor as Integer = 23
Dim nom as String = “Pepperoni”
Dim preu as Decimal = 223.32
Dim lletra as Char = “a”c ' ATENCIÓ: La „c‟ al darrera no és un error tipogràfic
„ ens permet diferenciar-ho d‟una cadena normal

INICIALITZACIÓ DE DATES
' dates en format mes/dia/any
Dim dtDate1 As Date = #12/31/2007#
Dim dtDate2 As New Date(2007, 12, 31)

' data i hora en format „mes/dia/any hora:minut:segon AM/PM


Dim dtDate3 As Date = #12/31/2007 3:01:59 AM#
Dim dtDate4 As New Date(2007, 2, 3, 3, 1, 59)

' carregar la data actual


Dim avui As Date = DateTime.Now
LECTURA I ESCRIPTURA AMB LA CONSOLA
Classe Console
Console.WriteLine("Hola Món”)
Console.WriteLine(...)
Console.WriteLine("Nom:{0} Edat:{1} Deute:{2}", nom, edat, deute)
Dim ciutat as String
Console.ReadLine(...)
ciutat = Console.ReadLine()
Console.ReadKey() „ llegir tecla sense recollir resultat

Console.ReadKey() Dim teclaPremuda As ConsoleKeyInfo


teclaPremuda = Console.ReadKey()„ llegir tecla
Console.WriteLine("la tecla apretada és {0}", teclaPremuda.KeyChar())

CONVERSIONS DE TIPUS
Conversió de Cadena a d’altres tipus sense control d’errors:
Cadena a Boolean b = Boolean.Parse(“True”)
Cadena a Integer i = Integer.Parse(“1412”)
Cadena a Double d = Double.Parse(“141,2”)
per altres tipus simplement heu de canviar la classe

Conversió de Cadena a d’altres tipus amb control d’error:


Cadena a Integer Dim conversioPossible as Boolean
Dim i as Integer
conversioPossible = Integer.TryParse(“412”, i )
per altres tipus simplement heu de canviar la classe

Qualsevol tipus a Cadena


El que sigui a cadena Dim cadena as String
cadena = variableDelTipusQueSigui.toString()

Conversions de tipus Numèrics


Widening [ de tipus més El Widening és automàtic i no cal fer res especial. Assigneu la variable directament.
petit a més gran ]
Narrowing [ de tipus més El Narrowing és automàtic si no activem OPTION STRICT.
gran a més petit ]
Sense control d’error ( pot Si activeu OPTION STRICT cal fer conversió explicita. Per les conversions tenim dues vies:
petar ! ) a) Usar les funcions de VB.6.0 ( que s’anomenen “C<tipus de dades>”, p.e. CByte, CInt, etc. )
b) Uar la classe Convert de VB.2008

Dim vinteger As Integer


Dim vbyte As Byte
vbyte = CByte(vinteger) ' opció a.1) VB6.0
vbyte = CType(vinteger, Byte) ' opció a.2) VB6.0
vbyte = Convert.ToByte(vinteger)' opció b) VB.2008

Narrowing am control Usem qualsevol de les tres estructures anteriors, dins d’un bloc try
d’error
Try
' INTENTEM (try) fer la conversió
vbyte = Convert.ToByte(vinteger)
Catch ex As Exception
' Si la convérsió dona error, s'executarà això
vbyte = 0
Console.WriteLine(" Pallús, això no cap aquí!")
End Try

Reals a sencers (truncat i  L’assignació directe de tipus reals a sencers provoca ARRODONIMENT autmàtic.
arrodoniment)  Si voleu TRUNCAR ( treure els decimals ), useu Math.Truncate (...)
vinteger = Math.Truncate(123.12)

Guia Ràpida I | Tipus de dades i Conversions 2


CADENES
Tractament bàsic de cadenes

Operador & :
Concatenació de cadenes
paraula1 = paraula1 & paraula2 & “; Hola.”

Constant vbCrLf :
Salt de línia
paraula1 = "Hola" & vbCrLf

Longitud de cadena longitud = cadena.Length

Majúscules cadena = cadena.ToUpper() 'ABC

Minúscules cadena = cadena.ToLower() 'abc

Subcadena (opció 1) cadenaB = cadenaA.Substring(3) ' a partir del caràcter 3 fins al final

Subcadena (opció 2) cadenaB = cadenaA.Substring(2, 4) 'a partir del caràcter 2, 4 caràcters endavant

Treure espais cadena = cadena.Trim()

“Muntatge de cadenes” cadena = String.Format("Cadena composta -{0}-, '{1}', <{2}>", 12, True, 45.33)

Posició d’un caràcter dins


i = cadena.IndexOf("c")
d’una cadena

Dim c As Char
Agafar un caràcter de la
cadena en una posició en Dim cadena As String = "abcdefg"
concret c = cadena.Chars(3)

Guia Ràpida I | Tipus de dades i Conversions 3

You might also like