User Control Trouble with Runtime Property

D

dave

I am having trouble accessing a property set during run-time in a user
control that I have built. The control is supposed to connect to a
database using the path supplied by the developer at runtime. Any
assistance would be greatly appreciated. The code is as follows:

In the class I declared the private variable strDatabasePath
public partial class UserControl1 : UserControl
{ //... other code

private string strDatabasePath;

//... other code
}


I created the DatabasePath accessor to store the user input from
design-time into the variable. The property shows up in the User
Control's property window.
[Browsable(true), Category("Database"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
DefaultValue(true),
Description("Sets and gets the database path.")]
public string DatabasePath
{
get
{
return strDatabasePath;

}
set
{
strDatabasePath = value;

}

}



In the load method I attempt to use the strDatabasePath variable to
access database.The variable is always set to null when I check it in
the debugger whether I set it in the property window during design time
or not.
private void UserControl1_Load(object sender, EventArgs e)
{
//... other code

//Establish Connection to the Database
MyDbConnect = new DataConnect(strDatabasePath);

//... other code

}
 
M

Marc Gravell

Have a look in initializecomponent; since you seem to be using 2.0 this
will be in the .designer.cs; I suspect that the property value is never
being written to the source code (?is it there if you save the project,
close the IDE and reload it?).

The reason I think is the [DefaultValue(true)]; since the property is
typed as a string, this should probably be [DefaultValue("")], with
matching field initializer:

private string strDatabasePath = "";

The designer checks the value of [DefaultValue(x)], compares it to the
current value, and writes code if it is different; the illegal bool /
string cast is probably throwing it (although it would be nice if it
failed-safe, and acted as though [DefaultValue(x)] had not been
applied, i.e. wrote the value /all/ the time...

Let me know if this helps (I haven't done any tests, so this is all
on-a-maybe...)

Marc
 
M

Marc Gravell

Right theory, wrong attribute; it is the DesignerSerializationVisibility
that is the problem; either remove this or set it to visible, and everything
works.

Marc

(PS: for ref (from my last post) the runtime /does/ failsafe for bad
[DefaultValue(x)] as I suggested it should...)
 

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