Cunstructors with arguments in VB 2005??

  • Thread starter Thread starter Just Me
  • Start date Start date
J

Just Me

If I use one of my Usercontrols on a form the IDE does not admit to a
constructor with arguments.

I'm wondering if the 2005 version has improved on this.

I have controls that have properties that absolutely must be set before
other properties are set. Argument to NEW word be a nice way of doing it.

Anyone familiar with 2005 version?
 
Just Me said:
If I use one of my Usercontrols on a form the IDE does not admit to a
constructor with arguments.

I'm wondering if the 2005 version has improved on this.

I have controls that have properties that absolutely must be set before
other properties are set. Argument to NEW word be a nice way of doing it.

/How/ should the IDE know what values to pass to the constructor?
 
Sounds like a design issue.
If you require default values then set them in the default new constructor

Sub New()
myclass.new("def1", "def2")
End

Sub New(arg1 as type, arg2 as type)
...
End Sub

You might also want to check out me.DesignMode property. That way you can
defer execution if you are currently
working with the control in design mode.

If me.designmode then exit sub

hth
Richard
 
Suppose a form contained a usercontrol

When the form was created it could be passed values via its New arguments.
In the form, before InitializeComponent() was called, the form would have to
set the argument values for any usercontrols it contained, maybe using the
values passed to it - maybe not (guess the arguments would have to be
module level or they could be local to New and passed to
InitializeComponent)

I suppose the IDE could generate default code for New (the code before the
InitializeComponent() as it does now for properties

Probably, a little more thought could produce a better approach.

Anyway, I surmise the answer to my question below is: No.

Thanks
 
Richard Myers said:
Sounds like a design issue.
If you require default values then set them in the default new constructor

Sub New()
myclass.new("def1", "def2")
End

Sub New(arg1 as type, arg2 as type)
...
End Sub

Not sure what you telling me I can do. If my usercontrol in on a form, the
IDE generates the call to the usecontrol New. and I'm told :
'Do not modify it using the code editor.

therefore, where are you picturing the statement myclass.new("def1",
"def2")?
Is your Sub New() above the form's New?

The me.designmode note below is very good to know.
I need that!
 
Just Me said:
Not sure what you telling me I can do. If my usercontrol in on a form, the
IDE generates the call to the usecontrol New. and I'm told :
'Do not modify it using the code editor.

Simply add both constructors to your control. Provide the parameterless
constructor for the designer and call the parameterized constructor with
default values inside the parameterless constructor.
 
I got it now.

Thanks to both

Herfried K. Wagner said:
Simply add both constructors to your control. Provide the parameterless
constructor for the designer and call the parameterized constructor with
default values inside the parameterless constructor.
 
Back
Top