Wiki source for KeyPgVar


Show raw source

{{fbdoc item="title" value="VAR"}}----
Declares a variable whose type is implied from the initializer expression

{{fbdoc item="syntax"}}##
**Var** [[[KeyPgShared|shared]]] //symbolname// = //expression//[, //symbolname// = //expression//]
##
{{fbdoc item="desc"}}
##**Var**## declares a variable whose type is implied from the initializer expression. It is illegal to specify an explicit type in a ##**Var**## declaration. The initializer expression can be either a constant or any variable of any type.

Note: ##[[KeyPgWstring|Wstring]]## is not supported with ##**Var**##, due to the fact that there is no var-len ##[[KeyPgWstring|Wstring]]## type. This isn't likely to change, due to the complexities involved with handling Unicode.

Since the type of the variable is inferred from what you assign into it, it's helpful to know how literals work. Any literal number without a decimal point defaults to ##[[KeyPgInteger|Integer]]##. A literal number //with// a decimal point defaults to ##[[KeyPgDouble|Double]]##. See [[ProPgLiterals]] for further information.

All ##[[KeyPgZstring|Zstring]]## expressions, including string literals and dereferenced ##[[KeyPgZstring|Zstring]]## ##[[KeyPgPtr|Ptr]]##s, will be given the ##[[KeyPgString|String]]## variable type.

Explicit suffixes may be used on literal variables, to change/clarify their type. See [[ProPgLiterals|Literals]] and [[TblVarTypes|Variable Types]] for some more information about suffixes that can be used on literals.

Note: Suffixes must appear on the initializer, not on the variable. Trying to use ##**Var**## with a variable that has a suffix will throw a compile error.

{{fbdoc item="ex"}}
{{fbdoc item="filename" value="examples/manual/variable/var.bas"}}%%(freebasic)
var a = cast(byte, 0)
var b = cast(short, 0)
var c = cast(integer, 0)
var d = cast(longint, 0)
var au = cast(ubyte, 0)
var bu = cast(ushort, 0)
var cu = cast(uinteger, 0)
var du = cast(ulongint, 0)
var e = cast(single, 0.0)
var f = cast(double, 0.0)
var g = @c '' integer ptr
var h = @a '' byte ptr
var s2 = "hello" '' var-len string

var ii = 6728 '' implicit integer
var id = 6728.0 '' implicit double

print "Byte: ";len(a)
print "Short: ";len(b)
print "Integer: ";len(c)
print "Longint: ";len(d)
print "UByte: ";len(au)
print "UShort: ";len(bu)
print "UInteger: ";len(cu)
print "ULongint: ";len(du)
print "Single: ";len(e)
print "Double: ";len(f)
print "Integer Pointer: ";len(g)
print "Byte Pointer: ";len(h)
print "Variable String: ";len(s2)
print
print "Integer: ";len(ii)
print "Double: ";len(id)

sleep
%%

{{fbdoc item="diff"}}
- New to ""FreeBASIC""

{{fbdoc item="lang"}}
- Only valid in the //[[CompilerOptlang|-lang fb]]// dialect.

{{fbdoc item="see"}}
- ##[[KeyPgCommon|Common]]##
- ##[[KeyPgDim|Dim]]##
- ##[[KeyPgErase|Erase]]##
- ##[[KeyPgExtern|Extern]]##
- ##[[KeyPgLbound|LBound]]##
- ##[[KeyPgRedim|Redim]]##
- ##[[KeyPgPreserve|Preserve]]##
- ##[[KeyPgShared|Shared]]##
- ##[[KeyPgStatic|Static]]##
- ##[[KeyPgByrefVariables|Byref (variables)]]##
- ##[[KeyPgUbound|UBound]]##
- [[ProPgProcedurePointers|Pointers to Procedures]]

{{fbdoc item="back" value="CatPgVariables|Variable Declarations"}}
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode