blah blah blah is here! blah blah » Close

up0down
link

Hey, I am working on a web control (ascx) and there are various private properties/feilds that are declared, but when I assign them values, they don't seems to get them (see example below):
[C#]
public class dome : ...
{
-private string m_dome;
-public string f_dome;
-
-public string Dome
-{
--get
--{
---return m_dome;
--}
--set
--{
---m_dome = value;
--}
-}
...
-Page_load(..)
-{
--f_dome = "Hello";
--Dome = "Bye";
--Response.WriteLine(f_dome + ' ' + Dome);
-}
}
If I run the code and try to display the vars inside the class, nothing shows up. When I trace the app in debug mode, the f_dome and Dome are null after they are assigned values!
What gives?!
}

last answered 2 years ago

1 answers

up0down
link

That page_load isn't being called. You either need to put the call to Page_load in the constructor or after constructing, call domeobjectname.Page_load();
Hope that helps!

up0down
link

EEP! vbguyny! Repeat after me! NO PUBLIC FIELDS!!!! Also, Pascal case on classes, properties, methods, CamelCase on fields!
OK, now that I got that off my chest.... ;)
I think the problem is probably that you are using these before they are instantiated, because controls don't instantiate the same way pages do.
You need to override CreateChildControls() in your control. I can't get to VS.Net right now, or I'd try to point you to an example.
Also, in your properties, you need to call EnsureChildControls() before setting values.
public string Dome {
get {
EnsureChildControls();
return _dome; //I like this notation, too..it's what MS uses.
}
set {
EnsureChildControls();
_dome = value;
}
}
Hope this helps!

up0down
link

Firstly, The page_load method is being called, I place breakpoints almost everywhere in the code. Lines are encountered, but no values are set.
Secondly, I'll try your suggest, Chads and get back to you.

up0down
link

Sorry, Chads, tryed what you suggested but no succuss.

up0down
link

Hmm...I'll ask around. I had the same problem, but the EnsureChildControls() fixed it.
And, you can call me Chad. :)

up0down
link

Ok. Found a strange solution, I changed the property block to a public field (I know, Chad). Good thing it's not my app ;)

up0down
link

As long as we agree that the solution to the problem doesn't technically make any sense... :)
Take care, bud!

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback