.NET Questions and Solutions

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘property’

FIX: A NullReferenceException exception may occur when you set the Table property of a DataView object in a .NET Framework 2.0 application

Symptoms
Consider the following scenario in a Microsoft .NET Framework 2.0 application:You set a DataTable object for the Table property of a DataView object.The DataTable object that you use contains fewer rows than the DataTable object that was previously set for the Table property. The DataView object is bound to a DataGrid object.In this scenario, a NullReferenceException exception may occur.
Resolution
This problem occurs because the DataView object raises a ListChanged event before the DataView object has completed the transition to the new DataTable object.The DataView object tries to access the index for a DataRow object. However, that index is no longer valid because the Table property is set to a different DataTable object.

BUG: You receive a COMException exception when you pass late-bound parameters to methods of Office objects in Visual Basic 2005 or in Visual Basic .NET

Symptoms
In a Microsoft Visual Basic 2005 or Microsoft Visual Basic .NET application, when you pass late-bound parameters to methods of Microsoft Office objects, you may receive COMException exceptions. For example, when you pass the Name property of a late-bound UserProperty object to the UserProperties.Items method, you may receive the following error message:

An unhandled exception of type ‘System.Runtime.InteropServices.COMException’ occurred in microsoft.visualbasic.dll
Additional information: Property is read-only.Note Similar symptoms may also occur when you call methods of other Microsoft Office objects.
Resolution
If you pass a property of an object to a method by reference and the property has a set accessor, Visual Basic calls the set accessor to set the property to the value that the method returns. At compile time, if you pass a property of a late-bound object, the Visual Basic compiler cannot determine whether the property is passed by reference. Also, the Visual Basic compiler cannot determine whether the property has a set accessor. Therefore, the Visual Basic compiler permits the late binder to determine these details at run time.
However, if the relevant objects are Component Object Model (COM) objects at run time, the late binder cannot obtain sufficient information about these details. The late binder uses managed reflection to try to determine these details. The late binder assumes that such method calls involve a ByRef parameter and that the property that you pass has a set accessor. If the property that you pass does not have a set accessor, the Microsoft .NET Framework generates a MissingMethodException exception. The late binder handles the MissingMethodException exception.
The .NET Framework generates a MissingMethodException exception when the call to a method of a Microsoft Office object returns a HRESULT value of COR_E_MISSINGMETHOD. The Microsoft Office object is a COM object. Visual Basic .NET incorrectly assumes that all the methods of Microsoft Office objects return COR_E_MISSINGMETHOD if the property that you pass does not have a set accessor. However, not all the methods of Microsoft Office objects return COR_E_MISSINGMETHOD if the property that you pass does not have a set accessor. Therefore, the behavior that is mentioned in the “Symptoms” section of this article occurs if you pass a property that does not have a set accessor, such as the UserProperty.Name property, to a method.
Note This behavior does not occur if you use early-bound Microsoft Office objects.

BUG: You receive a “Type mismatch” error message when you assign a value type variable to a property through COM InterOp in Visual Basic .NET or in Visual Basic 2005

Symptoms
When you assign a value to a property of a Component Object Model (COM) object in .NET, you may receive the following error message when you run your application:

An unhandled exception of type ‘System.Runtime.InteropServices.COMException’ occurred in InterOpDemo.exe
Additional information: Type mismatch
Resolution
The problem occurs if all of the following conditions are true: You are using the COM object in early bound mode.The property in the COM object has both Set and Let methods.You are trying to pass a value type variable to that property. In early bound mode, Microsoft Visual Basic .NET and Visual Basic 2005 always call the Set method of the property if it is available. If you want to call the Let method, you must explicitly specify it.

BUG: You receive a “System.Runtime.InteropServices.COMException” message when you bind an ImageList ActiveX control to a CoolBar ActiveX control

Symptoms
In Microsoft Visual Studio 2005 or in Microsoft Visual Studio .NET, when you set the ImageList property of the CoolBar ActiveX control to the ImageList ActiveX control, and then you run the application, you receive the following exception:

An unhandled exception of type ‘System.Runtime.InteropServices.COMException’ occurred in axinterop.comctl3.dll
Additional information: Invalid property value. Note You cannot set the ImageList property of the CoolBar ActiveX control in the Properties window.
Resolution
Microsoft Visual Basic 2005, Microsoft Visual Basic .NET, and Microsoft Visual C# .NET interpret the ImageList property of the CoolBar ActiveX control as a read-only property. Therefore, Visual Basic 2005, Visual Basic .NET, and Visual C# .NET throw an exception when you set the ImageList property of the CoolBar ActiveX control to the ImageList ActiveX control.

ACC2000: How to Use Collections to Manage Class Objects in VBA

Symptoms
Advanced: Requires expert coding, interoperability, and multiuser skills.
This article shows you how to use collections in Visual Basic forApplications to manage references to class objects in Access 2000.This technique allows your class objects to persist, and enables you tocontrol the individual properties of those objects by using the familiarcollection syntax used in Microsoft Access for implementing Data AccessObjects (DAO) and other Microsoft Office Object models.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
Resolution
In order to use collections to manage class objects, you must do thefollowing:Create an instance of the classSet the properties and methods of the classAdd the class to a public collectionUnload the instance of the classYou might expect that unloading the instance of the class results in theclass being closed and terminated. However, the class object persistsbecause you add it to a collection, which then owns the reference to theclass. This is a very powerful technique that allows you to control objectreferences through a collection; the class object does not terminate untilyou remove it from the collection.
The following example creates a class object and a form object, and thenmanages both objects from a collection in a standard module.
Create a Class ModuleCreate a new database called ClassTest.mdb.On the Insert menu, click Class Module.Save the class module as clsTest.Type the following lines in the Declarations section:

Private This_ClassID As StringPrivate This_frm As New Form_frmTest Select Class in the Object box of the Module window. “Initialize” isautomatically selected in the Procedure box.Type the following procedure:

Private Sub Class_Initialize()On Local Error GoTo Class_Initialize_ErrDim Msg As StringThis_frm.Visible = TrueThis_ClassID = “Initialized”This_frm.Caption = This_ClassIDMsgBox “Class Initialized”, vbInformation, “Class Example”Class_Initialize_End:Exit SubClass_Initialize_Err:Msg = “Error #: ” & Format$(Err.Number) & vbCrLfMsg = Msg & Err.DescriptionErr.Raise vbObjectError, “clsTest.Initialize (Private)”, MsgResume Class_Initialize_EndEnd Sub On the Insert menu, click Procedure.In the Insert Procedure dialog box, type ClassID in the Name box andclick Property in the Type box. Then type the following procedures

Public Property Get ClassID() As VariantClassID = This_ClassIDEnd PropertyPublic Property Let ClassID(ByVal vNewValue As Variant)This_ClassID = vNewValueThis_frm.ClassID = This_ClassIDThis_frm.Caption = This_ClassIDEnd Property Save and close the clsTest class module.
Create a FormCreate the following form not based on any table or query in Designview:

Form: frmTest—————–Caption: TestForm With the form still open in Design view, click Code on the View menu.Type the following line in the Declarations section of the form’s classmodule:

Dim This_ClassID As String Add the following event procedure to the form’s Unload property:

Private Sub Form_Unload(Cancel As Integer)col.Remove This_ClassIDEnd Sub On the Insert menu, click Procedure.In the Insert Procedure dialog box, type ClassID in the Name box andclick Property in the Type box. Then type the following procedures. Notethat the Get ClassID() function and vNewValue variable in this exampleare dimensioned as String instead of the default, which is Variant:

Public Property Get ClassID() As StringClassID = This_ClassIDEnd PropertyPublic Property Let ClassID(ByVal vNewValue As String)This_ClassID = vNewValueEnd Property Save and close the frmTest form.
Create a Standard ModuleCreate a new standard module and save it as Module1.Type the following line in the Declarations section:

Public col As New Collection Type the following procedure:

Function CreateClassTest() As String’ Create an instance of the clsTest class module, which creates’ an instance of the frmTest form.Dim cls As New clsTest’ Create a unique identifier string and set it to the upper index’ of the Public col Collection plus one.Dim varClassId As StringvarClassId = “Key_” & CStr(col.Count + 1)’ Set the clsTest class module’s ClassID property to the value of’ varClassId, which in turn sets the frmTest.ClassId property to’ the same value. This is so the form has a method to track its’ relationship to the collection.cls.ClassID = varClassId’ Add the instance of the class object to the collection passing’ varClassId as the Key argument.col.Add cls, varClassIdMsgBox “Created New Collection Item: ” & varClassId, _vbInformation, “Class Example”‘ Unload the cls object variable.Set cls = Nothing’ Return the varClassId.CreateClassTest = varClassIdEnd Function Close and save the module.
Test the ExampleWhen you call the CreateClassTest() function multiple times, it opensmultiple instances of the frmTest form, each of which is unique and capableof managing itself and its participation in the public collection. Eachform is aware of its Key position in the collection, and each one removesitself from the collection when you close the form.
The following sample procedure creates three instances of the clsTestclass:Create a standard module and type the following procedure:

Function CreateThreeItems() As BooleanDim strKeys(1 To 3) As StringDim i As IntegerFor i = LBound(strKeys) To UBound(strKeys)strKeys(i) = CreateClassTest()Next iFor i = LBound(strKeys) To UBound(strKeys)MsgBox col.Item(strKeys(i)).ClassID, vbInformation, _”Class Test”Next iEnd Function To test this function, type the following line in the Immediate window,and then press ENTER:

?CreateThreeItems() Note that messages boxes are displayed each time the clsTest classmodule initializes, when each of three instances of the frmTest form iscreated, and again after all three instances of the form are open.

A memory leak may occur when you use data binding in Windows Presentation Foundation

Symptoms
When you use data binding in Microsoft Windows Presentation Foundation (WPF), a memory leak may occur.
Resolution
This issue occurs if the following conditions are true:A data-binding path refers to property P of object X.Object X contains a direct reference or an indirect reference to the target of the data-binding operation.Property P is accessed through a PropertyDescriptor object instead of a DependencyProperty objector a PropertyInfo object.