Solved

Just ONE

Posted on 2002-06-17
26
138 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
Comment Utility
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
Comment Utility
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
Comment Utility
And for this , can you give me a sample :)
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
? whats the benefit, robert (richard) :-))
0
 
LVL 9

Author Comment

by:ginsonic
Comment Utility
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
Comment Utility
? last comment pointed to whom, nick
0
 
LVL 3

Expert Comment

by:raidos
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 9

Author Comment

by:ginsonic
Comment Utility
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
Comment Utility
Hey guys,
Nothing for me ?

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

Expert Comment

by:Netminder
Comment Utility
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
Comment Utility
I object.
Several working solutions have been proposed.
Mine works for me.
0
 
LVL 9

Author Comment

by:ginsonic
Comment Utility
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
Comment Utility
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
Comment Utility
I will check my solution again.
0
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 50 total points
Comment Utility
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
Comment Utility
That it's . Work perfect now .
Thanks,
Nick
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
? 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
Comment Utility
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
Comment Utility
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
Comment Utility
well, ok, never tested myself, as it was just from head
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

15 Experts available now in Live!

Get 1:1 Help Now