I have just completed an MDI application. But now I'm trying to create a property that I'll be able to set and have it persist through all child forms. On the Parent form I have the following code.
Private userrole As String
Public Property userrole() As String
Get
Return userrole
End Get
Set(ByVal value As String)
userrole = value
End Set
End Property
But I can't set the property from any of the other forms. I'm relatively new to .NET previous apps have been in Access but management wants the access apps migrated to .NET
Turns out it was a simple mistake in spelling. userrole vs usrrole.
Using the above method I can call form1.userrole.ToString and get the value of the variable.
form1.userrole() = sets it.
I told you I was new. ;)
Thanks for letting me be a part of this forum.
Related
I'm using Linq to Sql and have a proper foreign key relationship setup in the underlying tables.
However, when I try to use navigation properties I have a subtle bug.
In the code sample below, when I put a watch on the PartDetails, I do get the fully populated parts. However, if I call the property on each part to check their values, the instance is now null.
I've hunted around for the last couple of hours to find an answer but so far coming up dry.
Can anyone clue me in as to why this is happening?
I'm on .net 4.6.1, Visual studio 2015 and Sql Server 2014.
I confess I couldn't find the correct place to fire off the DataLoadOptions but this seemed to work fine!
Partial Public Class LabourDetail
Private Sub OnCreated()
Dim db As New DataContext
Dim ds As DataLoadOptions = New DataLoadOptions()
ds.LoadWith(Function(c As LabourDetail) c.PartDetails)
db.LoadOptions = ds
End Sub
Public ReadOnly Property AnyPartsUnConsumed As Boolean
Get
'If I put a watch on the partdetails I do get a proper collection with proper instances.
Return PartDetails.Where(Function(p) p.PartsUnConsumed).Any
End Get
End Property
End Class
Partial Public Class PartDetail
'When we reach this point, the values in the instance are all Null / Default
Public Property PartsUnConsumed() As Boolean = _CheckPartsUnConsumed()
End Class
I'd be grateful for any assistance!
This Private Sub OnCreated() effectively doesn't do anything. It creates a context that immediately goes out of scope.
I assume there is some context that materializes LabourDetails from the database. That's the context to set the LoadOptions of.
How can i get the name of a local resource that has been assigned to a control property like BackgroundImage?
For example, i have a button and i have set the BackgroundImage property to a local resource image.
What i want, is at runtime to get the name of the local resource that has been assigned to BackgroundImage of that button.
If you look at your image:
you can see two things about the way your resources are handled. First, the return is a Bitmap, so once assigned to a button or whatever, you would have a very hard time determining what it is from the image data. The second thing is that the identifiers are actually Properties not just tokens or keys into a collection. The IDE generates these in your Resources.Designer.vb file to provide access to the various resources. Here is the interface to get the bitmap of the French Flag from the resource designer file:
Friend ReadOnly Property FRFlag() As System.Drawing.Bitmap
Get
Dim obj As Object = ResourceManager.GetObject("FRFlag", resourceCulture)
Return CType(obj,System.Drawing.Bitmap)
End Get
End Property
Yours will have things like Property error_button24BUTTON_DISABLED. Like any other property, the name of the property is not part of the return, just the data associated with them.
Since what really matters is the state of the button, not the image being shown, and that Enabled state is very easy to evaluate, not much is lost just using an if statement:
If thisButton.Enabled Then
thisButton.BackGroundImage = My.Resources...
Else
thisButton.BackGroundImage = My.Resources...
End If
You would have had to do something like this to convert "True" for Enabled to "BUTTON_ENABLED" to create the resource "key" if it actually worked the way you thought it did, or was intent on getting it via Reflection.
There are several alternatives. One might be to write an ExtenderProvider to provide various state images for the controls you are working with, subclass them or just use a local Dictionary/HashTable like an Extender would:
Friend Class ButtonImages
' ToDo: load these from My.Resources in the ctor for a given button
' ...
Private Property EnabledImage
Private Property DisabledImage
Public Function GetStateImage(b As Boolean) As Bitmap
If b Then
Return EnabledImage
Else
Return DisabledImage
End If
End Function
End Class
Private myBtnImgs As New Dictionary(of Button, ButtonImages)
thisButton.BackgroundImage = myBtnImgs(thisButton).GetStateImage(thisButton.Enabled)
It is more involved than a simple If statement, but comes close to what you seem to have been looking for.
I've been using classes for a while now, but I feel I may have been using them incorrectly.
When I create the properties for the class, I just use public variables so I end up with something like the following:
Class clsMyClass
Public Name As String
End Class
However, I've been reading some info on the net and they suggest that it should be set up in the following way:
Class clsMyClass
Private Name As String
Property UsersName() As String
Get
Return Name
End Get
Set(ByVal Value As String)
Name = Value
End Set
End Property
End Class
Is the way I'm doing it extremely incorrect? If so, why? I feel like the second method adds some sort of security but to be honest, it just looks like unnecessary code..?
One advantage of properties is that they let you customise the access to your private fields and enable you to do more so you can do the following (examples, it's not limited to that):
Make a property read-only for public access
Raise an even when a property is updated
Update other private fields when a property is updated
Validate the value that is being set
See below advantages of Properties over Variables from the C# in Depth article:
• There's more fine-grained access control with properties. Need it to be publicly gettable but really only want it set with protected access? No problem (from C# 2 onwards, at least).
• Want to break into the debugger whenever the value changes? Just add a breakpoint in the setter.
• Want to log all access? Just add logging to the getter.
• Properties are used for data binding; fields aren't.
Few other points:
1) You can also make properties read-only so no one from outside the class set the values but can fetch it.
2) You can do certain actions in the get and set. i.e. Append a prefix anytime set is called
3) You can also use auto-implemented property to minimize code like below:
Public Property Name As String
You are not doing anything wrong. Properties give you a shorthand basically, a syntactic sugar.
You can still use a backing private variable and do logic in get and set if you have to while using properties. Even better is the private/protected set or get, which is again another syntactic sugar so that you won't have to write all the code manually.
First of all, VB.NET allows you to use this syntax (called shorthand property declaration - I believe since VS 2010):
Public Property Name As String
Not so much different from this (called field declaration):
Public Name As String
Second, Microsoft data binding does not work well with fields. Try this example (see below).
Example. Put a listbox called ListBox1 (default name) and a button called Button1 on an empty form in an empty WinForms project. Replace your form code with this:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lst As New List(Of clsMyClass)
lst.Add(New clsMyClass)
ListBox1.ValueMember = "Name"
ListBox1.DisplayMember = "Name"
ListBox1.DataSource = lst
End Sub
End Class
Class clsMyClass
Public Property Name As String = "Hello"
End Class
Start the application and notice that a listbox is populated with one entry, Hello. This proves that binding worked correctly. Now replace your property declaration with a field declaration instead. Start your application one more time and notice that a listbox is showing your class type converted to String. It means that your field binding did not work, and default binding was used instead, where DisplayMember is assigned sort of classInstance.ToString().
If you are curious to learn more about what happens behind the scenes, you can put a breakpoint on .DataSource assignment, and see how DisplayMember gets reset or keeps its value depending on whether you are using fields or properties.
I have 20+ MDI forms with consistently named Public variables. When the child form closes a method on the MDI Parent is called passing Me as a generic form type. How can I access the public variables by name via the Form reference? I only need to read the variables. Of course the Variables() method does not exist...
Public Sub CleanupForm(ByVal frm As Form)
Dim sTable_Name As String = frm.Variables("TABLE_NAME") ' Public at form level
Dim cLock As clsRecLocks
cLock = frm.Variables("Rec_Lock")
cLock.DeleteThisLock()
'..
I've seen some posts on similar requests but most start out with "don't do it that way..." then go off in the weeds not answering the question. I concede it is poor design. I can't change all the calling forms in the short term so I need to use this approach.
VS2010, VB.Net, Win Forms, .Net 2.0
I was able to get to a simple variable using CallByName:
Try
Dim s As String = CallByName(frm, "TABLE_NAME", CallType.Get)
Stop
Catch ex As Exception
MsgBox(ex.Message)
End Try
On to the class object. Perhaps I can add a default Get for the class that returns the ID I need.
Default property won't work as the Locks object was not declared Public - at least for the CallByName() approach.
Can Reflection get to form level variables not declared Public? Seems like a security issue, but...
Can I get a "Parent" reference in the instantiated Locks class? i.e. A reference to the form that established the Locks object? I can change the clsRecLocks() class.
I found a property I could get to that told me the form was "read-only" and I can use that tidbit to delete the correct (or more correct - still not 100%) lock record. So the bug is 90% fixed. I think I need update all the forms with code that records the info I need to get to 100%.
Thanks to all!
Poor design but you can do this:
Public Sub CleanupForm(ByVal frm As Form)
Dim frmTest as object = frm
? = frmTest.TABLE_NAME
? = frmTest.Rec_Lock
End Sub
This will compile and if the variables exist, it will return them but if not, you get an error.
Converting to an interface after the fact is not that hard, you should do it now rather than later.
I created two user controls with different user interfaces. Depending on a selection the user makes, one of these interfaces will be used in my class. Since I don't know until after the user makes a selection, I cannot declare the user control ahead of time so I created a public variable to later assign the correct user control to.
The error occurs when I try to access a control (textbox) on the user control. However, if I declare the user control without assigning it to the public variable, then I don't get an error. Also, if I were to assign the user control to the public variable as its being declared then I don't get an error either. I really do need to be able to pick between the two user controls though. I don't know what to do. Am I missing something? I appreciate any help.
Public Class VesselData
Public RCAVesselData
Public AOLVesselData
Public Sub New()
If Main.UserSelectedModule = "Arrival on Location" Then
OperatorView = New AOLVesselData 'User Control 1
ElseIf Main.UserSelectedModule = "Running Conventional Anchors" Then
OperatorView = New RCAVesselData 'User Control 2
End If
OperatorView.txtDistanceToFairlead.text = "A" 'THROWS MissingMemberException - Public member 'txtDistanceToFairlead' on type 'AOLVesselData' not found.
Dim Test as New AOLVesselData
Test.txtDistanceToFairlead.text = "A" 'DOES NOT THROW EXCEPTION
End Sub
The problem is that you are accessing a member that doesn't exist. From the code you have posted, of the AOLVesselData and RCAVesselData classes, it looks like the RCAVesselData class does not have a txtDistanceToFairlead member.
If you have a common set of methods/properties you expect both user controls to expose, refactor them into an Interface and have both user controls implement that Interface. That will make it easy to use them interchangeably.
Try using Shared instead of Public when you declare OperatorView. And, like tcarvin said, you might want to turn "Explicit" on (project compile options) or use "Option Explicit".