|
Lev Danielyan wrote: I actually wonder what is this for
you can use a glass plate like that to fix a broken window.
|
|
|
|
|
Right
Regards,
Lev
|
|
|
|
|
I'm losing it I think, bit I can't make this work. I have a class (Parent) that has a class (Child) within it. The Child class should only be able to be instanciated from the parent. Hopefully it'll seem clear in the morning - but in the meantime... pseudo code below.
public class Parent
{
private static Child[] children = CreateChildren();
private Parent()
{ }
public static Child GetChild(int id)
{
return children[id];
}
private static Child[] CreateChildren()
{
Child[] result = new Child[1];
result[0] = new Child();
return result;
}
public class Child
{
public Child()
{ }
}
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Then make the Child class's constructor private. Since it is nested inside the Parent class the Parent class can still access it even although it is private.
|
|
|
|
|
I just tried that and I get "'Parent.Child.Child()' is inaccessible due to its protection level".
The correct way I would have thought would be to mark the constuctor as internal. That compiles but doesn't give the desired effect as the constructor can still be accessed from outside.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
I've just realised what I got wrong. You can make the Child class private (but then it won't be accessible outside the Parent class) because that is directly a member of the Parent class.
The Child class constructor needs to be accessible to the Parent class.
Okay - the best option I can think of is to make the Child class internal and exercise restraint (in other words, don't instantiate it anywhere else). Since you control all the code in the assembly that shouldn't be too difficult. Third parties acccessing the assembly won't be able to instiantiate a Child object.
|
|
|
|
|
Yeah, that makes sense - thanks Colin.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Hi Davey,
to improve safety just a little bit, consider this:
private Child() { }
public static Child MySlightlyHiddenCreateChildMethod() {
return new Child();
}
|
|
|
|
|
How can this code provide more safety? In this case, it just use a static method instead of a public/internal construct. And the static method is not thread safe inherently.
I Love KongFu~
modified on Thursday, January 8, 2009 12:19 AM
|
|
|
|
|
Hi,
the little improvement is there no longer is a public constructor, so you now have to spend extra effort
to create a Child object in the unofficial way.
I would prefer to have a safer way to get what the OP was looking for, but I don't see one.
|
|
|
|
|
But the constructor is private and no one can instantiate it from outside of child .
|
|
|
|
|
N a v a n e e t h wrote: But the constructor is private and no one can instantiate it from outside of child.
Well, so far as i know, child is a internal class of parent. If we want no one can access child but parent, just set private for child class. Nothign about the construct. Correct me if i am misunderstanding.
I Love KongFu~
|
|
|
|
|
I need the child to be accessible but not instanciatable (is that a word?) apart from by the parent - so private won't cut it here unfortunately.
Mark gave a good solution below by having two classes for the child, a public abstract one and a private one derived from the abstract.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Dragonfly_Lee wrote: And the static method is not thread safe inherently.
Why? All he does is return an instance of a class.
|
|
|
|
|
S. Senthil Kumar wrote: Dragonfly_Lee wrote:
And the static method is not thread safe inherently.
Why? All he does is return an instance of a class.
Maybe I am not clear. In fact, no matter what the static method does, it does not render thread-safe. If the method will modify the shared resource, the method would be not thread-safe. We need to write the code to ensure this, such as lock. Hence, there is no inevitable relationship between method and thread-safe defautly.
I Love KongFu~
|
|
|
|
|
public class CheeseFactory
{
public Cheese MakeCheese()
{
return new RealCheese();
}
private class RealCheese : Cheese
{
}
public abstract class Cheese
{
}
}
You could also expose an interface, but that is likely to be more work.
|
|
|
|
|
Cool - thanks Mark.
I found that to make properties unsettable (it seems strict read only i.e. no setter isn't possible in this scenario) I had to make any property setters in the abstract class, protected. This is what I ended up with.
public static class Parent
{
private static Child[] children = CreateChildren();
public static Child GetChild(int id)
{
return children[id]; ;
}
private static Child[] CreateChildren()
{
Child[] result = new Child[1];
result[0] = new RealChild("Dave");
return result;
}
private class RealChild : Child
{
public RealChild(string name)
{
Name = name;
}
}
public abstract class Child
{
private string m_Name;
public string Name
{
get { return m_Name; }
protected set { m_Name = value; }
}
}
}
... so now I can return effectively readonly instances of the child but only from the parent class, who instanciates them privately. Just what I needed - cheers.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Hello Forum,
I am developing an application where the GUI could be displayed in multiple languages, I have an issue with Combo Boxes, is there a way to always get back the same value, I will try to explain.
I have a Combo Box where the items are “One, Two, Three” but in the French the items are “UN, Deux, Trios”
My logic says when the user chooses (comboBox.Text) “One”, execute method 'A' when user chooses “Two” execute method 'B' etc.
But in French the comboBox.Text will have the value of “Un”,
Is there a way to know that the user choose “One”?
Please Help
Thank You
|
|
|
|
|
This property [^] looks promising...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Looks good, the only issue is that you always have to remeber what index 0 stand for and so on
Thank you,
|
|
|
|
|
You can use Enum with index 0 to n... according to the combobox index values. and compare them instead of remembering the index values.
|
|
|
|
|
Thanks you very much,
do you have a small example?
Thanks again
|
|
|
|
|
The enum will only work if Your list is static and won't change while the application is running. Any change requires that the code is recompiled.
|
|
|
|
|
What you can do is create a small object to store your cultural value and your invarient (culture neutral) value.
public class GlobalisedItem
{
public string DisplayValue { get; set; }
public string InvarientValue { get; set; }
public override string ToString()
{
return DisplayValue;
}
}
The override for ToString() is necessary so that the ComboBox displays what you want it to display.
Then when you populate your ComboBox you can do something like this:
GlobalisedItem[] items = new GlobalisedItem[3];
items[0] = new GlobalisedItem { DisplayValue = "Uno", InvarientValue = "One" };
items[1] = new GlobalisedItem { DisplayValue = "Dos", InvarientValue = "Two" };
items[2] = new GlobalisedItem { DisplayValue = "Tres", InvarientValue = "Three" };
comboBox1.Items.AddRange(items);
Finally, when you are examining the SelectedItem you can do this:
label1.Text = ((GlobalisedItem)comboBox1.SelectedItem).InvarientValue;
Basically (GlobalisedItem)comboBox1.SelectedItem takes the selected item and then casts it back into the object that you had in the first place. Then it calls InvarientValue which can be your culture neutral value. It doesn't have to be a string, it can be anything you like really.
Does this help?
|
|
|
|
|