Passsing form object to a class

ran_deep

New member
Joined
Jan 25, 2005
Messages
4
Location
Malaysia
Programming Experience
3-5
Guys,

I created a Win Application, which includes user database. Now including user security module.
What I'm trying to do now is, enable end users to set own security setting for each user of the application.
Example, each user will belong to specific group, and each group will be assigned to tasks allowed to be performed. (All the info are stored in the SQL db).

So I've created a class, to which I'll supply specific Forms as object like below :

'check security setting for current user
VB.NET:
	Public Sub SecurityTask(ByVal obj As Object, ByVal userID As Integer, ByVal connKey As String, ByVal moduleName As String)
			'Get DAL access
			dal = New DataAccess(connKey)
			Dim tbl As New Data.DataTable
			Dim e As System.Windows.Forms.Control
			'store settings
			dal.BeginProc("sp_GetSecurityTask")
			dal.AddParameter("@UserID", userID)
			dal.AddParameter("@Module", moduleName)
			dal.EndProc()
			dal.ExecuteDataTable(tbl)
			dal.ClearCommandCollection()
			For i = 0 To tbl.Rows.Count - 1
				e = tbl.Rows(i).Item("Task")
				obj.e = tbl.Rows(i).Item("Permission")
		Next
Sample tasks are like :
  • MenuItems(1).MenuItems(0).Enabled
  • MenuItems(1).MenuItems(1).Enabled
  • cmdSave.Enabled
and permissions are either 1 or 0.
Now I get error on 'e' = 'Specific cast is not valid'.

I know why this occurs, but don't know way around for this.

Please help.
If you think my logic is wrong. Please correct me.
 
The value you are assigning to e is a string, not a control. I would think that you would need to have ControlName, PropertyName and PropertyValue columns in your database. You could then find the required control by name, but you would need to give the property meaning yourself, i.e. create a procedure for each property that will need to be set. Something like this:
VB.NET:
Dim controlName As String = tbl.Rows(i)("ControlName")
Dim propertyName As String = tbl.Rows(i)("PropertyName")
Dim propertyValue As String = tbl.Rows(i)("PropertyValue")
Dim selectedControl As Control

For Each selectedControl In Me.Controls
	If selectedControl.Name = controlName Then
		Exit For
	End If
Next

Select Case propertyName
	Case "Enabled"
		Me.SetControlEnabled(selectedControl, CBool(propertyValue))
	...
End Select

...

Private Sub SetControlEnabled(ByVal control As Control, ByVal enabled As Boolean)
	control.Enabled = enabled
End Sub
 
Back
Top