I have an array called lblTables which contains 21 lables of my form. This works perfectly fine. The code for it is below:
Public lblTables() As Label
Public Sub New()
InitializeComponent()
lblTables = New Label() {Label2, table1, table2, table3, table4, table5, table6, table7, table8, table9, table10,
table11, table12, table13, table14, table15, table16, table17, table18, table19, table20}
End Sub
I need to create another array called lblStartTimes which will contain another 21 labels. So i proceeded as follows:
Private lblStartTimes() As Label
Public Sub New()
InitializeComponent()
lblStartTimes = New Label() {startTime1, startTime2, startTime3, startTime4, startTime5, startTime6,
startTime7, startTime8, startTime9, startTime10, startTime11, startTime12, startTime13, startTime14,
startTime15, startTime16, startTime17, startTime18, startTime19, startTime20}
End Sub
But when i tried to do that i got an error saying Public Sub New() has multiple definitions with identical signatures. So after doing a research i found the easiest way of doing this would be as follows:
Public lblStartTimes() As Label = {Label3, startTime1, startTime2, startTime3, startTime4, startTime5, startTime6,
startTime7, startTime8, startTime9, startTime10, startTime11, startTime12, startTime13, startTime14,
startTime15, startTime16, startTime17, startTime18, startTime19, startTime20}
but when i use this method i'm constantly receiving a System.NullReferenceException error. i need to declare lblStartTimes the same OR similar way i declared lblTables array without getting the multiple definitions error. How can i achieve this?
Like already suggested in the comments, you are allowed to write multiple lines in the constructor. Just move the second array initialization code to the first constructor:
Public lblTables() As Label
Public lblStartTimes() As Label
Public Sub New()
InitializeComponent()
lblTables = New Label() {Label2, table1, table2, table3, table4, table5, table6, table7, table8, table9, table10,
table11, table12, table13, table14, table15, table16, table17, table18, table19, table20}
lblStartTimes = New Label() {startTime1, startTime2, startTime3, startTime4, startTime5, startTime6,
startTime7, startTime8, startTime9, startTime10, startTime11, startTime12, startTime13, startTime14,
startTime15, startTime16, startTime17, startTime18, startTime19, startTime20}
End Sub
Related
I'm trying to find an alternative way to solve the problem I'm stuck on here. I'm using MSTest to select one of a set of arrays of bytes to pass to a function under test.
I'm trying this approach as I haven't been able to get MSTest working directly passing an array of bytes to the test function.
I want to set up a Private ReadOnly jagged array of arrays of Bytes (TestMsgs) as part of my test class to allow the test subroutine to access elements one by one. Currently I'm getting error BC30201 "Expression Expected" as below. Something is missing in my initialisation, but I can't find any example on how to initialise this jagged array.
Public Class DecoderTests
Private ReadOnly TestMsgs As Byte()() = New Byte(2)() {
New Byte() {&HA1, &HB2, &HC3}, 'Test array should Pass
New Byte() {&HA2, &HB3}, 'Test array should Fail
} <========= Error BC30201 Here
Private DecoderInstance
Here is the full code of my test (Simplified to debug the original issue)
Test Class
Imports System.Text
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Namespace TestDecoder.Tests
<TestClass>
Public Class DecoderTests
Private ReadOnly TestMsgs As Byte()() = New Byte(2)() {
New Byte() {&HA1, &HB2, &HC3}, 'Test array should Pass
New Byte() {&HA2, &HB3}, 'Test array should Fail
}
Private DecoderInstance
<DataTestMethod>
<DataRow(0)>
<DataRow(1)>
Public Sub ParseTestData(message_number)
Dim result As Boolean
DecoderInstance = New Decoder()
result = DecoderInstance.parse(TestMsgs(message_number)(0))
Assert.IsTrue(result, "Failed the dummy test")
End Sub
End Class
End Namespace
Simplified Class under test:
Imports Microsoft.VisualBasic
Public Class Decoder
Function parse(rxchar As Byte) As Boolean
Return rxchar = &H41
End Function
End Class
I just tried this code and there were no errors:
Private ReadOnly TestMsgs As Byte()() = {
New Byte() {&HA1, &HB2, &HC3},
New Byte() {&HA2, &HB3}
}
I think the issue with your original code was that you were specifying the size of the array explicitly and also initialising it, therefore specifying the size of the array implicitly. I just tested this code and it worked too:
Private ReadOnly TestMsgs As Byte()() = New Byte()() {
New Byte() {&HA1, &HB2, &HC3},
New Byte() {&HA2, &HB3}
}
Basic arrays question:
I have declared a public array and initialized it with values. In another private sub, I want to fill that public array with values. What is the easiest way to do it?
The double variables in the array are also declared public. The following code is the array created and initialized in a public module. In the private sub, I am changing the values of the double variables (ch0LowLmt, ch1LowLmt, etc, etc) and now I just simply want to reinitialize the array with the new values. What is the simplest way to do it?
Public AlarmLowLimit() As Double = New Double(15) {Ch0LowLmt, Ch1LowLmt, Ch2LowLmt, Ch3LowLmt, Ch4LowLmt, Ch5LowLmt, Ch6LowLmt, Ch7LowLmt, Ch8LowLmt, Ch9LowLmt, _
Ch10LowLmt, Ch11LowLmt, Ch12LowLmt, Ch13LowLmt, Ch14LowLmt, Ch15LowLmt}
Because Double is a value type, you do need to change it manually in the way you are working.
Another way, is to encapsulate the double var inside a class, which can be put inside your array, so it will be a reference type.
In this case you'd just update your new reference type objects in the private function you have mentioned. They will also change in your array.
See a nice solution here.
And the code example in Vb.Net:
Public Class MyObjectWithADouble
Public Property Element() As Double
Get
Return m_Element
End Get
Set
m_Element = Value
End Set
End Property
Private m_Element As Double
' property is optional, but preferred.
End Class
Dim obj = New MyObjectWithADouble()
obj.Element = 5.0
I have forgotten how to make an array or whatever out of 8 fields. It looks something like Array.Subcategory[x] afterwards but i've forgotten what its called and how to do it.
It needs to be 1 array with multiple entries but each entry have 8 sub entries.
So for a group of people
1 array entry will contain:
Name
Eye colour
Haircolour
Age
Balls
Smell
???
One array will contain all this data for each array entry.
cheers
You could define a class in VB.NET like this:
public class Entry
public Name as String
public EyeColour as String
public Haircolour as String
public Age as Integer
public Balls as Integer
public Smell as String
end class
Then create a List or Array holding entries like this:
public ListOfEntries as List(of Entry) = new List(of Entry)
public ArrayOfEntries(10) as Entry
Use it like this:
dim e as Entry = new Entry
e.Name = "Test"
e.EyeColour = "Blue"
' add new object to list
ListOfEntries.Add(e)
' add new object at position 0
ArrayOfEntries(0) = e
Another possibility would be to use a Structure (used-defined data type):
' a record of data
public structure Entry
public Name as String
public EyeColour as Integer
'...
end structure
' array of entries
public Entries(10) as Entry
' usage like in the class example
dim e = new Entry
e.Name = "Test"
e.EyeColour = 5
Entries(0) = e
You can do it with types also, which avoids some of the initialization hassels with classes in arrays:
Type foo
Name as String
EyeColour as Integer
...
End Type
Dim bar() As foo
I need to get all of the subdocuments array from the Courses Class where the User.UserId = whatever and Courses.Status=active
Public Class User
Public Property UserId As String 'this is unique so i would like to index this, unless you think otherwise
Public Property CourseData() As List(Of Courses) '
Public Property Groups As List(Of String)
Public Property BU As List(Of String)
End Class
Public Class Courses
Public Property id As String 'this can be dynamic
Public Property description As String
Public Property CompletionDate As String
Public Property Hours As String
Public Property Status As String
End Class
Using vb.net , I tried a few ways, I only want the courses returned that have a Status="Active" to be dumped into Ienumberable
I tried (_users is a collection of User) (_uid is a variable passed into it)
Return _users.FindAs(Of User)(Query.And(query.EQ("LearningHours.Status", "Active"), (Query.EQ("UserId", _uid))))
Return _users.FindAs(Of User)(Query.And(query.EQ("LearningHours.Status", "Active"), (Query.EQ("UserId", _uid)))).SetFields("Courses", "1")
Return _users.FindAs(Of Courses)(Query.And(query.EQ("LearningHours.Status", "Active"), (Query.EQ("UserId", _uid))))
Return _users.FindAs(Of Courses)(Query.And(query.EQ("LearningHours.Status", "Active"), (Query.EQ("UserId", _uid)))).SetFields("Courses", "1")
None seem to work, they usually come back with the fields from Class User or both Class User and Class Course, but the Course fields are blank
I even am trying linq.. this works - but only returns 1 row result
Dim uc = From _u In _users.AsQueryable(Of User)()
Where _u.userid = _userid _
Select _
CourseID = _u.Courses.Where(Function(c) c.State = "Submitted").Select(Function(c) c.CourseId)(0), _
CourseDescription = _u.Courses.Where(Function(c) c.State = "Submitted").Select(Function(c) c.CourseDescription)(0)
Seems easy enough to do, just cant get it
Got It, I think I was over thinking it
Once I declare an instance of the class, I can iterate through the subdocument
Dim _u as new User
For Each c In _user.Courses.Where(Function(cs) cs.Status= "Active").Select(Function(cs) cs)
console.writeline(c.id & c.description & "so on...")
Next
I understand that List, Array, Object etc. types "copied" by reference. However my natural and ordinary intend is to just have a "copy" of it in this context where I intentionally use ReadOnly instead of Read/Write property. In below sample the ReadOnly 'Extensions' property get change through 'm_extensions' reference change. Regardless, I think this behavior is incorrect and I have to do extra work to prevent ReadOnly properties from being overwritten. Is there any built in keyword to use for 'm_extensions' value protection?
Public Classs A
' more properties and methods here...
Private m_extensions() As String = {"*.abc", "*.def"}
Public ReadOnly Property Extensions() As String()
Get
Return m_extensions
End Get
End Property
End Class
Public Classs B
' more stuff here...
Private Function BuildFilter() As String
Dim l() As String = A.Extensions
Dim s As String = String.Empty
For m As Integer = 0 To l.Length - 1
Select Case l(m).ToLower
Case "*.*" : s = "All Files"
Case "*.abc" : s = "ABC File"
Case "*.def" : s = "DEF File"
Case Else : s = "XYZ File " + m.ToString
End Select
l(m) = String.Format("{1} ({0})|{0}", l(m), s)
Next
Return String.Join("|", l)
End Function
End Class
Readonly modifier means that anything using the property cannot change the reference that you protected this way (e.g. cannot set it to Nothing). It doesn't prevent changing the values in the array returned from that property.
One way around it could be to copy the array inside the property. This will prevent modifications of the original array:
Public ReadOnly Property Extensions() As String()
Get
Return m_extensions.Clone()
End Get
End Property