Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error when translating from visual basic to delphi

Status
Not open for further replies.

binbyte

Technical User
Nov 24, 2004
11
AR
Hi everybody!
I have a problem when translating a piece of code from a visual basic program to another in delphi:
I have in vb the following:
i = 1
do while (i < n)
if Vect1(i) = Vect1(i + 1) then
For j = 1 to n
a(i + 1 , j) = a(i + 1 , j) + a(i , j)
a(i , j) = 0
next
end if
i= i + 1
loop

and in delphi

i := 1;
while (i < n) = true do
if Vect1^ = Vect1^[i + 1] then
Begin
For j := 1 to n do
Begin
a^[i + 1 , j] := a^[i + 1 , j] + a^[i , j];
a^[i , j] := 0;
end;
end;
Inc(i);
end;

Vect is a pointer to string and a is a pointer to integer, if it's true then it execute the code.
There's no problem in visual basic but in delphi it's wrong. I don't know why.

Please excuse me for my bad english and thanks if anyone help me
 
How did you define vect1 and a ?

The correct sintax is

vect1 and a[i,j]

Steven
 
Vect1 and a definition are the following:

UNIT Util;

INTERFACE

TYPE
Float = Double;

CONST
TNArraySize = 500; { Maximum size of matrix }

TYPE
PTNV = ^TNvector;
PTNM = ^TNmatrix;
PTSV = ^TSVector;
PTSM = ^TNSmatrix;
TSvector = ARRAY[1..TNArraySize] OF string;
TNvector = ARRAY[1..TNArraySize] OF integer;
TNmatrix = ARRAY[1..TNArraySize] OF TNvector;

VAR

a : PTNM;
SVect1 : PTSV;
 
The pointer annotation is an old technology used in Turbo Pascal. It forced the programmer to do dynamic allocation of memory and other stuff related to hardware. In times when hardware was expensive ,memory scarce and computing time costly (I started with a 8088, 640K ram, 2 floppies of 360K and no hard disk) the programmer had to know something about electronics (was supposed to have an electrical engineering degree) when programs were getting a little big.
This type of programming has been substituted by object oriented programming, (at start-up the program doesn't need to tell to the compiler what he needs to do the job).

In traditional TP, you had to declare what resources where needed, thus the ^pointer annotation was a workaround.

My suggestion:
TYPE
TSvector = ARRAY[1..TNArraySize] OF string;
TNvector = ARRAY[1..TNArraySize] OF integer;
TNmatrix = ARRAY[1..TNArraySize] OF TNvector;

VAR
a : TSVECTOR; etc..

Else you must make use of commands like GetMem, FreeMem, New, Dispose and others which are already replaced in Delphi. I think that the only survivor of this dos era is Nil

Steven
 
small language enhancement

Code:
i := 1;
  [b]while (i < n) do[/b]
    if Vect1[i] = Vect1[i + 1] then
      Begin
      For j := 1 to n do
        Begin
        [b]Inc(a[i + 1 , j], a[i , j]);[/b]
        a[i , j] := 0;
        end;
      end;
    Inc(i);
  end;

cheers

--------------------------------------
What You See Is What You Get
 
to whosrdaddy:
I think it's a infinite loop. can i use the following?

i := 1;
repeat
if Vect1 = Vect1[i + 1] then
Begin
For j := 1 to n do
Begin
Inc(a[i + 1 , j], a[i , j]);
a[i , j] := 0;
end;
end;
Inc(i);
Until (i>= n);

to svanels:
what i have to use instead of New() Dispose();?
I learnt something from pascal using turbo Pascal 5.




 
why an infinite loop??

it all depends on the initial value of n offcourse.
please do remind that in pascal, you need to initialize your variables first (vb did thid for you)

so if I have this

Code:
procedure someproc;

var b: boolean;

begin
 if b then
  begin
   showmessage('uncertain');
  end;
end;

you'll see that b wasn't initialized, so you don't know what value it has (sometimes, it will be false, sometime it will be true, this depends what the memory location of the boolean

--------------------------------------
What You See Is What You Get
 
You don't need to use New and Dispose (they are required when using pointers ^. The errors you received had probably something to do with allocation of memory. Using variables before they are declared.

Steven
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top