How Can I Expose Private Fields using a Partial Class in VB.NET? - vb.net

This is my first post on Stack Overflow so please exuse (and feel free to point out) any n00b mistakes.
I am trying to implement transactions across multiple TableAdapters in VB.NET (using Visual Studio 2010) by extending the partial class as described in the following examples:
http://blah.winsmarts.com/2006/06/18/the-definitive-tableadapters--transactions-blog-post.aspx
madprops.org/blog/typed-datasets-and-sqltransaction/
stackoverflow.com/questions/2342289/net-tableadapter-to-dataadapter
However, when I attempt to expose any of the private fields created by the designer they are underlined in the editor with the following error:
'_adapter' is not declared. It may be inaccessible due to its protection
level.
Searching this site as well as google has not revealed anything useful, but perhpas I'm searching the wrong keywords.
Here is the code in MyDataset.vb
Partial Public Class MyTableAdapter
Public Property MyTransaction() As SqlTransaction
Get
Return _adapter.SelectCommand.Transaction
End Get
Set(ByVal value As SqlTransaction)
If _adapter Is Nothing Then
InitAdapter()
End If
Connection = value.Connection
_adapter.InsertCommand.Transaction = value
_adapter.UpdateCommand.Transaction = value
_adapter.DeleteCommand.Transaction = value
End Set
End Property
End Class
and here is the designer code:
Partial Public Class MyTableAdapter
Inherits Global.System.ComponentModel.Component
Private WithEvents _adapter As Global.System.Data.SqlClient.SqlDataAdapter
Private _connection As Global.System.Data.SqlClient.SqlConnection
Private _transaction As Global.System.Data.SqlClient.SqlTransaction
Private _commandCollection() As Global.System.Data.SqlClient.SqlCommand
Am I missing something, or is this not possible in VB (all of the examples I've seen are in C#)?
TIA for any help!
JE

Can you access any Public properties/methods from the Adapter class?
I think you may have created your partial class in the wrong namespace. It has to be in Namespace MyDataSetTableAdapters

Related

New to nUnit, getting error ... "is not accessible in this context because it is 'Public'"

I have written the following code as my first test in nUnit.
Public Class Tests
<TestFixture()>
Private Class TranslationTest
<Test()>
Private Sub LanguageTest()
Dim stringToTest As String = "Tower Count"
strLanguageText(stringToTest, LanguageIDs.English, 0)
Assert.Equals("Tower Count")
End Sub
End Class
End Class
Intellisense is saying that both the strLanguageText method and the LanguageIDs.Enlish enum are "not accessible in this context because it is 'Public'". I can understand something not being accessible because its modifier is Private, but why would having a modifier of Public prevent it from being accessible by the test?
Even though the error said "Public" when I looked at the Method being tested its modifier was Friend. When I assigned my test to the same namespace as the methods being tested it worked.
Update: According to O'Reilly's C# & VB.NET Conversion Pocket Reference, "Outer Classes at the IL level have only two possible scopes: private or public."
My amature guess is that this is why the error was reporting as 'Public'.

How to bind to Class using Generics in XAML

I have created a simple class like this one...
Public Class Localizer(Of T)
Public Shared ReadOnly Property DisplayName(ByVal propertyName As String) As String
Get
...
End Get
End Property
End Class
And I'm attempting to bind to it in XAML something like this...
Header="{x:Static loc:Localizer(Of AircraftReference).DisplayName [IsMilitary]}"
But this must not be the right syntax. This may not even be possible.
There are two problems here.
XAML support for generics is not complete; I don't think you can specify the type parameter in XAML.
However, there's an easy workaround for this problem:
Public Class Localizer(Of T)
Public Shared ReadOnly Property DisplayName As String
Get
...
End Get
End Property
End Class
Public Class AircraftReferenceLocalizer
Inherits Localizer(Of AircraftReference)
End Class
Now you can use:
Header="{x:Static loc:AircraftReferenceLocalizer.DisplayName}"
x:Static does not support parameterized properties, so you cannot pass the string "IsMilitary". I'm afraid you'll have to find a solution without x:Static. It might make sense to describe want problem you want to solve and ask for a solution in a new question.

Turn private variable to public in runtime?

please see image attached.
i want it to be
Public components As System.ComponentModel.lContainer
but every time I edit the form design, it always change back to its original code
Private components As System.ComponentModel.lContainer
You can have a public Property in your class that returns a private member, like yours. This code is generated by form designer and probably you can not change the behavior of this. Even if you could, I wouldn't suggest you to do this.
Public Class MyForm
Public Property Container As System.ComponentModel.IContainer
Get
Return Me.components
End Get
End Property
End Class
Cheers

Error using multiple Generic Classes in VB.Net

I tried to find a similar issue already posted but simply got confused with what i found.
I have a situation which involves the following objects:-
Reports contain parameters
Documents contain parameters
Report and Document Parameters are similar
Queries contain parameters
Reports, Documents and Queries all have similar Parameters
So I created the following class structure:-
Public MustInherit Class clsBaseCollection(Of TclsBaseChild As clsBase, TclsBaseParent As clsBase)
Public MustInherit Class clsParams(Of TclsParam As clsParam, TclsRootObject As clsRootObject)
Inherits clsBaseCollection(Of TclsParam, TclsRootObject)
Public MustInherit Class clsRepDocParams(Of TclsRepDocParam As clsRepDocParam, TclsReportDocument As clsReportDocument)
Inherits clsParams(Of TclsRepDocParam, TclsReportDocument)
Public Class clsReportParams
Inherits clsRepDocParams(Of clsReportParam, clsReport)
Public MustInherit Class clsReportDocument
MustOverride ReadOnly Property Parameters() As clsRepDocParams(Of clsRepDocParam,clsReportDocument)
Public Class clsReport
Inherits clsReportDocument
Private _Params As clsReportParams
Public Overrides ReadOnly Property Parameters() As clsReportParams
Get
If _Params Is Nothing Then
BeginUpdate()
_Params = New clsReportParams(Me)
EndUpdate()
End If
Return _Params
End Get
End Property
The last property produces the following error:-
‘Public Overrides ReadOnly Property Parameters As clsReportParams' cannot override 'Public MustOverride ReadOnly Property Parameters As clsRepDocParams(Of clsRepDocParam, clsReportDocument)' because they differ by their return types.
I cant see how this is so because I believe I have my classing levels correct!?
Apologies for the lack of tab formatting... im new to the stack overflow site and hopefully i will improve ;)
Cheers
Jeff
The MustOverride property is declared as
As clsRepDocParams(Of clsRepDocParam,clsReportDocument)
Your Override is simply declared as
As clsReportParams
It does not specify the base collection so they do not match. You should probably declare the override as
As clsRepDocParams(Of clsRepDocParam,clsReportDocument)
to match the base class.
It is worth noting that with every additional level of inheritance that you create, you need to be that much more careful to ensure that you design is done very carefully! If I was reviewing that code I would need to be convinced that the complexity was actually going to pay off over the lifetime of the project.

DisplayName DataAnnotations not working in WinForms 3.5 DataGridView

Ok, I'm completely at a loss here. I've used DataAnnotations attribute DisplayName successfully using MVC model binding, and even with WPF/Silverlight model binding and of course it works just fine, but now I'm on a project that I'm forced to use VB.NET 3.5 WinForms.
I have a Linq2Sql model and I created a partial class for one of my classes and included a MetadataType attribute to point to a metadata class. I added a DisplayName attribute to a property in the metadata class. I then bind my datagridview with an IQueryable(Of mydatatype), but the column name in the grid is the Property's name and not the DisplayName.
Am I missing something? Is there something else I need to do to get the datagridview to use the DisplayName?
In my Model class:
Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations
<MetadataType(GetType(vwREVIEW_ECRMetadata))> _
Partial Class vwREVIEW_ECR
Public Sub TestMethod()
End Sub
End Class
Public Class vwREVIEW_ECRMetadata
Private _ECRNumber As String
<DisplayName("ECR #")> _
Public Property ECRNumber() As String
Get
Return _ECRNumber
End Get
Set(ByVal value As String)
_ECRNumber = value
End Set
End Property
End Class
In my Repository class:
Public Function GetAllECRsForLookup() As IQueryable(Of vwREVIEW_ECR)
Return db.vwREVIEW_ECRs
End Function
In my Presenter class:
Public Sub GetData()
view.FillData(model.GetFilteredECRsForLookup())
End Sub
In my View:
Public Sub FillData(ByVal data As System.Linq.IQueryable(Of vwREVIEW_ECR)) Implements ILookupECRView.FillData
Me.uxECRData.DataSource = data
End Sub
Any help would be greatly appreciated! Thanks
Ok, so I found a solution to my problem. Didn't even think about it this way, but in ASP.NET & WPF, you get this behavior becuase of model binding behavior built in. WinForms has databinding as well, but isn't just there for your. Though I could've "bound" my datagridview to my linq2sql generated object in the runtime, which would've accomplished what I needed, I needed to do this at design time, so instead, I modified my MVP to use ViewModels where needed, and bind the datagrid to that object at runtime to get my column names to look the way I want. The ViewModel is hooked up to the model and can pass the real values to it.
I based this approach on this blog, though I didn't fully implement what he did:
http://aviadezra.blogspot.com/2009/08/mvp-mvvm-winforms-data-binding.html