in a class i declare a variable private :
Private test As String
then i right click do quick action and generate :
Public Property Test1 As String
Get
Return test
End Get
Set(value As String)
test = value
End Set
End Property
Do you know a way to modify the autogenerate code produce?
I want to add something like :
Public Property Test1 As String
Get
Return test
End Get
Set(value As String)
Tmp_Val = test
test = value
RaiseEvent VariableChanged(test, Tmp_Val, VarDesc("test"))
End Set
End Property
if somebody know a place where the "template" is stored?
Thanks.
quick actions are used for refactoring and pointers toward fixing errors in your code, not so much for automatic code generation. Unfortunately, I haven't found any way to edit these. You can however edit code snippets, but this is laborious to say the least. There is an extension you can install which is supposed to make snippet editing easier - I haven't tried it, but it comes via Microsoft#s Visial Studio Gallery -
Snippetizer
Related
After quite some frustrating research into VB auto properties. I decided to just ignore them. So I had this code.
Public Class Categoria
Private _nome As String
Public ReadOnly Property Nome As String
Get
Return _nome
End Get
End Property
Public Sub New(nome As String)
Me._nome = nome
End Sub
End Class
But Visual Studio, being helpful, suggested to use auto-properties. I agreed, I even tried that before but he offered help in the form of a click here and I will do it. So I did. And he did exactly the same code I was not having any luck with before, char by char.
Public Class Categoria
Public ReadOnly Property Nome As String
Public Sub New(nome As String)
Me.Nome = nome
End Sub
End Class
Can someone shed some light as to why the last piece of code fails to compile with error BC30126: 'ReadOnly' property must provide a 'Get'.?
Aparently VS is missing the same as I am, so I don't feel so stupid anymore.
As point out by #Heinzi and following a previous post he supplied.
How can I use the latest VB.NET language level in an ASP.NET web site project?
Updating the Microsoft.CodeDom.Providers.DotNetCompilerPlatform NuGet package fix it, in my dev machine at least.
Trying to convert some VBA code to VB.Net and getting hung up on some class issues. I have a main class: clsComputer. It has a subclass: clsHardDrive. Since a computer can have more than 1 hard drive, it's Get and Set properties look like this in clsComputer:
Private pHardDrive(8) As clsHardDrive
Public Property Get HardDrive(index As Integer) As clsHardDrive
If pHardDrive(index) Is Nothing Then Set pHardDrive(index) = New clsHardDrive
Set HardDrive = pHardDrive(index)
End Property
Public Property Set HardDrive(index As Integer, value As clsHardDrive)
pHardDrive(index) = value
End Property
This allows me to do something like this in code:
objComp.HardDrive(0).Size = "500"
objComp.HardDrive(1).Size = "1000"
However, I don't know how to convert this to VB.Net. I tried this:
Property HardDrive As HDD
Get (ByVal index As Integer)
Return pHardDrive(index)
End Get
Set (ByVal index As Integer, value As HDD)
pHardDrive(index) = value
End Set
End Property
But it gives a compile error: Property without a 'ReadOnly' or 'WriteOnly' specifier must provide both a 'Get' and a 'Set'. (BC30124)
Searching that error isn't very helpful: To correct this error make sure you include both a Get procedure and a Set procedure between the Property statement and the End Property statement. I have both Get and Set, and I think they are properly terminated. I've also searched for examples on using one class within another, but I didn't find anything useful.
How do I get the same functionality in VB.Net that I have in VBA? EG, how do I create the instances of HardDrive so that one computer object can have many hard drive objects?
Indexed properties still exist in VB.NET, just like classic COM-based VB.
You just have the syntax wrong. Indexed properties really just a special case of a parameterized property, so the index is taken as a parameter for the entire property, not the individual Get and Set statements.
Public Class Computer
Private m_hardDrives(8) As HDD
Public Property HardDrive(ByVal index As Integer) As HDD
Get
Return m_hardDrives(index)
End Get
Set(ByVal value As HDD)
m_hardDrives(index) = value
End Set
End Property
End Class
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.
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 am trying to check if an output column of my script component is NULL.
I tried to use the Row.Column_IsNull, but when I try to do the following:
If Row.Column_IsNull = True Then
// do something
End If
I get an error " Property Row.Column_IsNull is WriteOnly".
What the problem is
The key error in the above was is WriteOnly. When you are referencing columns in Script Components as Transformation, you can specify whether they are ReadOnly, ReadWrite.
When acting as Source, you don't have that option. It's WriteOnly (logically) and they don't even give you the option of the above dialog. So, when you're in your Source and attempt to access write only properties like the following code demonstrates, it breaks.
Public Overrides Sub CreateNewOutputRows()
Output0Buffer.AddRow()
' this is logically wrong
If Output0Buffer.Column_IsNull Then
End If
End Sub
The resolution is that you need to inspect whatever you are assigning into OutputBuffer0.Column prior to making the assignment (or create a separate boolean flag) to keep track of whether the current value was populated.
What the problem isn't
Keeping this here since I already ran down this rabbit hole
Since _IsNull is boolean, you can skip the explicit test and simply use
If Row.Column_IsNull Then
Originally, I had thought this was the classic C-like language issue of assignment (=) vs equality (==) but as #John Saunders was kind enough to point out, this was VB.
That said, the supplied code should work (it does for me).
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
Dim x As String
If Row.Src_IsNull = True Then
x = "" ' do nothing
End If
End Sub