Object initializer question

C

CSharper

I am creating a MVC pattern code for my project and here is what I am
doing

in the view constructor

presenter = new presenter(){name = "MVC", view = this, age=30};

and int the presnter I have public variables for name, age and
declared the view as interface to interact with the view.

After the initialization, while inside the presenter, when I checked
the passed values and none of them are set and all of them are
assigned to defaults. I am developing it through C#3.0 Frameowrk 3.5
and WinForms. Am I doing something wrong?
 
C

CSharper

I am creating a MVC pattern code for my project and here is what I am
doing

in the view constructor

presenter = new presenter(){name = "MVC", view = this, age=30};

and int the presnter I have public variables for name, age and
declared the view as interface to interact with the view.

After the initialization, while inside the presenter, when I checked
the passed values and none of them are set and all of them are
assigned to defaults. I am developing it through C#3.0 Frameowrk 3.5
and WinForms. Am I doing something wrong?

One additional information, the view is a usercontrol. I stepped
through the program and it looks like the object initializer is
working properly when the constrctor is called. But later when I
consume the object thats when the properties are reset to default
values.
 
J

Jon Skeet [C# MVP]

CSharper said:
I am creating a MVC pattern code for my project and here is what I am
doing

in the view constructor

presenter = new presenter(){name = "MVC", view = this, age=30};

and int the presnter I have public variables for name, age and
declared the view as interface to interact with the view.

After the initialization, while inside the presenter, when I checked
the passed values and none of them are set and all of them are
assigned to defaults. I am developing it through C#3.0 Frameowrk 3.5
and WinForms. Am I doing something wrong?

Well probably, but it's hard to tell from a single line of code.

(Arguably yes, you're doing something wrong by having public variables
instead of properties in the first place, but...)

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
C

CSharper

[...]
One additional information, the view is a usercontrol. I stepped
through the program and it looks like the object initializer is
working properly when the constrctor is called. But later when I
consume the object thats when the properties are reset to default
values.

There's nothing obviously wrong with the code you posted.  So it means  
your mistake is somewhere in the code you didn't post.

As always, a concise-but-complete code example that reliably reproduces  
the problem would be much better than the vague description we've got to  
work with so far.

For what it's worth, the fact that you say that the "initializer is  
working properly" at any point suggest strongly that the line of code you  
posted is in fact doing what it's supposed to, and that instead you've got 
some other code somewhere that's creating a new instance and overwriting  
the old one.

But without a complete code example, there's no way for anyone else to  
show you where that's happening.  :(

Pete

Thanks and I agree. Here is the think, I have created a user control
(which is the one I have it as MVC) and I can step through constructor
code and everything is peachy. The next call to the user control
happens in a button. Right there when I stepped into it all the values
initialised during the constructor is gone. There are no code in
between. So I am kind of lost. But I will see if I can reproduce it
with a simple code.
Later,
 
C

CSharper

[...]
One additional information, the view is a usercontrol. I stepped
through the program and it looks like the object initializer is
working properly when the constrctor is called. But later when I
consume the object thats when the properties are reset to default
values.
There's nothing obviously wrong with the code you posted. So it means
your mistake is somewhere in the code you didn't post.
As always, a concise-but-complete code example that reliably reproduces
the problem would be much better than the vague description we've got to
work with so far.
For what it's worth, the fact that you say that the "initializer is
working properly" at any point suggest strongly that the line of code you
posted is in fact doing what it's supposed to, and that instead you've got
some other code somewhere that's creating a new instance and overwriting
the old one.
But without a complete code example, there's no way for anyone else to
show you where that's happening. :(

Thanks and I agree. Here is the think, I have created a user control
(which is the one I have it as MVC) and I can step through constructor
code and everything is peachy. The next call to the user control
happens in a button. Right there when I stepped into it all the values
initialised during the constructor is gone. There are no code in
between. So I am kind of lost. But I will see if I can reproduce it
with a simple code.
Later,

Well, I took your suggestion and created a simple project exactly like
the one I am trying to solve and in that, the data presisted so I
think I am doing something wrong in this one.
 
I

Ignacio Machin ( .NET/ C# MVP )

On Apr 4, 1:10 pm, "Peter Duniho" <[email protected]>
wrote:
[...]
One additional information, the view is a usercontrol. I stepped
through the program and it looks like the object initializer is
working properly when the constrctor is called. But later when I
consume the object thats when the properties are reset to default
values.
There's nothing obviously wrong with the code you posted.  So it means
your mistake is somewhere in the code you didn't post.
As always, a concise-but-complete code example that reliably reproduces
the problem would be much better than the vague description we've got to
work with so far.
For what it's worth, the fact that you say that the "initializer is
working properly" at any point suggest strongly that the line of code you
posted is in fact doing what it's supposed to, and that instead you'vegot
some other code somewhere that's creating a new instance and overwriting
the old one.
But without a complete code example, there's no way for anyone else to
show you where that's happening.  :(
Pete
Thanks and I agree. Here is the think, I have created a user control
(which is the one I have it as MVC) and I can step through constructor
code and everything is peachy. The next call to the user control
happens in a button. Right there when I stepped into it all the values
initialised during the constructor is gone. There are no code in
between. So I am kind of lost. But I will see if I can reproduce it
with a simple code.
Later,

Well, I took your suggestion and created a simple project exactly like
the one I am trying to solve and in that, the data presisted so I
think I am doing something wrong in this one.- Hide quoted text -

- Show quoted text -

Most probably you are creating a new instance of the presenter
somewhere and not initializing the parameters.
 
C

CSharper

On Apr 4, 1:10 pm, "Peter Duniho" <[email protected]>
wrote:
[...]
One additional information, the view is a usercontrol. I stepped
through the program and it looks like the object initializer is
working properly when the constrctor is called. But later when I
consume the object thats when the properties are reset to default
values.
There's nothing obviously wrong with the code you posted.  So it means
your mistake is somewhere in the code you didn't post.
As always, a concise-but-complete code example that reliably reproduces
the problem would be much better than the vague description we've got to
work with so far.
For what it's worth, the fact that you say that the "initializer is
working properly" at any point suggest strongly that the line of code you
posted is in fact doing what it's supposed to, and that instead you've got
some other code somewhere that's creating a new instance and overwriting
the old one.
But without a complete code example, there's no way for anyone else to
show you where that's happening.  :(
Pete
Thanks and I agree. Here is the think, I have created a user control
(which is the one I have it as MVC) and I can step through constructor
code and everything is peachy. The next call to the user control
happens in a button. Right there when I stepped into it all the values
initialised during the constructor is gone. There are no code in
between. So I am kind of lost. But I will see if I can reproduce it
with a simple code.
Later,
Well, I took your suggestion and created a simple project exactly like
the one I am trying to solve and in that, the data presisted so I
think I am doing something wrong in this one.- Hide quoted text -
- Show quoted text -

Most probably you are creating a new instance of the presenter
somewhere and not initializing the parameters.

Thanks, thats what my first instict as well, I did a global search on
the presenter and I don't find any where else except in the view
constructor. I will research this more. Thanks again for looking into
it.
 
J

Jon Skeet [C# MVP]

Thanks, thats what my first instict as well, I did a global search on
the presenter and I don't find any where else except in the view
constructor. I will research this more. Thanks again for looking into
it.

Make the public variables into properties instead, stick a breakpoint
in both the properties and the constructor, then you should be able to
see if anything odd is happening (with something unexpectedly creating
an extra object, or setting properties when you don't expect it to).
 
C

CSharper

Make the public variables into properties instead, stick a breakpoint
in both the properties and the constructor, then you should be able to
see if anything odd is happening (with something unexpectedly creating
an extra object, or setting properties when you don't expect it to).

It is crazy, I changed the property to method and put a break point on
the set and get and it is still happening. But Set is invoked only
once and it sets the correct value and next time, it breaks at the get
and at that time the value is null.
 
J

Jon Skeet [C# MVP]

CSharper said:
It is crazy, I changed the property to method

Do you mean changed the field to a property?
and put a break point on
the set and get and it is still happening. But Set is invoked only
once and it sets the correct value and next time, it breaks at the get
and at that time the value is null.

Again, that sounds like *something* is creating a new value.

Just to check: this *is* a class and not a struct, right?
 
I

Ignacio Machin ( .NET/ C# MVP )

It is crazy, I changed the property to method and put a break point on
the set and get and it is still happening. But Set is invoked only
once and it sets the correct value and next time, it breaks at the get
and at that time the value is null.- Hide quoted text -

- Show quoted text -

Can you post a complete example?
Try to minimize it to a minimun
 

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