Unique object name for every object created

D

David Hubball

Hi

Please can anyone help me - I'm trying to increment the name of my
object every time one gets created but not sure how to do that. My
code so far is this to create lots of MDI child forms :-

createChildForm()
{
Form ChildFormStickyNote1 = new FormStickyNote();
ChildFormStickyNote1.MdiParent = this;
ChildFormStickyNote1.Show();
}

Ideally, I would like it so that the object name ChildFormStickyNote1
increments to Form ChildFormStickyNote2, ChildFormStickyNote2 for
every object created etc.... Ultimately, I'd like to save the
properties of each MDI form when the user exits - which is why (I
think) I need to have a unique object name for each MDI form created.

Very much appreciated to anyone who can help.

Thanks
David
 
P

Peter Duniho

Hi

Please can anyone help me - I'm trying to increment the name of my
object every time one gets created but not sure how to do that. My
code so far is this to create lots of MDI child forms :-

createChildForm()
{
Form ChildFormStickyNote1 = new FormStickyNote();
ChildFormStickyNote1.MdiParent = this;
ChildFormStickyNote1.Show();
}

Ideally, I would like it so that the object name ChildFormStickyNote1
increments to Form ChildFormStickyNote2, ChildFormStickyNote2 for
every object created etc....

I kind of understand what you're trying to do, but I don't really
understand the question. You didn't post any code that sets the name of
your new FormStickyNote in the first place. Without knowing how the name
is set to start with, it's hard to suggest how to change that to set the
name in the way you want.

As far as coming up with the name itself, there are two reasonably common
approaches for program objects with names that are presented to the user:

-- Just maintain a counter, and use that as part of the name,
incrementing the counter with each new object
-- Keep a list of all the objects (something that typically would
happen anyway), and search the list for the first available number
Ultimately, I'd like to save the
properties of each MDI form when the user exits - which is why (I
think) I need to have a unique object name for each MDI form created.

If the only reason for having a unique object name is for internal use -
that is, the user will never actually see the name - then you may find
that using a new Guid value for each new object is even easier than the
above two suggestions.

Pete
 
S

Scott M.

This can only be accomplished through the use of System.Reflection, which
gives you classes that help in creating code that creates more code.

What you are tryiing to do requires late binding, which C# does not natively
allow.

-Scott
 
T

Tom Spink

David said:
Hi

Please can anyone help me - I'm trying to increment the name of my
object every time one gets created but not sure how to do that. My
code so far is this to create lots of MDI child forms :-

createChildForm()
{
Form ChildFormStickyNote1 = new FormStickyNote();
ChildFormStickyNote1.MdiParent = this;
ChildFormStickyNote1.Show();
}

Ideally, I would like it so that the object name ChildFormStickyNote1
increments to Form ChildFormStickyNote2, ChildFormStickyNote2 for
every object created etc.... Ultimately, I'd like to save the
properties of each MDI form when the user exits - which is why (I
think) I need to have a unique object name for each MDI form created.

Very much appreciated to anyone who can help.

Thanks
David

Hi David,

I'm not sure I understand your problem. At the moment, the code you
have written is great for creating multiple instances of your form - but
the variable you are assigning it to is only valid within the scope of
the createChildForm() method, meaning it has no meaning outside that
context and indeed you could call your variable whatever you want.

Further more, variable names have no meaning at run-time -
ChildFormStickyNote1 is just a named slot in memory, it doesn't relate
to anything except your own sanity when coding.

Could you explain a bit what you mean by:
Ultimately, I'd like to save the
properties of each MDI form when the user exits

Thanks,
 
G

Göran Andersson

David said:
Hi

Please can anyone help me - I'm trying to increment the name of my
object every time one gets created but not sure how to do that. My
code so far is this to create lots of MDI child forms :-

createChildForm()
{
Form ChildFormStickyNote1 = new FormStickyNote();
ChildFormStickyNote1.MdiParent = this;
ChildFormStickyNote1.Show();
}

Ideally, I would like it so that the object name ChildFormStickyNote1
increments to Form ChildFormStickyNote2, ChildFormStickyNote2 for
every object created etc.... Ultimately, I'd like to save the
properties of each MDI form when the user exits - which is why (I
think) I need to have a unique object name for each MDI form created.

Very much appreciated to anyone who can help.

Thanks
David

The variable ChildFormStickyNote1 is not the name of the form, it's just
the name of a variable that holds the reference to the form.

You don't need different names for the forms, what you need is a
collection where you store the references to the forms:

private List<FormStickyNote> _childForms = new List<FormStickyNote>();

createChildForm() {
FormStickyNote childForm = new FormStickyNote();
childForm.MdiParent = this;
childForm.Show();
_childForms.Add(childForm);
}
 
D

David Hubball

Hi David,

I'm not sure I understand your problem.  At the moment, the code you
have written is great for creating multiple instances of your form - but
the variable you are assigning it to is only valid within the scope of
the createChildForm() method, meaning it has no meaning outside that
context and indeed you could call your variable whatever you want.

Further more, variable names have no meaning at run-time -
ChildFormStickyNote1 is just a named slot in memory, it doesn't relate
to anything except your own sanity when coding.

Could you explain a bit what you mean by:


Thanks,

Hi Tom

Basically I'm trying to write a project for a user to create MDI child
forms within a parent form --- to keep track of what tasks they have
left to do. I thought is would be nice if the user could save the MDI
forms as they looked before exiting, so when they re-load the
application, the forms created will still look exactly the same.

The only code I've written so far is :-

private void newStickyNoteToolStripMenuItem_Click(object sender,
EventArgs e)
{
Form ChildFormStickyNote1 = new FormStickyNote();
ChildFormStickyNote1.MdiParent = this;
ChildFormStickyNote1.Show();
}

which I've just put inside File>Open menu.

I've also changed the parent form property IsMdiContainer to equal
true so it knows its a parent form.

The new form (the child) was just a form that a manually created by
going into the Project > Add Windows Form in Studio --- I called the
child form FormStickyNote which I've not done anything else with as
yet but maybe I'll add a text box for the user to type some notes into
(which again I will save the notes in a text document for example
later when I'm doing the project some more).

Please anyone be a bit more specific (with examples) on how to create
a unique object name (or how to access he object guid? ) so I can then
use to save the form properties.

Thanks again
David
 
J

Jeff Johnson

Please can anyone help me - I'm trying to increment the name of my
object every time one gets created but not sure how to do that. My
code so far is this to create lots of MDI child forms :-

createChildForm()
{
Form ChildFormStickyNote1 = new FormStickyNote();
ChildFormStickyNote1.MdiParent = this;
ChildFormStickyNote1.Show();
}

Ideally, I would like it so that the object name ChildFormStickyNote1
increments to Form ChildFormStickyNote2, ChildFormStickyNote2 for
every object created etc.... Ultimately, I'd like to save the
properties of each MDI form when the user exits - which is why (I
think) I need to have a unique object name for each MDI form created.

Very much appreciated to anyone who can help.

You've got to separate the concept of design-time (or coding-time) from
run-time. You're operating in a design-time mindset and wanting to create
new "in-code" variables at run-time. Abandon that, as that way leads to
madness. Do as Göran suggested.
 
S

Scott M.

As I said, this is not possible in C#, unless you were to use the
System.Relfection classes. The problem is that to construct the variable
name dynamically from a string, you are essentially in need of software that
can write more software. Since C# is type-safe doesn't allow for late
binding (which is what you need to be able to accomplish what you are
asking), you can't do it.

However, the forms that are created will be available via the forms
collection. This means you could loop through the collection looking for
something that is unique to each form, like perhaps its text property value.

-Scott


Hi David,

I'm not sure I understand your problem. At the moment, the code you
have written is great for creating multiple instances of your form - but
the variable you are assigning it to is only valid within the scope of
the createChildForm() method, meaning it has no meaning outside that
context and indeed you could call your variable whatever you want.

Further more, variable names have no meaning at run-time -
ChildFormStickyNote1 is just a named slot in memory, it doesn't relate
to anything except your own sanity when coding.

Could you explain a bit what you mean by:


Thanks,

Hi Tom

Basically I'm trying to write a project for a user to create MDI child
forms within a parent form --- to keep track of what tasks they have
left to do. I thought is would be nice if the user could save the MDI
forms as they looked before exiting, so when they re-load the
application, the forms created will still look exactly the same.

The only code I've written so far is :-

private void newStickyNoteToolStripMenuItem_Click(object sender,
EventArgs e)
{
Form ChildFormStickyNote1 = new FormStickyNote();
ChildFormStickyNote1.MdiParent = this;
ChildFormStickyNote1.Show();
}

which I've just put inside File>Open menu.

I've also changed the parent form property IsMdiContainer to equal
true so it knows its a parent form.

The new form (the child) was just a form that a manually created by
going into the Project > Add Windows Form in Studio --- I called the
child form FormStickyNote which I've not done anything else with as
yet but maybe I'll add a text box for the user to type some notes into
(which again I will save the notes in a text document for example
later when I'm doing the project some more).

Please anyone be a bit more specific (with examples) on how to create
a unique object name (or how to access he object guid? ) so I can then
use to save the form properties.

Thanks again
David
 
D

David Hubball

You've got to separate the concept of design-time (or coding-time) from
run-time. You're operating in a design-time mindset and wanting to create
new "in-code" variables at run-time. Abandon that, as that way leads to
madness. Do as Göran suggested.- Hide quoted text -

- Show quoted text -

Thanks Jeff and everyone for your help - I'll use the ListArray class
now.
 
S

Scott M.

You shouldn't even need to do that. Each MDIChild form is going to wind up
in the forms collection anyway, so just use the collection you already have.
No need to introduce more code to do what you already can.

-Scott



You've got to separate the concept of design-time (or coding-time) from
run-time. You're operating in a design-time mindset and wanting to create
new "in-code" variables at run-time. Abandon that, as that way leads to
madness. Do as Göran suggested.- Hide quoted text -

- Show quoted text -

Thanks Jeff and everyone for your help - I'll use the ListArray class
now.
 
D

David Hubball

You shouldn't even need to do that.  Each MDIChild form is going to wind up
in the forms collection anyway, so just use the collection you already have.
No need to introduce more code to do what you already can.

-Scott






Thanks Jeff and everyone for your help - I'll use the ListArray class
now.- Hide quoted text -

- Show quoted text -

Hi all

I've not learn't about Generics yet which is why I was getting
confused.
I'll use this method now

Thanks again
David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top