Solved

Just ONE

Posted on 2002-06-17
26
144 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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