How can I write enums with inline calculation of the powers? - vb.net

I have once seen a beautifully and easy to follow enum.
It was written in such a way that one didn't have to calculate the powers by himself (1, 2, 4, 8, 16, 32, 64, 128, etc.), but instead, one could simply write like 1, 2, 3, 4, 5, etc.
The following is surely not correct, but you can get the flavor:
Public Enum eSomething
NotDefined = 0 ^ 2
Max = 1 ^ 2
Jeff = 2 ^ 2
Lisa = 3 ^ 2
Donald = 4 ^ 2
etc...
Does anybody know how to write it correctly?
Thank you very much!

Does this work for you?
Public Enum eSomething
NotDefined = CInt(2 ^ 0)
Max = CInt(2 ^ 1)
Jeff = CInt(2 ^ 2)
Lisa = CInt(2 ^ 3)
Donald = CInt(2 ^ 4)
End Enum

I have this enum that I find a convenient starting place...
<Flags()> Public Enum Bits As Long
none = 0
B00 = 1L << 0 ' 1
B01 = 1L << 1 ' 2
B02 = 1L << 2 ' 4
B03 = 1L << 3 ' 8
B04 = 1L << 4 ' 16
B05 = 1L << 5 ' 32
B06 = 1L << 6 ' 64
B07 = 1L << 7 ' 128
B08 = 1L << 8 ' 256
B09 = 1L << 9 ' 512
B10 = 1L << 10 ' 1,024
B11 = 1L << 11 ' 2,048
B12 = 1L << 12 ' 4,096
B13 = 1L << 13 ' 8,192
B14 = 1L << 14 ' 16,384
B15 = 1L << 15 ' 32,768
B16 = 1L << 16 ' 65,536
B17 = 1L << 17 ' 131,072
B18 = 1L << 18 ' 262,144
B19 = 1L << 19 ' 524,288
B20 = 1L << 20 ' 1,048,576
B21 = 1L << 21 ' 2,097,152
B22 = 1L << 22 ' 4,194,304
B23 = 1L << 23 ' 8,388,608
B24 = 1L << 24 ' 16,777,216
B25 = 1L << 25 ' 33,554,432
B26 = 1L << 26 ' 67,108,864
B27 = 1L << 27 ' 134,217,728
B28 = 1L << 28 ' 268,435,456
B29 = 1L << 29 ' 536,870,912
B30 = 1L << 30 ' 1,073,741,824
B31 = 1L << 31 ' 2,147,483,648
B32 = 1L << 32 ' 4,294,967,296
B33 = 1L << 33 ' 8,589,934,592
B34 = 1L << 34 ' 17,179,869,184
B35 = 1L << 35 ' 34,359,738,368
B36 = 1L << 36 ' 68,719,476,736
B37 = 1L << 37 ' 137,438,953,472
B38 = 1L << 38 ' 274,877,906,944
B39 = 1L << 39 ' 549,755,813,888
B40 = 1L << 40 ' 1,099,511,627,776
B41 = 1L << 41 ' 2,199,023,255,552
B42 = 1L << 42 ' 4,398,046,511,104
B43 = 1L << 43 ' 8,796,093,022,208
B44 = 1L << 44 ' 17,592,186,044,416
B45 = 1L << 45 ' 35,184,372,088,832
B46 = 1L << 46 ' 70,368,744,177,664
B47 = 1L << 47 ' 140,737,488,355,328
B48 = 1L << 48 ' 281,474,976,710,656
B49 = 1L << 49 ' 562,949,953,421,312
B50 = 1L << 50 ' 1,125,899,906,842,624
B51 = 1L << 51 ' 2,251,799,813,685,248
B52 = 1L << 52 ' 4,503,599,627,370,496
B53 = 1L << 53 ' 9,007,199,254,740,992
B54 = 1L << 54 ' 18,014,398,509,481,984
B55 = 1L << 55 ' 36,028,797,018,963,968
B56 = 1L << 56 ' 72,057,594,037,927,936
B57 = 1L << 57 ' 144,115,188,075,855,872
B58 = 1L << 58 ' 288,230,376,151,711,744
B59 = 1L << 59 ' 576,460,752,303,423,488
B60 = 1L << 60 ' 1,152,921,504,606,846,976
B61 = 1L << 61 ' 2,305,843,009,213,693,952
B62 = 1L << 62 ' 4,611,686,018,427,387,904
B63 = 1L << 63 ' -9,223,372,036,854,775,808
End Enum

Related

Reading enum with hexademinal

Hi can someone give me a little help, this is my Enum code
Enum _WSC_SECURITY_PROVIDER
WSC_SECURITY_PROVIDER_FIREWALL = &H1
WSC_SECURITY_PROVIDER_AUTOUPDATE_SETTINGS = &H2
WSC_SECURITY_PROVIDER_ANTIVIRUS = &H4
WSC_SECURITY_PROVIDER_ANTISPYWARE = &H8
WSC_SECURITY_PROVIDER_INTERNET_SETTINGS = &H16
WSC_SECURITY_PROVIDER_USER_ACCOUNT_CONTROL = &H32
WSC_SECURITY_PROVIDER_SERVICE = &H64
WSC_SECURITY_PROVIDER_NONE = &H0
End Enum
Enum _WSC_SCANNER_SETTINGS
SCANNER_UNKNOWN = &H1
SCANNER_RUNNING = &H16
End Enum
Enum _WSC_UPTODATE
up_To_Date = &H0
too_Old = &H10
End Enum
So I heve this Hexadecimal value: 61100 that in Binary is: 1100001000100000000, and I want that my output be
WSC_SECURITY_PROVIDER_ANTIVIRUS + WSC_SECURITY_PROVIDER_AUTOUPDATE_SETTINGS + ???? + up_To_Date
Until now I only have this but it doesn't do what I want
Dim _security As _WSC_SECURITY_PROVIDER = hex.Substring(0, 2)
Dim _scanner As _WSC_SCANNER_SETTINGS = hex.Substring(2, 2)
Dim _uptodate As _WSC_UPTODATE = hex.Substring(4, 2)
Console.WriteLine("_security={0}, _scanner={1}, _uptodate={2}",
_security,
_scanner,
_uptodate
)
I will appreciate the hints.
If you are designing an enum that is meant to represent flags, individual bits, the items in the enum should be powers of two, hex 16 is three bits. I created this enum as a template so I wouldn't have to do a lot of typing. Just change the names.
<FlagsAttribute()> _
Public Enum bit As Integer
none = 0
s0 = 1 << 0
s1 = 1 << 1
s2 = 1 << 2
s3 = 1 << 3
s4 = 1 << 4
s5 = 1 << 5
s6 = 1 << 6
s7 = 1 << 7
s8 = 1 << 8
s9 = 1 << 9
s10 = 1 << 10
s11 = 1 << 11
s12 = 1 << 12
s13 = 1 << 13
s14 = 1 << 14
s15 = 1 << 15
s16 = 1 << 16
s17 = 1 << 17
s18 = 1 << 18
s19 = 1 << 19
s20 = 1 << 20
s21 = 1 << 21
s22 = 1 << 22
s23 = 1 << 23
s24 = 1 << 24
s25 = 1 << 25
s26 = 1 << 26
s27 = 1 << 27
s28 = 1 << 28
s29 = 1 << 29
s30 = 1 << 30
all = -1
End Enum
Normally I comment out unused items, but leave them.

Declare (automatically) variables in vba

Is it possible to declare (automatically) variables in vba and with increasing number?
I have to Dim 50 variables, like Var1 till Var50
Is it possible with vba code
All my efforts (for next with concatenate) didn't work
The result should be;
Dim Var1 as integer
Dim Var2 as integer
.
.
Dim Var50 as integer
Use
Dim Var(1 to 50) as integer
Then you can set values for each like so
Var(1) = 1
Var(2) = 2
Var(3) = 3
Var(4) = 4
Var(5) = 5
Var(6) = 6
Var(7) = 7
Var(8) = 8
Var(9) = 9
Var(10) = 10
Var(11) = 11
Var(12) = 12
Var(13) = 13
Var(14) = 14
Var(15) = 15
Var(16) = 16
Var(17) = 17
Var(18) = 18
Var(19) = 19
Var(20) = 20
Var(21) = 21
Var(22) = 22
Var(23) = 23
Var(24) = 24
Var(25) = 25
Var(26) = 26
Var(27) = 27
Var(28) = 28
Var(29) = 29
Var(30) = 30
Var(31) = 31
Var(32) = 32
Var(33) = 33
Var(34) = 34
Var(35) = 35
Var(36) = 36
Var(37) = 37
Var(38) = 38
Var(39) = 39
Var(40) = 40
Var(41) = 41
Var(42) = 42
Var(43) = 43
Var(44) = 44
Var(45) = 45
Var(46) = 46
Var(47) = 47
Var(48) = 48
Var(49) = 49
Var(50) = 50

project euler problem17 vb code bug

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
my answer is coming 21148. i cant find the mistake here is my code in vb
the answer should be 21124
Dim length As UInt32 = 11
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i = 1 To 999
If i / 10 < 1 Then
single_digit(i)
ElseIf i / 10 = 1 Then
ten()
ElseIf i / 10 > 1 And i / 10 < 2 Then
ele_twe_thi(i)
ElseIf i / 10 >= 2 And i / 10 < 10 Then
multiple_10(Math.Floor(i / 10) * 10)
single_digit(i Mod 10)
ElseIf i = 100 Then
single_digit(i / 100)
length += 7
ElseIf i Mod 100 >= 11 And i Mod 100 <= 19 Then
length += 10
single_digit(Math.Floor(i / 100))
ele_twe_thi(i Mod 100)
ElseIf i Mod 100 = 10 Then
length += 3
single_digit(Math.Floor(i / 100))
length += 10
Else
length += 10
single_digit(Math.Floor(i / 100))
multiple_10(Math.Floor((i Mod 100) / 10) * 10)
single_digit(i Mod 10)
End If
Next
MsgBox(length)
End Sub
Private Sub single_digit(num)
If num = 1 Then
length = length + 3
ElseIf num = 2 Then
length = length + 3
ElseIf num = 3 Then
length = length + 5
ElseIf num = 4 Then
length = length + 4
ElseIf num = 5 Then
length = length + 4
ElseIf num = 6 Then
length = length + 3
ElseIf num = 7 Then
length = length + 5
ElseIf num = 8 Then
length = length + 5
ElseIf num = 9 Then
length = length + 4
End If
End Sub
Private Sub ele_twe_thi(num)
If num = 11 Or num = 12 Then
length = length + 6
ElseIf num = 13 Or num = 14 Or num = 18 Or num = 19 Then
length = length + 8
ElseIf num = 17 Then
length = length + 9
ElseIf num = 15 Or num = 16 Then
length = length + 7
End If
End Sub
Private Sub multiple_10(num)
If num = 20 Then
length = length + 6
ElseIf num = 30 Then
length = length + 6
ElseIf num = 40 Then
length = length + 5
ElseIf num = 50 Then
length = length + 5
ElseIf num = 60 Then
length = length + 5
ElseIf num = 70 Then
length = length + 7
ElseIf num = 80 Then
length = length + 6
ElseIf num = 90 Then
length = length + 6
End If
End Sub
Private Sub ten()
length = length + 3
End Sub
every time the program reaches 100, 200, 300 etc. is it adding an "and" unnecessarily?

Base64 Encoding for file upload VB.Net

I have a project in VBA that does the base64 encoding and uploads the file (.xlsm).
VBA Code:
Option Explicit
Private Const clOneMask = 16515072 '000000 111111 111111 111111
Private Const clTwoMask = 258048 '111111 000000 111111 111111
Private Const clThreeMask = 4032 '111111 111111 000000 111111
Private Const clFourMask = 63 '111111 111111 111111 000000
Private Const clHighMask = 16711680 '11111111 00000000 00000000
Private Const clMidMask = 65280 '00000000 11111111 00000000
Private Const clLowMask = 255 '00000000 00000000 11111111
Private Const cl2Exp18 = 262144 '2 to the 18th power
Private Const cl2Exp12 = 4096 '2 to the 12th
Private Const cl2Exp6 = 64 '2 to the 6th
Private Const cl2Exp8 = 256 '2 to the 8th
Private Const cl2Exp16 = 65536 '2 to the 16th
Private cbTransTo(63) As Byte
Private cbTransFrom(255) As Byte
Private clPowers8(255) As Long
Private clPowers16(255) As Long
Private clPowers6(63) As Long
Private clPowers12(63) As Long
Private clPowers18(63) As Long
Private Sub Class_Initialize()
Dim lTemp As Long
For lTemp = 0 To 63 'Fill the translation table.
Select Case lTemp
Case 0 To 25
cbTransTo(lTemp) = 65 + lTemp 'A - Z
Case 26 To 51
cbTransTo(lTemp) = 71 + lTemp 'a - z
Case 52 To 61
cbTransTo(lTemp) = lTemp - 4 '1 - 0
Case 62
cbTransTo(lTemp) = 43 'Chr(43) = "+"
Case 63
cbTransTo(lTemp) = 47 'Chr(47) = "/"
End Select
Next lTemp
For lTemp = 0 To 255 'Fill the lookup tables.
clPowers8(lTemp) = lTemp * cl2Exp8
clPowers16(lTemp) = lTemp * cl2Exp16
Next lTemp
For lTemp = 0 To 63
clPowers6(lTemp) = lTemp * cl2Exp6
clPowers12(lTemp) = lTemp * cl2Exp12
clPowers18(lTemp) = lTemp * cl2Exp18
Next lTemp
For lTemp = 0 To 255 'Fill the translation table.
Select Case lTemp
Case 65 To 90
cbTransFrom(lTemp) = lTemp - 65 'A - Z
Case 97 To 122
cbTransFrom(lTemp) = lTemp - 71 'a - z
Case 48 To 57
cbTransFrom(lTemp) = lTemp + 4 '1 - 0
Case 43
cbTransFrom(lTemp) = 62 'Chr(43) = "+"
Case 47
cbTransFrom(lTemp) = 63 'Chr(47) = "/"
End Select
Next lTemp
End Sub
Public Function Encode(sString As String) As String
Dim bTrans(63) As Byte, bOut() As Byte, bIn() As Byte, lOutSize As Long
Dim lChar As Long, lTrip As Long, iPad As Integer, lLen As Long, lTemp As Long, lPos As Long
iPad = Len(sString) Mod 3 'See if the length is divisible by 3
If iPad Then 'If not, figure out the end pad and resize the input.
iPad = 3 - iPad
sString = sString & String(iPad, Chr(0))
End If
bIn = StrConv(sString, vbFromUnicode) 'Load the input string.
lLen = ((UBound(bIn) + 1) \ 3) * 4 'Length of resulting string.
lTemp = lLen \ 72 'Added space for vbCrLfs.
lOutSize = ((lTemp * 2) + lLen) - 1 'Calculate the size of the output buffer.
ReDim bOut(lOutSize) 'Make the output buffer.
lLen = 0 'Reusing this one, so reset it.
For lChar = LBound(bIn) To UBound(bIn) Step 3
lTrip = clPowers16(bIn(lChar)) + clPowers8(bIn(lChar + 1)) + bIn(lChar + 2) 'Combine the 3 bytes
lTemp = lTrip And clOneMask 'Mask for the first 6 bits
bOut(lPos) = cbTransTo(lTemp \ cl2Exp18) 'Shift it down to the low 6 bits and get the value
lTemp = lTrip And clTwoMask 'Mask for the second set.
bOut(lPos + 1) = cbTransTo(lTemp \ cl2Exp12) 'Shift it down and translate.
lTemp = lTrip And clThreeMask 'Mask for the third set.
bOut(lPos + 2) = cbTransTo(lTemp \ cl2Exp6) 'Shift it down and translate.
bOut(lPos + 3) = cbTransTo(lTrip And clFourMask) 'Mask for the low set.
If lLen = 68 Then 'Ready for a newline
bOut(lPos + 4) = 13 'Chr(13) = vbCr
bOut(lPos + 5) = 10 'Chr(10) = vbLf
lLen = 0 'Reset the counter
lPos = lPos + 6
Else
lLen = lLen + 4
lPos = lPos + 4
End If
Next lChar
If bOut(lOutSize) = 10 Then lOutSize = lOutSize - 2 'Shift the padding chars down if it ends with CrLf.
If iPad = 1 Then 'Add the padding chars if any.
bOut(lOutSize) = 61 'Chr(61) = "="
ElseIf iPad = 2 Then
bOut(lOutSize) = 61
bOut(lOutSize - 1) = 61
End If
Encode = StrConv(bOut, vbUnicode) 'Convert back to a string and return it.
End Function
The generated output is exactly like this:
Required Output
I am trying to achieve the same output in VB.Net but I am unable to do so. I tried using the following:
Dim testStr As String = "test.xlsm"
Dim byt As Byte() = Encoding.Default.GetBytes(testStr)
testStr = Convert.ToBase64String(byt)
Input String: http://txt.do/obff
But I am not getting exactly the same string back. The first line is different in both cases (VBA and VB.Net) and I'm not sure why.
Required Output (VBA and the weblink):
UEsDBBQABgAIAAAAIQCsmTVRbwEAAD8EAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAAAAAAAAAAAAAAAAAAAAAA
Generated Output (VB.Net):
UEsDBBQABgAIAAAAIQA/PzVRbwEAAD8EAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCA/BAIoPwACAAAAAAAAAAAAAAAAAAAAAAAAA
Could you please let me know where I am going wrong and how can I achieve the same in VB.Net?
I managed to solve the problem by getting the file directly, reading all its bytes and converting it to base64 string.
Dim dat As Byte() = IO.File.ReadAllBytes(FileName)
testStr = Convert.ToBase64String(dat, Base64FormattingOptions.InsertLineBreaks)

Making variables in a procedure global

I know the title seems pretty trivial but i have an issue and i can't seem to get round it, i have the following code within one sub procedure..
Dim X, Y As Integer
X = 32
Y = 285
X_coords(0) = X ' x1 (a1)
X_coords(1) = X - 13 ' x2 (a2)
X_coords(2) = X + 16 ' x3 (a3)
X_coords(3) = X + 63 ' x4 (b1)
X_coords(4) = X + 45 ' x5 (b2)
X_coords(5) = X + 74 ' x6 (b3)
X_coords(6) = X + 124 ' x7 (c1)
X_coords(7) = X + 103 ' x8 (c2)
X_coords(8) = X + 132 ' x9 (c3)
X_coords(9) = X + 63 ' x10 (b4)
X_coords(10) = X + 76 ' x11 (y3)
X_coords(11) = X + 21 ' x12 (n1)
X_coords(12) = X + 9 ' x13 (q1)
X_coords(13) = X + 65 ' x14 (q3)
X_coords(14) = X + 117 ' x14 (q5)
X_coords(15) = X + 87 ' x14 (q4)
X_coords(16) = X + 31 ' x13 (q2)
X_coords(17) = X + 139 ' x13 (q6)
X_coords(18) = X + 76 ' x13 (q6)
X_coords(19) = X + 129 ' x13 (q6)
Y_coords(0) = Y ' y1s1
Y_coords(1) = Y - 23 ' y1s2
Y_coords(2) = Y - 11.5 ' y1,2s
Y_coords(3) = Y - 47 ' y2s1
Y_coords(4) = Y - 70 ' y2s2
Y_coords(5) = Y - 59 ' y2,2s
Y_coords(6) = Y - 132 ' y4s1
Y_coords(10) = Y - 152 ' y4s2
Y_coords(7) = Y - 140 ' y4 30cred
Y_coords(8) = Y - 127 ' y4s1, 10cred
Y_coords(9) = Y - 105 ' y3
Dim a1 As New posData("a1", X_coords(0), Y_coords(0))
Dim a2 As New posData("a2", X_coords(1), Y_coords(0))
Dim a3 As New posData("a3", X_coords(2), Y_coords(0))
Dim a4 As New posData("a4", X_coords(0), Y_coords(0))
I do know how to create global variable within your class, I want to make a1, a2 etc global but they must take the values of the said X_coords and Y coords, i dont know how to make this global so that it will do so.. the values are blank because i cannot initialize them globally?
Any Help?
Declare a1, a2... as a global variable (whatever you mean by global). Then in the class' constructor you can assign a value to them:
Sub New()
'....
a1 = New ...
'....
If these variables are shared, you can use the shared constructor.
If the variables need to be recalculated, it probably would be a good idea to make them readonly properties.
The question would be global to what? The class itself?
Public Class MyClass
Private a1 As posData
Private a2 As posData
Private a3 As posData
Private a4 As posData
Public Function SetVariables()
'blah blah
a1 = New posData("a1", X_coords(0), Y_coords(0))
a2 = New posData("a2", X_coords(1), Y_coords(0))
a3 = New posData("a3", X_coords(2), Y_coords(0))
a4 = New posData("a4", X_coords(0), Y_coords(0))
End Function
End Class
The variables are available to any function in the current instance of that class.
Global to your application?
Public Class MyClass
Public Shared a1 As posData
Public Shared a2 As posData
Public Shared a3 As posData
Public Shared a4 As posData
Public Function SetVariables()
'blah blah
a1 = New posData("a1", X_coords(0), Y_coords(0))
a2 = New posData("a2", X_coords(1), Y_coords(0))
a3 = New posData("a3", X_coords(2), Y_coords(0))
a4 = New posData("a4", X_coords(0), Y_coords(0))
End Function
End Class
Now they are static and shared across all instances of the class.
Hope that helps.