If have an array of Bytes in visual basic:
Dim data() As Byte = {0, 128, 0, 4, 9, 9, 32, 0, 0, 0, 0, 0, 0, 0, 0}
Is there a quick and easy way to insert two data values to the front of this array, and knock off the last two values?
Dim data() As Byte = {128, 128, 0, 128, 0, 4, 9, 9, 32, 0, 0, 0, 0, 0, 0}
Yes. First you need to move all of the existing values up 2 places in your array. Doing so will overwrite the last 2 values. You'll then want to set the first two values of your array.
'Move data up 2 spots. This needs to be done in reverse order so we don't lose any data
For i as Integer = data.Length - 1 To 2 Step -1
data(i) = data(i - 2)
End
'Assign the new values
data(0) = 128
data(1) = 128
You could load the bytes into a vb.net stack with a loop then use the stack.push then rewrite the data back
Related
I am trying to create two parallel reference planes equidistant from the origin. I am able to create the positive plane with:
Dim swDoc As SldWorks.ModelDoc2
Dim distance As Double
Dim BoolStatus As Boolean
Dim swLeftFace As SldWorks.RefPlane
Dim swRightFace As SldWorks.RefPlane
BoolStatus = swDoc.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
Set swRightFace = swDoc.FeatureManager.InsertRefPlane(swRefPlaneReferenceConstraints_e.swRefPlaneReferenceConstraint_Distance, distance, 0, 0, 0, 0)
However, I cannot create the negative plane. When "distance" is negative, it is evaluated as 0. This creates a plane coincident with the origin.
I have tried a few variations with "swRefPlaneReferenceConstraint_OptionFlip" constraint, but the documentation is very poor and it either:
Fails to create a plane
BoolStatus = swDoc.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
Set swLeftFace = swDoc.FeatureManager.InsertRefPlane(swRefPlaneReferenceConstraints_e.swRefPlaneReferenceConstraint_OptionFlip, distance, 0, 0, 0, 0)
or creates a plane with a positive offset, coincident with the first reference plane. This occurs for X=-1, X=0, and X=1.
BoolStatus = swDoc.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
Set swRightFace = swDoc.FeatureManager.InsertRefPlane(swRefPlaneReferenceConstraints_e.swRefPlaneReferenceConstraint_Distance, distance, 0, 0, 0, 0)
BoolStatus = swDoc.Extension.SelectByID2("Right Plane", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
Set swLeftFace = swDoc.FeatureManager.InsertRefPlane(swRefPlaneReferenceConstraints_e.swRefPlaneReferenceConstraint_Distance, distance, swRefPlaneReferenceConstraints_e.swRefPlaneReferenceConstraint_OptionFlip, X, 0, 0)
The options need to be added like this:
Set swRightFace = swDoc.FeatureManager.InsertRefPlane(swRefPlaneReferenceConstraints_e.swRefPlaneReferenceConstraint_Distance + swRefPlaneReferenceConstraints_e.swRefPlaneReferenceConstraint_OptionFlip, distance, 0, 0, 0, 0)
Dim LastNumber as Integer = 1
Dim num_0() as Integer = {1, 2, 3, 4, 5}
Dim num_1() as Integer = {6, 7, 8, 9, 10}
Dim num_2() as Integer = {20, 21, 14, 36, 0}
Dim y() As Integer
y(0) = num_0(2)
When I use this code it executes perfectly
But the problem is I want to change the "0" in num_0(2)
When I do...
y(0) = num_ & LastNumber & (2)
This doesnt work
Or
y(0) = ("num_" & LastNumber & "(2)")
This gives me an error that converting a string to an integer is not possible
My question is How can I replace the "0" in num_0(2) with the LastNumber integer variable... so it reads the "8" out of the array num_1(2)
You can use multidimensional array (AKA rectangular array):
Dim num As Integer(,) = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {20, 21, 14, 36, 0} }
y(0) = num(LastNumber, 2)
or jagged array (array of arrays) :
Dim num As Integer()() = { ({1, 2, 3, 4, 5}), ({6, 7, 8, 9, 10}), ({20, 21, 14, 36, 0}) }
y(0) = num(LastNumber)(2)
Is there a plugin that helps generate letters (A-Z) in excel as seen below? Or can we write some sort of VBA script to do this?
Stackoverflow is not a code-for-me service. Anyhow, the task looked interesting, and I have decided to code something about it:
Option Explicit
Public Sub WriteLetterA()
Dim varLetterA(8) As Variant
Dim lngColCounter As Long
Dim lngRowCounter As Long
Dim blnReverse As Boolean
Dim rngCell As Range
blnReverse = True
varLetterA(0) = Array(1, 1, 1, 0, 0, 1, 1, 1)
varLetterA(1) = Array(1, 0, 0, 0, 0, 0, 0, 1)
varLetterA(2) = Array(1, 0, 0, 1, 1, 0, 0, 1)
varLetterA(3) = Array(1, 0, 0, 1, 1, 0, 0, 1)
varLetterA(4) = Array(0, 0, 0, 1, 1, 0, 0, 0)
varLetterA(5) = Array(0, 0, 0, 0, 0, 0, 0, 0)
varLetterA(6) = Array(0, 0, 0, 0, 0, 0, 0, 0)
varLetterA(7) = Array(0, 0, 1, 1, 1, 1, 0, 0)
varLetterA(8) = Array(0, 0, 1, 1, 1, 1, 0, 0)
Cells(1, 1).Select
For lngRowCounter = 0 To UBound(varLetterA)
For lngColCounter = 0 To UBound(varLetterA(lngRowCounter))
Set rngCell = Cells(lngRowCounter + 1, lngColCounter + 1)
If varLetterA(lngRowCounter)(lngColCounter) Then
rngCell.Interior.Color = IIf(blnReverse, vbBlack, vbWhite)
Else
rngCell.Interior.Color = IIf(blnReverse, vbWhite, vbBlack)
End If
Next lngColCounter
Next lngRowCounter
End Sub
' Points for improvement - varLetterA in a separate class
' Refer to the sheet, do not assume it
' Pass the first cell as a reference
This is what you get:
blnReverse = False
blnReverse = True
Take a look at the points for improvement - they can be useful, if you decide to build the rest of the alphabet. Good luck.
I have 3 x one dimensional arrays of type integer, each array is the same length (between 5 - 20 million values) and I would like to sort them in parallel (ie keeping the relative position) by the second array, then the third array, then the first array. Does anyone have any ideas on the most efficient way to do this in vb.net?
If it helps, the first array is just a record of the initial position (the three arrays will be re-sorted to this order once various calculations are done). I need to sort them to determine the number of unique combinations of the second and third arrays (a combination is determined by the position in the array - in the example below the combinations are (0-1-4), (1-1-6) etc). Once that is determined I will resort them back based on the first array.
I looked at array.sort but that only covers 2 parallel arrays. I'm a bit wary of putting the values in tuples (or any other format), as it would (I assume - perhaps not) be a big overhead when converting 5 - 20 million records before processing.
For example:
Record Number Array: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Link Array: {1, 1, 2, 1, 1, 2, 2, 2, 1, 2}
Line Number Array: {4, 6, 3, 5, 6, 7, 3, 2, 3, 4}
Having sorted / ordered by the second, then third, then first array, the expected output would be:
Record Number (1st Array): {8, 0, 3, 1, 4, 7, 2, 6, 9, 5}
Link Array (2nd Array): {1, 1, 1, 1, 1, 2, 2, 2, 2, 2}
Line Number Array (3rd Array): {3, 4, 5, 6, 6, 2, 3, 3, 4, 7}
Array.sort only allows you to sort 2 arrays in parallel and i'm a little confused by the options available in LinQ.
Does anyone have any suggestions on the best way to solve this problem?
Cheers,
I think you can get a backup of your Link Array. Then sort Link Array with Record Array, then you can sort un-sorted Link Array with Line Number Array. Can you please try it?
You didn't say if you want efficient in terms of time or space, but this takes about 3 seconds to transfer 20 million records from arrays to a list, and then about 30 seconds to do the sort on them, using less than 1GB of RAM. On my computer.
Option Infer On
Module Module1
Class Grouped
Property RecNo As Integer
Property Link As Integer
Property LineNo As Integer
Public Overrides Function ToString() As String
Return String.Format("({0}, {1}, {2})", Me.RecNo, Me.Link, Me.LineNo)
End Function
End Class
Sub Main()
' First try with the test data to show the correct result is obtained
Dim recNos = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim links = {1, 1, 2, 1, 1, 2, 2, 2, 1, 2}
Dim lineNos = {4, 6, 3, 5, 6, 7, 3, 2, 3, 4}
' transfer the arrays to a List
Dim xs As New List(Of Grouped)
xs.Capacity = recNos.Length
For i = 0 To recNos.Length() - 1
xs.Add(New Grouped With {.RecNo = recNos(i), .Link = links(i), .LineNo = lineNos(i)})
Next
' sort the data
Dim ys = xs.OrderBy(Function(x) x.Link).ThenBy(Function(x) x.LineNo).ToList()
Console.WriteLine(String.Join(", ", ys))
' Now try with twenty million records
Dim rand = New Random()
Dim nRecs As Integer = 20000000
recNos = Enumerable.Range(0, nRecs - 1).ToArray()
ReDim links(nRecs - 1)
ReDim lineNos(nRecs - 1)
For i = 0 To nRecs - 1
links(i) = rand.Next(0, 9)
lineNos(i) = rand.Next(1, 9)
Next
Dim sw As New Stopwatch
sw.Start()
xs.Clear()
xs.Capacity = nRecs
For i = 0 To recNos.Length() - 1
xs.Add(New Grouped With {.RecNo = recNos(i), .Link = links(i), .LineNo = lineNos(i)})
Next
sw.Stop()
Console.WriteLine(sw.ElapsedMilliseconds.ToString())
sw.Restart()
ys = xs.OrderBy(Function(x) x.Link).ThenBy(Function(x) x.LineNo).ToList()
sw.Stop()
Console.WriteLine(sw.ElapsedMilliseconds.ToString())
Console.ReadLine()
End Sub
End Module
I need to take this string:
Dim tmpTry As String = "10, 20, 30, 40, 50, 52, 20, 20, 10, 35, 3, 8, 47, 7, 2, 5, 55, 8, 0, 0, 6, 55, 0, 2, 12, 0, 0, 21, 14, 0, 3"
And convert it to a double array:
Dim arrNumOfVisits As Double() = New Double(tmpTry) {}
How do i go about doing that?
FYI the arrNumOfVisits goes into a ParamArray System.Collections.IEnumerable()
David
Dim arrString As String() = tmpTry.Split(New Char() {" "C})
Dim arrNumOfVisits As Double() = New Double(arrString.Length) {}
Dim i As Integer = 0
While i < arrString.Length
arrNumOfVisits(i) = Double.Parse(arrString(i))
i += 1
End While
The above code will do the trick, using regEx on this would be overkill.
Never the less do try to learn the basic RegEx operations, here are my favorite cheat sheets:
http://regexlib.com/CheatSheet.aspx?AspxAutoDetectCookieSupport=1
http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/