Having an optional variable in the constructor causes error

G

Guest

Hi,
I am trying to pass the name of a grid I create in a form through to the grid control class I have created. I am doing this so that I can setup the grid according to the settings I store in a database. The grid I created inherits the Datagrid. I tried mybase.name but that doesn't work I just get an empty string back. My second Idea was to include an optional variable in the constructor. Just running the code this works however I do get an error message saying:
'Public Sub New([Name As String = ""])' and 'Public Sub New()' cannot overload each other because they differ only by optional parameters.
The code I am using is down here:

Option Strict Off
Option Explicit On

Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms

Public Class DGrid
Inherits DataGrid


#Region " Windows Form Designer generated code "
Public Sub New(Optional ByVal Name As String = "")
MyBase.New()
If Not Name = "" Then Me.Name = Name

'This call is required by the Windows Form Designer.
InitializeComponent()

MsgBox(Name)
LoadDataset()
'setColumns()

'Add any initialization after the InitializeComponent() call

End Sub


Any suggestion please...
please keep it simple as I am still to be considered a newbie ;)))

Thanks

David J
 
A

Andy Gaskell

You should overload the constructor. It's also a bad idea to modify code
generated by VS.NET. I'm not a VB.NET guy so the syntax might be a bit off.

First, put the old constructor back the way it was. Then add a new
constructor that looks like this:

Public Sub New(ByVal Name As String)
If Not Name = "" Then Me.Name = Name
Me.New()
End Sub


David J said:
Hi,
I am trying to pass the name of a grid I create in a form through to the
grid control class I have created. I am doing this so that I can setup the
grid according to the settings I store in a database. The grid I created
inherits the Datagrid. I tried mybase.name but that doesn't work I just get
an empty string back. My second Idea was to include an optional variable in
the constructor. Just running the code this works however I do get an error
message saying:
'Public Sub New([Name As String = ""])' and 'Public Sub New()' cannot
overload each other because they differ only by optional parameters.
 

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