Ambiguous match found - vb.net

I face a problem with Ambiguous match found
What I am trying to do is described in :GetType.GetProperties
In two words I am trying to run through all the properties of a control and find if user had made any changes to control's properties , then I take only the changed properties and store the values for these properties
I followed the suggestions but I get an error for propery Padding when the control is a TabControl (the tabControl has 2 tabPages).

Ok with help from Ravindra Bagale I manage to solve it:
The problem wasn't the new modifier but my stupidity:
In MSDN is says:
Situations in which AmbiguousMatchException occurs include the following:
A type contains two indexed properties that have the same name but different numbers of parameters. To resolve the ambiguity, use an overload of the GetProperty method that specifies parameter types.
A derived type declares a property that hides an inherited property with the same name, by using the new modifier (Shadows in Visual Basic). To resolve the ambiguity, use the GetProperty(String, BindingFlags) method overload and include BindingFlags.DeclaredOnly to restrict the search to members that are not inherited.
So I used BindingFlags.DeclaredOnly and the problem solved:
Private Sub WriteProperties(ByVal cntrl As Control)
Try
Dim oType As Type = cntrl.GetType
'Create a new control the same type as cntrl to use it as the default control
Dim newCnt As New Control
newCnt = Activator.CreateInstance(oType)
For Each prop As PropertyInfo In newCnt.GetType().GetProperties(BindingFlags.DeclaredOnly)
Dim val = cntrl.GetType().GetProperty(prop.Name).GetValue(cntrl, Nothing)
Dim defVal = newCnt.GetType().GetProperty(prop.Name).GetValue(newCnt, Nothing)
If val.Equals(defVal) = False Then
'So if something is different....
End If
Next
Catch ex As Exception
MsgBox("WriteProperties : " & ex.Message)
End Try
End Sub

I have to apologize.
My previous answer was wrong.
With BindingFlags.DeclaredOnly I don't get the properties that I wanted.
So I had to correct the problem with other way.
The problem occurs because two properties have the same name.
So I searched where the same named properties are different and I found that they have: have different declaringType,MetadataToken and PropertyType.
So I change the way I get the value and problem solved:
Dim val = cntrl.GetType().GetProperty(prop.Name, prop.PropertyType).GetValue(cntrl, Nothing)
Dim defVal = newCnt.GetType().GetProperty(prop.Name,prop.PropertyType).GetValue(newCnt,Nothing)
Sorry if I misguided someone.

Related

Method to return extending type instance from extended control

In my solution, I have a custom component which implements IExtenderProvider in order to provide properties to other controls. I would like to implement a method for that component which, taking a control as argument, would return the instance of the extender component it is associated with, something like this:
Public Function GetErrorProvider(c As Control) As MyErrorProvider
Dim errorProvider as MyErrorProvider
'Some code here
Return errorProvider
End Function
I thought of simply looking at the form and looping for a control of the MyErrorProvider type and use that, as I am not going to have more than one of this component per form, but I would like a more direct approach. I want this for some logic that depends on runtime defined values for that instance, outside the scope of forms.
Any ideas/suggestions?
Thanks
For completeness purposes, I am adding the solution that worked converted from the C# code linked above and adjusted slightly. It appears this can only be accomplished with reflection (correct me if I am wrong!):
Public Shared Function GetErrorProvider(control As Control) As MyErrorProvider
'get the containing form of the control
Dim form = control.GetContainerControl()
'use reflection to get to "components" field
Dim componentField = form.[GetType]().GetField("components", BindingFlags.NonPublic Or BindingFlags.Instance)
If componentField IsNot Nothing Then
'get the component collection from field
Dim components = componentField.GetValue(form)
'locate the ErrorProvider within the collection
Return TryCast(components, IContainer).Components.OfType(Of MyErrorProvider)().FirstOrDefault()
Else
Return Nothing
End If
End Function

Access VBA listing collection items in a class module

Although I'm reasonable experienced VBA developer, I have not had the need to use class modules or collections but thought this may be my opportunity to extend my knowledge.
In an application I have a number of forms which all have the same functionality and I now need to increase that functionality. Towards that, I am trying to reorder a collection in a class module, but get an error 91 - object variable or with block not set. The collection is created when I assign events to controls. The original code I obtained from here (Many thanks mwolfe) VBA - getting name of the label in mousemove event
and has been adapted to Access. The assignments of events works well and all the events work providing I am only doing something with that control such as change a background color, change size or location on the form.
The problem comes when I want to reorder it in the collection - with a view to having an impact on location in the form. However I am unable to access the collection itself in the first place.
The below is my latest attempt and the error occurs in the collcount Get indicated by asterisks (right at the bottom of the code block). I am using Count as a test. Once I understand what I am doing wrong I should be able to manipulate it as required.
mLabelColl returns a correct count before leaving the LabelsToTrack function, but is then not found in any other function.
As you will see from the commented out debug statements, I have tried making mLabelColl Private and Dim in the top declaration, using 'Debug.Print mLabelColl.Count' in the mousedown event and trying to create a different class to store the list of labels.
I feel I am missing something pretty simple but I'm at a loss as to what - can someone please put me out of my misery
Option Compare Database
Option Explicit
'weMouseMove class module:
Private WithEvents mLbl As Access.Label
Public mLabelColl As Collection
'Dim LblList as clLabels
Function LabelsToTrack(ParamArray labels() As Variant)
Set mLabelColl = New Collection 'assign a pointer
Dim i As Integer
For i = LBound(labels) To UBound(labels)
'Set mLabelColl = New Collection events not assigned if set here
Dim LblToTrack As weMouseMove 'needs to be declared here - why?
Set LblToTrack = New weMouseMove 'assign a pointer
Dim lbl As Access.Label
Set lbl = labels(i)
LblToTrack.TrackLabel lbl
mLabelColl.Add LblToTrack 'add to mlabelcoll collection
'Set LblList as New clLabels
'LblList.addLabel lbl
Next i
Debug.Print mLabelColl.Count 'returns correct number
Debug.Print dsform.countcoll '1 - incorrect
End Function
Sub TrackLabel(lbl As Access.Label)
Set mLbl = lbl
End Sub
Private Sub mLbl_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
Dim tLbl As Access.Label
'Debug.Print LblList.Count 'Compile error - Expected function or variable (Despite Count being an option
'Debug.Print mLabelColl.Count 'error 91
'Debug.Print LblList.CountLbls 'error 91
Debug.Print collCount
End Sub
Property Get collCount() As Integer
*collCount = mLabelColl.Count* 'error 91
End Property
In order to have all the weMouseMove objects reference the same collection in their mLabelColl pointer, a single line can achieve it:
LblToTrack.TrackLabel lbl
mLabelColl.Add LblToTrack
Set LblToTrack.mLabelColl = mLabelColl ' <-- Add this line.
But please be aware that this leads to a circular reference between the collection and its contained objects, a problem that is known to be a source of memory leaks, but this should not be an important issue in this case.

Convert Control Using Cast Not Working

i have a control named SuperValidator1 on every form with SuperValidator type. i want to find this control and enable it using its name because the name is consistent in all forms. so this is the code i came up with :
Dim validator As SuperValidator
Dim frm As Form = Me.ParentForm
Dim ctrl As Control()
ctrl = frm.Controls.Find("SuperValidator1", True)
Dim singleCtrl As Control = ctrl(0)
validator = TryCast(singleCtrl, SuperValidator) '< ERROR LINE
it throws editor error : Value of Type 'Control' cannot be converted to 'SuperValidator'
i tried CType and DirectCast but it is the same. according to this i should be able to cast any data type. what is wrong and what should i do ?
btw SuperValidator is from DevComponents.DotNetBar.Validator
thanks
Since SuperValidator is a component you must get it from your form's component collection. However at runtime components don't seem to inherit a name, so finding the exact one might be tricky.
As far as I know your only options are:
A) Get the first SuperValidator you can find, or
B) Match its properties (if possible).
Either way you do it you must iterate through the Me.components.Components collection:
Dim validator As SuperValidator = Nothing
For Each component In Me.components.Components
If component.GetType() Is GetType(SuperValidator) Then
validator = DirectCast(component, SuperValidator)
'Perform additional property checking here if you go with Option B.
End If
Next
Here is a test that uses a control I have on a form. Changed your logic slightly. Give it s try and see what results you have.
Dim validator As RichTextBox ' SuperValidator
Dim frm As Form = Me ' .ParentForm
Dim ctrl() As Control = frm.Controls.Find("RichTextBox1", True) ' ("SuperValidator1", True)
If ctrl.Length > 0 Then
validator = TryCast(ctrl(0), RichTextBox) ' , SuperValidator) < ERROR LINE
Else
Stop
End If

Property reflection - How to get value?

I have a need to get properties and their values dynamically. My code below is failing. Can someone give me a hand? I have tried numerous examples but nothing so far.
Dim seriesName As String = s.SeriesName
If model.Settings.ShowNativeLanguage Then
Dim propInfo As System.Reflection.PropertyInfo = s.GetType().GetProperty(model.Country)
seriesName = CStr(propInfo.GetValue(s, Nothing))
End If
This code produces the error "Object does not match target type."
The question was already answered here for C# Object does not match target type using C# Reflection
The solution is to change this line of your code:
seriesName = propInfo.GetValue(propInfo, Nothing).ToString()
to this:
seriesName = propInfo.GetValue(s, Nothing).ToString()
You need to pass the object of which you want to get the value. (More information in MSDN)
Update:
You should always check reflection results for Nothing values. So first store the output of propInfo.GetValue(s, Nothing) in a temporary variable and later on only call the ToString()-function if the object is not Nothing
Surely that should be:
... propInfo.GetValue(s) ...
Normally you must pass the object representing the this instance as the first parameter. You are getting that error because it's expecting the instance s, not a PropertyInfo instance.

Option Strict On disallows late binding

Can someone help me fix this error?
Option Strict On disallows late binding
Here's the code that's causing the error:
Dim SF6StdData As BindingSource = New BindingSource()
' ...
If StrComp(SF6StdData.Current("O2AreaCts").ToString, "") = 0 Then
AreaCts(3) = 0
Else
AreaCts(3) = Convert.ToDouble(SF6StdData.Current("O2AreaCts").ToString)
End If
I need to rewrite the code so it will not have any errors. I know I could fix this by setting Option Strict to Off in the project properties, but I really don't want to do that. Is there some other way?
Late binding is not allowed when Option Strict is on. If you need to perform late binding, the only options are either to use reflection or to shut off Option Strict. The one saving grace, however, is that you don't have to shut off Option Strict for the whole project. You can leave it on for the project and then just add the line Option Strict Off at the top of any code files where you need to perform late binding. It's not a great solution, but it's better than affecting the whole project.
Also, since the Option Strict placed at the top of a file applies just to that file, it doesn't even have to apply to an entire class. If you split your class into multiple Partial Class files, then you could have Option Strict set differently for each of those files. For instance, if you put most of your class in a file with Options Strict On, and then just put one method in a Partial Class in a separate file with Option Strict Off, then only that one method would be compiled loosely. The rest of the class would be compiled using the strict rules.
You need to make the BindingSource act as a strongly-typed data source. See the remarks in the documentation: http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.aspx
This is an old post, but I struggled with the error "Option Strict On disallows late binding". Maybe another answer will help someone else. The problem may be coming when you try to convert the data in your SF6StdData bindingsource to a string. You can probably solve the problem by defining a local variable with the desired type, and then using Ctype to extract the data into the correct type. Here's an example of how I solved a similar problem.
This code gave me the late-binding error:
Friend Function CountNumCheckedInGroupbox(ByVal gbox As GroupBox, ByRef nameschecked() As String) As Integer
Dim numchecked As Integer = 0
For Each ctrl In gbox.Controls
If TypeOf ctrl Is CheckBox Then
If ctrl.Checked = True Then
nameschecked(numchecked) = ctrl.Text
numchecked += 1
End If
End If
Next
Return numchecked
End Function
The late binding error occurred where I referenced "ctrl.Checked" and "ctrl.Text"
Instead of referencing "ctrl" directly, I defined a variable cbox that is typed as a Checkbox. Then I extracted the information from "ctrl" into cbox. Now the code does not show late-binding errors:
Friend Function CountNumCheckedInGroupbox(ByVal gbox As GroupBox, ByRef nameschecked() As String) As Integer
Dim numchecked As Integer = 0
Dim cbox As CheckBox
For Each ctrl In gbox.Controls
If TypeOf ctrl Is CheckBox Then
cbox = CType(ctrl, CheckBox)
If cbox.Checked = True Then
nameschecked(numchecked) = cbox.Text
numchecked += 1
End If
End If
Next
Return numchecked
End Function
If you declared AreaCts without a type, ex:
Dim AreaCts as Array
Try
Dim AreaCts() as Double
This fixed my late binding error.