Swap 2 objects - how?

I am trying to come up with a fairly general code that swaps 2 objects.

Both objects will be the same class at the time I call this procedure,
but they can be any descendant of TObject.

The following doesn't work.


Procedure Exchange2Objects(var AObject: TObject; var BObject: TObject);
var
  Object1: TObject;
begin
  Object1 := AObject;
  AObject := BObject;
  BObject := Object1;
end;

Is there one that does?
0
Adem
7/10/2010 2:41:58 AM
embarcadero.delphi.general 4258 articles. 0 followers. Follow

28 Replies
973 Views

Similar Articles

[PageSpeed] 3
Get it on Google Play
Get it on Apple App Store

Yes, it will work as you wrote it.

But don't forget that you just swap pointers, not the actual values of the objects.

So don't expect any other object or component using these objects to know about the swap: you just swap the addresses of the supplied parameters, not the object content.
0
Arnaud
7/10/2010 6:37:18 AM
> {quote:title=Arnaud BOUCHEZ wrote:}{quote}
> So don't expect any other object or component using these objects to know about the swap: you just swap the addresses of the supplied parameters, not the object content.

In you are not in a multithreaded app, you can try this code, which MAY copy the object content. Not fully tested, just a try....

{code}
procedure SwapObjects(var O1,O2: TObject);
var tmp: pointer;
    Len: integer;
begin
  if (O1=O2) or (O1=nil) or (O2=nil) then
    exit; // don't swap same objects or null objects
  Len := O1.InstanceSize;
  if (O1.ClassType<>O2.ClassType) or (Len<>O2.InstanceSize) then
    exit; // need same exact classes
  Getmem(tmp,Len);
  try
    Move(pointer(O1)^,tmp^,Len);
    Move(pointer(O2)^,pointer(O1)^,Len);
    Move(tmp^,pointer(O2)^,Len);
  finally
    Freemem(tmp);
  end;
end;
{code}
0
Arnaud
7/10/2010 6:51:04 AM
Code above won't work as expected, because InstanceSize contains the vmt table...

This one could work better:
{code}

procedure SwapObjects(var O1,O2: TObject);
var tmp,P1,P2: pointer;
    Len: integer;
const
    ovtVmtPtrOffs = -4;
begin
  if (O1=O2) or (O1=nil) or (O2=nil) then
    exit; // don't swap same objects or null objects
  Len := O1.InstanceSize;
  if (O1.ClassType<>O2.ClassType) or (Len<>O2.InstanceSize) then
    exit; // need same exact classes
  Getmem(tmp,Len);
  try
    P1 := pointer(integer(@O1)+ovtVmtPtrOffs); // Point to VMT
    P2 := pointer(integer(@O2)+ovtVmtPtrOffs); // Point to VMT
    Move(P1^,tmp^,Len);
    Move(P2^,P1^,Len);
    Move(tmp^,P2^,Len);
  finally
    Freemem(tmp);
  end;
end;
{code}
0
Arnaud
7/10/2010 7:11:32 AM
> In you are not in a multithreaded app, you can try this code, which MAY copy the object content. Not fully tested, just a try....
>
> {code}
> procedure SwapObjects(var O1,O2: TObject);
> var tmp: pointer;
>      Len: integer;
> begin
>    if (O1=O2) or (O1=nil) or (O2=nil) then
>      exit; // don't swap same objects or null objects
>    Len := O1.InstanceSize;
>    if (O1.ClassType<>O2.ClassType) or (Len<>O2.InstanceSize) then
>      exit; // need same exact classes
>    Getmem(tmp,Len);
>    try
>      Move(pointer(O1)^,tmp^,Len);
>      Move(pointer(O2)^,pointer(O1)^,Len);
>      Move(tmp^,pointer(O2)^,Len);
>    finally
>      Freemem(tmp);
>    end;
> end;
> {code}

This is just as good as changing the pointer addresses but takes much 
longer.

kind regards
   Mike
0
Michael
7/10/2010 7:42:05 AM
> This is just as good as changing the pointer addresses but takes much 
> longer.
> 
> kind regards
>    Mike

No it's not the same, as I said in my first post, if some external components or objects use these two objects, swaping the pointers won't be reflected, but swaping the content of the objects will work.
0
Arnaud
7/10/2010 7:53:26 AM
> No it's not the same, as I said in my first post, if some external
> components or objects use these two objects, swaping the pointers
> won't be reflected, but swaping the content of the objects will work.

When I try do something like below

var
  Panel1: TPanel;
  Panel2: TPanel;
begin
  SwapObjects(Panel1, Panel2);
end;

I get this error:

[DCC Error] E2033 Types of actual and formal var parameters must be
identical

Any idea how to get past that?
0
Adem
7/10/2010 8:18:03 AM
Adem Meda wrote:

> I am trying to come up with a fairly general code that swaps 2
> objects.
> 
> Both objects will be the same class at the time I call this procedure,
> but they can be any descendant of TObject.
> 
> The following doesn't work.
> 
> 
> Procedure Exchange2Objects(var AObject: TObject; var BObject:
> TObject); var
>   Object1: TObject;
> begin
>   Object1 := AObject;
>   AObject := BObject;
>   BObject := Object1;
> end;
> 

It does work, you just have to call it with typecasts to keep the
compiler happy:

  Exchange2Objects(TObject(Obj1), TObject(obj2));

Here is a more generic method:

Procedure SwapAnything( Var a, b; datasize: Integer );
Var
  pBuf: PByte;
Begin
  GetMem( pBuf, datasize );
  try
    Move( a, pBuf^, datasize );
    Move( b, a, datasize );
    Move( pBuf^, b, datasize );
  finally
    FreeMem( pBuf );
  end;
End;

SwapAnything( a, b, Sizeof(a));

will work regardless of the types of a and b, as long as both are of
identical types (or at least have the same size, swapping a longint and
a single would work, with somewhat dubious result). It may not always
work intuitively, especially with types which are really pointer types
in disguise, like AnsiString or objects.

Like any routine using untyped Var parameters it is inherently dangerous
and can cost you a lot of hair when used incorrectly.

If you are using Delphi 2009 and above you can also use real generics. 

type
  TSwapper<T> = class
  public
     class procedure Swap(Var A, B: T);
  end;
class procedure TSwapper<T>.Swap(Var A, B: T);
var
  Temp: T;
begin
  Temp:= A;
  A:= B;
  B:= Temp;
end;

  TSwapper<TMyClass>.Swap(obj1, obj2);

where both objects are of class TMyClass.


-- 
Peter Below (TeamB)  
Don't be a vampire (http://slash7.com/pages/vampires), 
use the newsgroup archives :
http://codenewsfast.com
http://groups.google.com
0
Peter
7/10/2010 8:32:51 AM
> [DCC Error] E2033 Types of actual and formal var parameters must be
> identical
> 
> Any idea how to get past that?

Simply remove the "var":

procedure SwapObjects(O1,O2: TObject);


-- 
Jens Gruschel
http://www.pegtop.net
0
Jens
7/10/2010 8:35:10 AM
>   if (O1.ClassType<>O2.ClassType) or (Len<>O2.InstanceSize) then
>     exit; // need same exact classes

If the classtype is the same, the instance size cannot be different :-)


-- 
Jens Gruschel
http://www.pegtop.net
0
Jens
7/10/2010 8:38:00 AM
Jens Gruschel wrote:

> > [DCC Error] E2033 Types of actual and formal var parameters must be
> > identical
> > 
> > Any idea how to get past that?
> 
> Simply remove the "var":
> 
> procedure SwapObjects(O1,O2: TObject);

Then, this error message:

"Demo.exe faulted with message 'priviledged istruction at 0x0018f8e0'."

It's pretty fatal.
0
Adem
7/10/2010 8:59:13 AM
Peter,

> Here is a more generic method:

This works, but having to remember to the class types and add sizeof()
is kind of drag.

If I forego type checking, would the following be gauaranteed to work
for all TObject descendants --I kinda think it would but would like to
hear it from you :)

Procedure Swap2Objects(Var a, b);
const
  DataSize = SizeOf(TObject);
Var
  pBuf: PByte;
Begin
  Getmem(pBuf, DataSize);
  try
    Move(a, pBuf^, DataSize);
    Move(b, a, DataSize);
    Move(pBuf^, b, DataSize);
  finally
    Freemem(pBuf);
  end;
End;

> If you are using Delphi 2009 and above you can also use real
> generics.

Thanks, but D7 is still good enough for me.

Cheers,

Adem
0
Adem
7/10/2010 9:27:31 AM
Adem Meda <adem.meda@gmail.com> wrote:
> I am trying to come up with a fairly general code that swaps 2
> objects.
> 
> Both objects will be the same class at the time I call this procedure,
> but they can be any descendant of TObject.
> 
> The following doesn't work.
> 
> 
> Procedure Exchange2Objects(var AObject: TObject; var BObject:
> TObject);
> var
>   Object1: TObject;
> begin
>   Object1 := AObject;
>   AObject := BObject;
>   BObject := Object1;
> end;
> 
> Is there one that does?

Pardon my ignorance but in what cases would you want to do this?
0
Thomas
7/10/2010 10:31:05 AM
Just try like this:
{code}
procedure SwapObjects(O1,O2: TObject);
var tmp,P1,P2: pointer;
    Len: integer;
const
    ovtVmtPtrOffs = -4;
begin
  if (O1=O2) or (O1=nil) or (O2=nil) then
    exit; // don't swap same objects or null objects
  Len := O1.InstanceSize;
  if (O1.ClassType<>O2.ClassType) or (Len<>O2.InstanceSize) then
    exit; // need same exact classes
  Getmem(tmp,Len);
  try
    P1 := pointer(integer(O1)+ovtVmtPtrOffs); // Point to VMT
    P2 := pointer(integer(O2)+ovtVmtPtrOffs); // Point to VMT
    Move(P1^,tmp^,Len);
    Move(P2^,P1^,Len);
    Move(tmp^,P2^,Len);
  finally
    Freemem(tmp);
  end;
end;
{code}
Notes:
1. The @O1 and @O2 were faulty.
2. The var in parameters was not necessary, indeed.
3. The "generic" Delphi 2009 function is not necessary: just use a pointer to TObject.
4. The version of Adem Meda  will copy only two pointers, because DataSize = SizeOf(TObject) will always be executed as DataSize := 4 since a TObject is a pointer. My version use the true instance size of the object.
5. I agree with Jens Gruschel that if the classtype is the same, the instance size should be the same: this check was just "pedagogical" ;)
0
Arnaud
7/10/2010 11:04:15 AM
> {quote:title=Jens Gruschel wrote:}{quote}
> >   if (O1.ClassType<>O2.ClassType) or (Len<>O2.InstanceSize) then
> >     exit; // need same exact classes
> 
> If the classtype is the same, the instance size cannot be different :-)

Indeed - my check was "pedagogical" ;)
0
Arnaud
7/10/2010 11:05:28 AM
This will copy only two pointers, because DataSize = SizeOf(TObject) will always be executed as DataSize := 4 since a TObject is a pointer. My version use the true instance size of the object.

See https://forums.embarcadero.com/thread.jspa?messageID=256166&#256166
0
Arnaud
7/10/2010 11:06:31 AM
Thomas Hahn wrote:

> Pardon my ignorance but in what cases would you want to do this?

As it stands, it does seem like a silly thing to do; doesn't it? :)

It does.

But, I am using this as part of a larger code --in a polymorphic
tree/graph structure.

In that class, everything (including the root node) are all identical,
or descendants from a base class.

There are times when you wish to swap a node somewhere in that
tree/graph thing with something else within the same tree/graph or some
entirely different tree/graph.

After the swap operation, I wanted to keep referring to the same
variable names --as opposed to keeping dummy local copies. It's more
convenient this way.

It will be a class procedure.

Having read what I wrote, I am not sure if it still makes sense; but,
that's the best I could explain it.
0
Adem
7/10/2010 11:48:53 AM
Arnaud BOUCHEZ wrote:

> Just try like this:

It works. Thanks.

Now, a couple more questions:

1) Is this Delphi specific? I mean, would it also work in FPC?
2) Is there anything 32-bit specific in it; would it work in 64-bit FPC?
0
Adem
7/10/2010 11:56:05 AM
> 1) Is this Delphi specific? I mean, would it also work in FPC?
I checked the objpash.inc file of the FPC 2.4 source code.
I suspect it would work with
{code}
const
    ovtVmtPtrOffs = vmtInstanceSize;
{code}
There is a comment which states that all vmt were negative value's, but are now positive, because some OS expect that.
I don't know on which FPC version these values started. Using vmtInstanceSize instead of 0 should work always... but it was not tested...

> 2) Is there anything 32-bit specific in it; would it work in 64-bit FPC?

You will have to change the integer to the PtrInt FPC 64 bits compatible type. But you'll have to define type PtrInt = integer; under Delphi.
{code}
    P1 := pointer(PtrInt(O1)+ovtVmtPtrOffs); // Point to VMT
    P2 := pointer(PtrInt(O2)+ovtVmtPtrOffs); // Point to VMT
{code}

So for a true cross-platform procedure, you may try:
{code}
procedure SwapObjects(O1,O2: TObject);
var tmp,P1,P2: pointer;
    Len: integer;
const
{$ifdef FPC}
    ovtVmtPtrOffs = vmtInstanceSize;
{$else}
    ovtVmtPtrOffs = -4;
type
  PtrInt = integer;
{$endif}
begin
  if (O1=O2) or (O1=nil) or (O2=nil) then
    exit; // don't swap same objects or null objects
  Len := O1.InstanceSize;
  if (O1.ClassType<>O2.ClassType) or (Len<>O2.InstanceSize) then
    exit; // need same exact classes
  Getmem(tmp,Len);
  try
    P1 := pointer(PtrInt(O1)+ovtVmtPtrOffs); // Point to VMT
    P2 := pointer(PtrInt(O2)+ovtVmtPtrOffs); // Point to VMT
    Move(P1^,tmp^,Len);
    Move(P2^,P1^,Len);
    Move(tmp^,P2^,Len);
  finally
    Freemem(tmp);
  end;
end;
{code}
0
Arnaud
7/10/2010 12:27:31 PM
Adem Meda <adem.meda@gmail.com> wrote:
> Thomas Hahn wrote:
> 
>> Pardon my ignorance but in what cases would you want to do this?
> 
> As it stands, it does seem like a silly thing to do; doesn't it? :)
> 
> It does.
> 
> But, I am using this as part of a larger code --in a polymorphic
> tree/graph structure.
> 
> In that class, everything (including the root node) are all identical,
> or descendants from a base class.
> 
> There are times when you wish to swap a node somewhere in that
> tree/graph thing with something else within the same tree/graph or
> some
> entirely different tree/graph.
> 
> After the swap operation, I wanted to keep referring to the same
> variable names --as opposed to keeping dummy local copies. It's more
> convenient this way.
> 
> It will be a class procedure.
> 
> Having read what I wrote, I am not sure if it still makes sense; but,
> that's the best I could explain it

I understand generally what you are saying; thank you for answering.
0
Thomas
7/10/2010 1:07:49 PM
"Peter Below" wrote
> Here is a more generic method:
> Procedure SwapAnything( Var a, b; datasize: Integer );
> Var
>  pBuf: PByte;
> Begin
>  GetMem( pBuf, datasize );
>  try
>    Move( a, pBuf^, datasize );
>    Move( b, a, datasize );
>    Move( pBuf^, b, datasize );
>  finally
>    FreeMem( pBuf );
>  end;
> End;
> ...
> Like any routine using untyped Var parameters it is inherently
> dangerous and can cost you a lot of hair when used incorrectly.

Peter, Adem, et. al,

Of you like a little more checking for programmer errors,
you might consider:

Procedure Exchange2Objects(var A, B);
var Object1: TObject;
begin
  Assert(TObject(A) is TObject(B).ClassType);
  Assert(TObject(B) is TObject(A).ClassType);
  Object1 := TObject(A);
  TObject(A)  := TObject(B);
  TObject(B)  := Object1;
end;

Rgds, JohnH

PS: Thx for example of using generics.
0
John
7/10/2010 2:11:59 PM
Instead of 
  Assert(TObject(A) is TObject(B).ClassType);
  Assert(TObject(B) is TObject(A).ClassType);
I should have coded
  Assert(TObject(A).ClassType = TObject(B).ClassType,
      'Class types are not the same.');

--JohnH
0
John
7/10/2010 3:31:09 PM
Instead of 
  Assert(TObject(A) is TObject(B).ClassType);
  Assert(TObject(B) is TObject(A).ClassType);
maybe I should have coded
  Assert(Assigned(TObject(A)),'A is not assigned.');
  Assert(Assigned(TObject(B)),'B is not assigned.');
  Assert(TObject(A).ClassType = TObject(B).ClassType,
      'Class types are not the same.');

--JohnH
0
John
7/10/2010 4:14:39 PM
Arnaud,

Thank you.

It all works to my heart's content now :)

Cheers,

Adem
0
Adem
7/10/2010 7:51:50 PM
Adem Meda wrote:

> I am trying to come up with a fairly general code that swaps 2
> objects.
> 
> Both objects will be the same class at the time I call this procedure,
> but they can be any descendant of TObject.
> 
> The following doesn't work.

It should.

But what exactly do you mean with "swap objects"? Swap the contents or
swap the references? Your code merely swaps references.
-- 
Rudy Velthuis (TeamB)        http://www.teamb.com

"I hear Glenn Hoddle has found God. That must have been one hell
 of a pass." -- Bob Davies.
0
Rudy
7/11/2010 1:10:28 AM
Arnaud BOUCHEZ wrote:

> This will copy only two pointers, because DataSize = SizeOf(TObject)
> will always be executed as DataSize := 4 since a TObject is a
> pointer. My version use the true instance size of the object.
> 
> See
> https://forums.embarcadero.com/thread.jspa?messageID=256166&#256166

The OP has never specified whether he wants to exchange the content of
the two variables (= pointers) or the content of the objects they point
to, as far as I see...

-- 
Peter Below (TeamB)  
Don't be a vampire (http://slash7.com/pages/vampires), 
use the newsgroup archives :
http://codenewsfast.com
http://groups.google.com
0
Peter
7/11/2010 7:53:38 AM
Adem Meda wrote:

> Peter,
> 
> > Here is a more generic method:
> 
> This works, but having to remember to the class types and add sizeof()
> is kind of drag.
> 
> If I forego type checking, would the following be gauaranteed to work
> for all TObject descendants --I kinda think it would but would like to
> hear it from you :)

Oh, it will work for all pointer types, including objects, but since
nothing prevents you from passing any other type to this procedure your
hair is indeed in grave danger here.

In my opinion you have your priorities wrong. You focus on reducing
typing but forget the nightmares that can result later when you go
hunting for obscure errors caused by things you did wrong and which the
compiler could not catch due to your "optimizations". You should focus
on writing safe and clear code, it may take a bit longer to create
(more typing) but that will pack back dividends later when you have to
maintain and extend the code.


-- 
Peter Below (TeamB)  
Don't be a vampire (http://slash7.com/pages/vampires), 
use the newsgroup archives :
http://codenewsfast.com
http://groups.google.com
0
Peter
7/11/2010 7:59:43 AM
Peter Below wrote:

> Adem Meda wrote:
> > This works, but having to remember to the class types and add
> > sizeof() is kind of drag.
> > 
> > If I forego type checking, would the following be gauaranteed to
> > work for all TObject descendants --I kinda think it would but would
> > like to hear it from you :)
> 
> Oh, it will work for all pointer types, including objects, but since
> nothing prevents you from passing any other type to this procedure
> your hair is indeed in grave danger here.
> 
> In my opinion you have your priorities wrong. You focus on reducing
> typing but forget the nightmares that can result later when you go
> hunting for obscure errors caused by things you did wrong and which
> the compiler could not catch due to your "optimizations". You should
> focus on writing safe and clear code, it may take a bit longer to
> create (more typing) but that will pack back dividends later when you
> have to maintain and extend the code.

The actual resultant code after taking into account
suggestions/contributions is the following.

As you can see, swapping variable names will be optional --default
being false.

While I am aware that such code is dangerous, it can also be quite
useful when I need it.

Thank you all for taking the time to helping me solve it.

Cheers,

Adem

  class procedure TMyNode.SwapNodes(ANode:: TMyNode; BNode: TMyNode;
ASwapAlsoVariableNameReferences: boolean = False);
  { SwapNodes() exchanges the position of two nodes from the
same/different tree.
    If ASwapAlsoVariableNameReferences is True, then returned ANode
will refer to old BNode (and similarly, returned BNode will refer to
old ANode).
    While powerful, usage of 'ASwapAlsoVariableNameReferences := True'
is also somewhat dangereous; you need to understand what it does.
    When working in different platforms/OSes, you might also have to
modify the code that pertains to 'ASwapAlsoVariableNameReferences :=
True' cases. }
    
    Procedure SwapReferrers(ANode: TMyNode; BNode: TMyNode);
    begin
      { Nodes referring ANode are now going to refer to BNode }
      if Assigned(ANode.FPrevSibling) then begin
        ANode.FPrevSibling.FNextSibling := BNode;
      end;

      if Assigned(ANode.FNextSibling) then begin
        ANode.FNextSibling.FPrevSibling := BNode;
      end;

      if Assigned(ANode.FParent) then begin
        if ANode.FParent.FFirstChild = ANode then begin
          ANode.FParent.FFirstChild := BNode;
        end;
        if ANode.FParent.FLastChild = ANode then begin
          ANode.FParent.FLastChild := BNode;
        end;
      end;

      { Nodes referring BNode are now going to refer to ANode }
      if Assigned(BNode.FPrevSibling) then begin
        BNode.FPrevSibling.FNextSibling := ANode;
      end;

      if Assigned(BNode.FNextSibling) then begin
        BNode.FNextSibling.FPrevSibling := ANode;
      end;

      if Assigned(BNode.FParent) then begin
        if BNode.FParent.FFirstChild = BNode then begin
          BNode.FParent.FFirstChild := ANode;
        end;
        if BNode.FParent.FLastChild = BNode then begin
          BNode.FParent.FLastChild := ANode;
        end;
      end;
    end;

  var
    Parent1: TMyNode;
    Parent2: TMyNode;
    NextSibling1: TMyNode;
    PrevSibling1: TMyNode;

    NextSibling2: TMyNode;
    PrevSibling2: TMyNode;

    PNode1: Pointer;
    PANode1: Pointer;
    PBNode1: Pointer;
    Size1: integer;
    Size2: integer;

  const
    // ovtVmtPtrOffs = vmtInstanceSize; {for FPC}
    ovtVmtPtrOffs = -4; { Delphi 32-bit }
  begin
    if (ANode = BNode) then begin
      raise Exception.Create('Nodes are the same');
    end;

    if (ANode = nil) or (BNode = nil) then begin
      raise Exception.Create('Nodes cannot be nil');
    end;

    if ASwapAlsoVariableNameReferences then begin
      Size1 := ANode.InstanceSize;
      Size2 := BNode.InstanceSize;
      if (Size1 <> Size2) then begin
        raise Exception.Create('InstanceSize of both nodes must be the
same');
      end;
    end;

    Parent1 := ANode.FParent;
    Parent2 := BNode.FParent;

    NextSibling1 := ANode.FNextSibling;
    PrevSibling1 := ANode.FPrevSibling;

    NextSibling2 := BNode.FNextSibling;
    PrevSibling2 := BNode.FPrevSibling;

    { Adjusting nodes that refer to the these two nodes }
    SwapReferrers(ANode, BNode);

    { Adjusting nodes that ANode refers to }
    ANode.FNextSibling := NextSibling2;
    ANode.FPrevSibling := PrevSibling2;

    { Adjusting nodes that BNode refers to }
    BNode.FNextSibling := NextSibling1;
    BNode.FPrevSibling := PrevSibling1;

    { Finally, swap parents of ANode with BNode }
    ANode.FParent := Parent2;
    BNode.FParent := Parent1;

    if ASwapAlsoVariableNameReferences then begin
      GetMem(PNode1, Size1);
      try
        // PANode1 := Pointer(PtrInt(ANode) + ovtVmtPtrOffs); { Point
to VMT - FPC 64bit}
        // PBNode1 := Pointer(PtrInt(BNode) + ovtVmtPtrOffs); { Point
to VMT - FPC 64bit}
        PANode1 := Pointer(integer(ANode) + ovtVmtPtrOffs); { Point to
VMT }
        PBNode1 := Pointer(integer(BNode) + ovtVmtPtrOffs); { Point to
VMT }
        Move(PANode1^, PNode1^, Size1);
        Move(PBNode1^, PANode1^, Size1);
        Move(PNode1^, PBNode1^, Size1);
      finally
        Freemem(PNode1);
      end;
    end;
  end;
0
Adem
7/11/2010 9:49:45 AM
Rudy Velthuis (TeamB) wrote:

> Adem Meda wrote:
> 
> > I am trying to come up with a fairly general code that swaps 2
> > objects.
> > 
> > Both objects will be the same class at the time I call this
> > procedure, but they can be any descendant of TObject.
> > 
> > The following doesn't work.
> 
> It should.
> 
> But what exactly do you mean with "swap objects"? Swap the contents or
> swap the references? Your code merely swaps references.

[See the code I posted under Peter's post.]

Swapping contents don't make much sense in my case: While both objects
are descendants from the same class, their contents can be too
incompatible.
0
Adem
7/11/2010 10:08:14 AM
Reply:

Similar Artilces:

objection 0.2 #2
http://www.yardley.ca/objection/ <QUOTE> objection 0.2 What is 'objection'? objection is an extension for Firefox that adds deletion of Local Shared Objects to the Option > Privacy panel. A Local Shared Object? What's that? Local Shared Objects are cookie-like data files stored on your computer by Flash movies viewed by the browser. Not every Flash movie creates a Local Shared Object file; in fact, few do. However, once a Flash movie creates a Local Shared Object file, it remains resident on your computer and the data within it can be read by the Fla...

DNN 3.2.2 Object+reference+not+set+to+an+instance+of+an+object.&content=0
Hi all,  I just installed DNN 3.2.2 on Framework 1.1 with SQL Server 2000. It installed successfully but when I tried running this error appearred. An error has occurred.An error has occurred. The url shows : http://localhost/DotNetNuke/Default.aspx?tabid=36&error=Object+reference+not+set+to+an+instance+of+an+object.&content=0 Any help is appreciated.  Thanks. Got that error on an upgrade (haven't worked out what it is yet). Was yours an upgrade or new install?Regards, Nick Free Css Skins! | nuke.nickclements.net  Did you use the source code ver...

Adding new module in dnn 2.1.2:Object reference not set to an instance of an object.
I´ve added the module NewsArticles.00.00.04.zip, using the acces to file manger in module definitions. Evererything looks to work fine(no message errors, and the defenition is added) but when I try to put the module in a tab, I get this error:Object reference not set to an instance of an object. Thanks. If you are logged in as admin/host, go to the admin tab and select the 'Log Viewer'. You should see a red field there, select that and post that error message. This should give the complete error.~ChrisNet Data Design706Horsman.us Ok, here goes: Object reference not set to an inst...

borland.public.delphi.database.general not fond #2
Hello. My problem is I am trying to connect to a newsgroup that I have configured in Outlook Express 6, under Windows XP and appears this as follows mistake : 411 Group borland.public.delphi.database.general not found. Setup: Count: newsgroups.borland.com Server: newsgroups.borland.com Protocol: NNTP Port: 119 Security (SSL): 0 Error Number: 411 Code: 800ccca5 With the newsgroup of Newsgroups.borland.com before it could enter the group borland.public.delphi.database.general, This is for all groups that begin with borland. I hope you can hel...

2 Datawindow with 2 Transaction Object
PowerBuilder 8 I have 2 transaction object each connect to separate database what i want to do is very easy but i don't know how can i do this, i want to make DATAWINDOW dw_1 to use transaction tr_1, and another DATAWINDOW dw_2 to use transaction tr_2. i can do this using SQL Statement but i don't like to write this SQL into my PowerScript -- --- Osama Diab i-o@link.net ---------------------------------------------------------------------------- ---------------------------------------------------------------------------- ----------------------------...

Object reference not set to an instance of an object. #2
Hello! I am having an recurring error with the production version of my website. Users, randomly, get the following error: Error: System.NullReferenceException: Object reference not set to an instance of an object. at ekoke.ekokeBase.InitializeComponent() at ekoke.Gen_Search_Pro.OnInit(EventArgs e) at System.Web.UI.Control.InitRecursive(Control namingContainer) at System.Web.UI.Page.ProcessRequestMain() But I can NOT reproduce the error so I do not have any clue of what might happening. The code for the ekoke.ekokeBase.InitializeComponent is the following: pr...

Object reference not set to an instance of an object. #2
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.1 if (!Page.IsPostBack) 2 { 3 txtUserID.Text = Profile.UserID; 4 ddlTheme.Items.FindByValue(Profile.MyTheme).Selected = true; // error is on this line 5 }   It's hard to say exactly what's going wrong without seeing your code. Howe...

Object reference not set to instance of an object #2
I'm getting this error and have not a clue what is causing it. Here is the code:EsiWs.EsiWs proxy = new EsiWs.EsiWs(); DataSet ds1 = new DataSet();DataSet ds2 = new DataSet(); try { //Get Oracle data ds1 = proxy.GetOraclePayments(txtGroupId.Text.ToString());ds1.Tables[0].PrimaryKey = new DataColumn[1] {ds1.Tables[0].Columns["Invoice_Num"] }; }catch (NullReferenceException ee) { Server.ClearError(); }catch (Exception ee) {ExceptionPolicy.HandleException(ee, "Log and Alert Policy");return; } try { //Get ESI data ds2 = proxy.GetESIPayments(txtG...

Object reference not set to an instance of an object. #2
How do you check the value of a cookie before the value has been set? HttpCookie cookie = Request.Cookies["MyCookie"];        if(cookie != null)            Response.Write(cookie.Value); I should add that I need it in VB Also, a simple check of the cookie produces an error....Object not set to an instance of an object. I am wondering how people get around this. VB is similiar: Dim cookie As HttpCookie = Request.Cookies("MyCookie")       ...

Object reference not set to an instance of an object #2
Since this is my first time looking at the portal and how it works and having no knowledge of how portals work, I would like some assistance in solving this dilema. Logged in as the administrator of the IBuySpy Portal, when I attempt to access a module to view its properties and editting functions, I get this "Object reference not set to an instance of an object" error. They all pertain to the following line of code that can be found on the pages that handle each module: ViewState["UrlReferrer"] = Request.UrlReferrer.ToString(); I downloaded the C# version of the p...

2 server objects share ipaddress #2
Hello, A site I am working at has 2 server objects which share an ipaddress. This occured when a linux (OES) server was deserviced and the server object not deleted before the box was replaced with a Netware 6.5 box. Both old and new are/were virtualised on SLES10. Any ideas about the best way to remove the old server object. I am woried that if I just delete it that bad things may happen. Thankyou for any assistance Regards Brian -- bdavis97 ------------------------------------------------------------------------ bdavis97, > Any ideas about the best ...

Object reference not set to an instance of an object. #2
Good Morning. I have the following code that does exactly what I need. However, is give me the error "Object reference not set to an instance of an object." when it gets to the "mail". I checked Active Driectory and an email address wasn't entered for the user. How do I trap for empty strings or Null values in Active Directory? Dim strRealName As String Dim strEmail As StringDim Entry As DirectoryEntry = New DirectoryEntry("LDAP://INTFNBA", "username", "password") Dim Searcher As DirectorySearcher = New DirectorySearcher(Entry) Sea...

Object reference not set to an instance of an object. #2
Try to debug the code for registration page using custmized createUserWizard, found out it seems something wrong with the sqldata source, but cannot figure out where is wrong. Here is the code I have, on line 7 and 18, I got error the same: as the followings: System.NullReferenceException was unhandled by user code  Message="Object reference not set to an instance of an object."  Source="App_Web_rzqkqmuc"  I guess there is something wrong with the mydatasource Initialtion. I also attach my sqldatasouce control in the aspx page below. Thank you <as...

object reference not set to an instance of an object #2
Hi Everyone, I have a simple webform with few textboxes that saves data to a sql database.  I was able to retrieve and save data successfully until i added few more dropdownlists and other text boxes.  Now, when i try to hit my Add button to send all the data in the textboxes to the database, i am getting this error: Object reference not set to an instance of an object. Line 121:        Dim myServer As New ServersDPLine 122:        Dim serverRow As Servers.SERVERINFORowLine 123:     &n...

Web resources about - Swap 2 objects - how? - embarcadero.delphi.general

Object database - Wikipedia, the free encyclopedia
When database capabilities are combined with object-oriented programming language capabilities, the result is an object-oriented database management ...

Sydney Festival 2016 review: One man's trash an absolute treasure in Object Lesson
The Object Lesson is one of those frog-boiler experiences. Once immersed, you don't realise how thoroughly you are being affected until it's ...

Residents object to mosque in Perth's south concerned about "sexual deviancy of Muslims".
Residents living near a proposed mosque in Perth's south have objected to the place of worship concerned about the &quot;violence and sexual ...

Relativity Objects To Paying Bankruptcy Consultant
Relativity Media said today that its Official Committee of Unsecured Creditors plans to file an objection in U.S. Bankruptcy Court over the fees ...

This is the first object 3D-printed from alien metal
So-called "asteroid mining" company Planetary Resources is built on the belief that asteroids and other objects in space are loaded with resources ...

Take A Trip Around The World Through 1,300 Design Objects
... Haar Romeny opened Kiosk in 2005. The SoHo boutique, which closed its brick-and-mortar location in 2014, celebrated everyday and esoteric objects ...

Prosecutors seek anonymous jury at February terrorism trial; defense objects, calls it unfair - Fox News ...
The lawyer for a Vietnamese man facing a terrorism trial in New York City is urging a judge to reject the government's demands for an anonymous ...

Vintage arcade games are once again objects of fascination and wonder
Vintage arcade games are once again objects of fascination and wonder Greenfield Daily Reporter Most of the two dozen or so games are at least ...

Sri Lanka drops plan to buy fighters from Pak after India objects
New Delhi has opposed SLAF plans to buy the J-17s on the ground that Sri Lanka does not need fighter aircraft.

This is the first object 3D printed from asteroid dust
... has made several leaps and bounds within the last few years, especially in regards to the materials that can be used to create different objects. ...

Resources last updated: 1/22/2016 8:49:01 PM