indexing - Integer to custom type conversion in Ada -
i have array of kind of objects, indexed type index
:
type index new integer range 1..50; type table new array(index) of expression;
now, need access 1 of these expressions, depending on user entry keyboard. following:
c: character; get(c); s: string := " "; s(1) := c;
finally can cast character type integer
:
i: integer; := integer'value(s);
now, have position of value user want access, ada doesn't let access table
, because indexed index
, not integer
, different types.
what best solution, access expression based on user's input?
type index new integer range 1..50; type table new array(index) of expression;
you don't need (and can't have) new
keyword in declaration of table
.
c: character; get(c); s: string := " "; s(1) := c;
the last 2 lines can written as:
s: string := (1 => c);
(assuming c
visible , initialized @ point s
declared).
i: integer; := integer'value(s);
this not "cast". ada doesn't have casts. it's not type conversion. understand mean; if c = '4'
, s = "4"
, , integer'value(s) = 4
. (you should think if value of c
not decimal digit; cause integer'value(s)
raise constraint_error
.)
now, have position of value user want access, ada doesn't let access
table
, because indexedindex
, notinteger
, different types.
simple: don't use different types:
i: index := index'value(s);
Comments
Post a Comment