Solved

Just ONE

Posted on 2002-06-17
26
141 Views
Last Modified: 2010-04-04
I wish to be sure that I can't add more than a single of my components. If I add two of same components my project can crash.

How verify if I have already a component in my project ?

I WISH TO DO THAT IN MY COMPONENT AND NOT IN PROJECT!
0
Comment
Question by:ginsonic
  • 10
  • 8
  • 6
  • +2
26 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7083542
hmm,
best would be to do this in the constructor
there you could iterate through the compoentlist,
and if you find there the same componentclass,
which is not self, then you could abort the constructor-method

just a way, how would i try it first

maybe the findcomponent-method is a faster way

meikl ;-)

0
 
LVL 9

Author Comment

by:ginsonic
ID: 7083563
Hi meikl ,
Have a problem with previous accepted question . Can you take a look ? I put there the description.
Thanks in advance!
0
 
LVL 9

Author Comment

by:ginsonic
ID: 7083564
And for this , can you give me a sample :)
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 7083566
another way would be to supply a local
(in the implementation-part) variable like

var
  one_Created : Boolean = False;

constructor TMyClass.Create(AOwner : TComponent);
begin
  if not one_Created then
  begin
    inherited create(AOwner);
    one_Created := True;
  end
  else
  begin
    Showmessage('Only one Creation per Project allowed');
    abort;
  end;
end;

destructor TMyClass;
begin
  one_Created := False;
  inherited destroy;
end;


just from head

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7083569
oops, didn't see your post

samples follows this evening, if needed
(no delphi on hand yet)

meikl ;-)
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7083595
I would throw an exception in the constructor.

constructor TMyClass.Create(AOwner : TComponent);
begin
  inherited create(AOwner);
 
  if one_Created then
    raise Exception.Create('only one');
 
  one_Created := True;
  // rest of Create
end;

The only thing to watch is that an exception in the constructor triggers the destructor which means that the destructor should be able to handle the situation that "rest of Create" has not yet been executed.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7083614
? whats the benefit, robert (richard) :-))
0
 
LVL 9

Author Comment

by:ginsonic
ID: 7083617
Isn't a good idea. For this case when I try to add a second the code work perfect but at destroy my variable is False again so the 3th try will create a new component .
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7083636
? last comment pointed to whom, nick
0
 
LVL 3

Expert Comment

by:raidos
ID: 7083655
Hmm...well...add a private boolean variable in your class and in the constructor set the variable to True if (Self) is supposed to exist and false otherwise...

then in the destructor
set one_Created to false only if the private variable is True...would this fix it or am i missing something.. =)

Regards
//raidos
0
 
LVL 9

Author Comment

by:ginsonic
ID: 7083686
I think that the bettter idea is to iterate through the components list.

Meikl, the other question is more important alike this . If you wish I can send you the code for my component to test it and to see where is my problem.

If you remember some time ago I was interested about a mouse animated movement . I put all codes into one component. Added some loopings , too :) . Now work perfect . But I wish to receive a done notification to can make another move. So I get the specified problem .

I need it to create a visual help for my programs .
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7083744
well,
i guess it will be better for me to understand,
if you send me, what you have (info@meikl.de)

but i can look at this in ~3 hours,
when i came home

meikl ;-)
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7083780
Throwing an exception works for me. I use it in my HID component.
The benefit is that the IDE shows the exception when dropping a second component and does not create it.
0
 
LVL 9

Author Comment

by:ginsonic
ID: 7083781
Send it.
And I appreciate your help. I really need it soon.

Nick

P.S. New 100 points for this .
0
 
LVL 9

Author Comment

by:ginsonic
ID: 7085119
Hey guys,
Nothing for me ?

Meykl,
Thanks for the other problem. Take a look for your points :)
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7125285
The Asker has requested that this question be deleted. If no Expert objects, the deletion will occur in five (5) days.

Netminder
CS Moderator
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7125432
I object.
Several working solutions have been proposed.
Mine works for me.
0
 
LVL 9

Author Comment

by:ginsonic
ID: 7125868
robert_marquardt I tested your solution ( and kretzschmar too :) ) . Read my comment :

'Isn't a good idea. For this case when I try to add a second the code work perfect but at destroy my variable is False again so the 3th try will create a new component . '
0
 
LVL 9

Author Comment

by:ginsonic
ID: 7125870
P.S. If somebody put in next 5 days a working code for this problem the points are still available.
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7126461
I will check my solution again.
0
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 50 total points
ID: 7126486
I checked and it works. It seems that a Boolean variable is not the correct way. A counter has to be used.

implementation

var
  InstanceCount: Integer = 0;

constructor TMyClass.Create;
begin
  Inc(InstanceCount);
  if InstanceCount > 1 then
    raise Exception.Create('only one TMyClass component allowed');
  // anything initialized here is uninitialized in the destructor
  // which is implicitly called by the exception
end;

destructor Destroy;
begin
  Dec(InstanceCount);
end;
0
 
LVL 9

Author Comment

by:ginsonic
ID: 7126662
That it's . Work perfect now .
Thanks,
Nick
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7126684
? seems to be the same as my solution far above,
except that an integer instead a boolean is used

btw. missing the inherited destroy

meikl ;-)

0
 
LVL 9

Author Comment

by:ginsonic
ID: 7129013
Not sure why but with boolean don't work .

When I create first component the variable is set to True. When add the second this test the value and if True step create procedure, but go to destroy procedure and set it to False. So the next component (3th) can be created now .

Not sure that this is the answer for problem , but for me work alike this description.
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7129178
Do you set the variable back to False in the destructor? That is wrong.
Only set it to True in the constructor.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7129583
well, ok, never tested myself, as it was just from head
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TVirtualStringTree  search using TEdit 7 119
Delphi inherited method 6 67
SUM 2 INTEGER ARRAYS INTO 1 10 100
Tvertscrollbox like a whatsapp layout delete messages 1 16
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now