Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 157
  • Last Modified:

Just ONE

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
ginsonic
Asked:
ginsonic
  • 10
  • 8
  • 6
  • +2
1 Solution
 
kretzschmarCommented:
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
 
ginsonicAuthor Commented:
Hi meikl ,
Have a problem with previous accepted question . Can you take a look ? I put there the description.
Thanks in advance!
0
 
ginsonicAuthor Commented:
And for this , can you give me a sample :)
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
kretzschmarCommented:
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
 
kretzschmarCommented:
oops, didn't see your post

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

meikl ;-)
0
 
robert_marquardtCommented:
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
 
kretzschmarCommented:
? whats the benefit, robert (richard) :-))
0
 
ginsonicAuthor Commented:
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
 
kretzschmarCommented:
? last comment pointed to whom, nick
0
 
raidosCommented:
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
 
ginsonicAuthor Commented:
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
 
kretzschmarCommented:
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
 
robert_marquardtCommented:
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
 
ginsonicAuthor Commented:
Send it.
And I appreciate your help. I really need it soon.

Nick

P.S. New 100 points for this .
0
 
ginsonicAuthor Commented:
Hey guys,
Nothing for me ?

Meykl,
Thanks for the other problem. Take a look for your points :)
0
 
NetminderCommented:
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
 
robert_marquardtCommented:
I object.
Several working solutions have been proposed.
Mine works for me.
0
 
ginsonicAuthor Commented:
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
 
ginsonicAuthor Commented:
P.S. If somebody put in next 5 days a working code for this problem the points are still available.
0
 
robert_marquardtCommented:
I will check my solution again.
0
 
robert_marquardtCommented:
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
 
ginsonicAuthor Commented:
That it's . Work perfect now .
Thanks,
Nick
0
 
kretzschmarCommented:
? 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
 
ginsonicAuthor Commented:
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
 
robert_marquardtCommented:
Do you set the variable back to False in the destructor? That is wrong.
Only set it to True in the constructor.
0
 
kretzschmarCommented:
well, ok, never tested myself, as it was just from head
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 10
  • 8
  • 6
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now