3

I am using an object initializer to create an object with a Position property like this:

var control = new HtmlTextbox(browser)
{
    Position = position;
};

As I know it's the same as:

var control = new HtmlTextbox(browser);
control.Position = position;

But I want to use initializated Position property in my constructor method. Is there any way to do it without providing the Position as an argument for constructor?

stackuser83
  • 2,012
  • 1
  • 24
  • 41
yolo sora
  • 434
  • 5
  • 17
  • 7
    No, there isn't. But if `Position` is so important for the initialization process you should provide a constructor that takes it anyway. – Tim Schmelter Jan 27 '17 at 14:40
  • How would you intend to take the code you know it is transformed into, `var control = new HtmlTextbox(browser); control.Position = position;` and use the position in the constructor? – Servy Jan 27 '17 at 14:42
  • 1
    "_But I want to use initializated Position property in my constructor method_" ... Sorry didn't understand what you said at all – M.kazem Akhgary Jan 27 '17 at 14:43
  • If you need the information available to the constructor, then *pass it into the constructor as an argument*. It doesn't make sense any other way. – Baldrick Jan 27 '17 at 14:47
  • @Servy I'm intrested if there is any ways to force constructor to set property value without puting that value into constructor paremeters. – yolo sora Jan 27 '17 at 14:47
  • 1
    @yolosora How do you expect the constructor to set a value when it isn't provided the value to be set? – Servy Jan 27 '17 at 14:48
  • if the value for position is a readonly or const, you can set the class Position member to that value in the constructor, or just use the literal value in the constructor, is that what you are looking for? – stackuser83 Jan 27 '17 at 14:50
  • @Servy by magic!! >:D – M.kazem Akhgary Jan 27 '17 at 14:50
  • @stackuser83 You cannot in fact set a `const` variable in a constructor. You can only ever assign it when declaring the variable. That's what it means for it to be constant. – Servy Jan 27 '17 at 14:51
  • @Servy: You can let the constructor see other data, but only if you have something global / static for it to see. You could set a public static property on the class from the outside, for example, before invoking the constructor, then read that. But that's pretty horrible... – Baldrick Jan 27 '17 at 14:51
  • @yolosora: what you want is possible, but only once. It's the default value – Tim Schmelter Jan 27 '17 at 14:52
  • @Servy also consider things like static globals, depending on situation needs – stackuser83 Jan 27 '17 at 14:54
  • @TimSchmelter yeah, i was using the default value before, but in some cases it provided wierd behaviour, so i wanted to make opportunity for setting propery value without global changes in my classes. Well, looks like that's impossible :( – yolo sora Jan 27 '17 at 14:57
  • @Servy you can declare a local scope variable as a const in a class constructor – stackuser83 Jan 27 '17 at 15:05
  • 1
    Looks like [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is your ultimate goal here? – slawekwin Jan 27 '17 at 15:06
  • @stackuser83 Yes, you can, but you could never assign the value of a variable to said local. – Servy Jan 27 '17 at 15:06

2 Answers2

4

What you want to achieve is not possible.

It seems to me you want to make some parameters to the constructor optional. You may want to look into this pattern:

//Your constructor
public HtmlTextbox(TextboxConfiguration config)
{
    //config.Position
}

//A Transfer class
public class TextboxConfiguration
{
    public T Browser { get; set; }
    public T Position { get; set; }
}

//Your code
var config = new TextboxConfiguration
{
    Browser = browser;
    Position = position;
}
var textbox = new HtmlTextbox(config);
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
0

Another way you can approach this is using inheritance and initialize the value from there, as the most-derived class' constructor gets called last, but its values are initialized first.

jnhecate
  • 11
  • 1
  • 4