tags:

views:

144

answers:

1

HI all,

Please help me with this:

I have a form which contains my custom control.

In the custom control, I have 2 collections referencing to the same DataSource to get data.

My current CodeDOM serializer working like this:

control1.Values.DataSource = new objA();
   control1.CategoryNames.DataSource = new objA();

As you can see, the objA was instantiated 2 time.

How to solve this?

I guess I can declare a variable that hold reference to the objA, then assign that variable to 2 collections:

    ObjA var = new objA();
    control1.Values.DataSource = var;
   control1.CategoryNames.DataSource = var;

But I cannnot ensure the "var" is an unique name. How can I get it automatically assigned exactly the name Form gave to my control (control1, control2 and so on)?

Thank you.

+1  A: 

As an alternative, you could name the variable as a guid. It's not guaranteed to be unique, but it's very likely that it will be (especially if none of the other variables in your class are named as guids).

You'll need to do some filtering on it to make it a valid variable name, such as making sure it starts with a letter, and removing the dashes.

string variableName = "A" + Guid.NewGuid().ToString().Replace("-","");
Matthew Steeples
there's case that the variableName was declared else where such as if I use multiple instance of custom control.I need a way to check whether a variableName valid to use. Please Help!!
thethanghn
if you regenerate the guid each time then there is a very slim chance that the names will be the same.
Matthew Steeples