Check if integer is valid bitwise enum value - vb.net

is there a way to check if an integer is a valid value for an enum even if the value is bitwise?
Example:
Public Enum eFlags As Integer
None = 0
First = 1
Second = 2
Third = 4
End Enum
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim x As eFlags = eFlags.Second Or eFlags.Third
Dim y As Integer = 5
Dim res = x.GetType.IsEnum AndAlso [Enum].IsDefined(x.GetType, y)
End Sub
This returns false. I want to set the value of a property using reflection where the property is an enum and the value is integer...

Maybe this will help
<Flags> Public Enum eFlags As Integer
None = 0
First = 1
Second = 2
Third = 4
'note: flags added above will have to be Or'ed in All
All = First Or Second Or Third '<<<<<<<<<
End Enum
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim x As Integer = 14
For y As Integer = 6 To 13
Dim res As eFlags = CType(x And y, eFlags)
If eFlags.All.HasFlag(res) Then
Stop
Else
Stop
End If
Next
End Sub

Related

Fibonacci Sequence Visual Basic

I have a quick question about another Visual Basic assignment I'm working on. I have all the code and everything has gone smoothly so far. The app is meant to display the first 100 Fibonacci numbers in a list box, adding the two previously displayed numbers to get the next in a loop. The only problem is that when I hit the button to display the code, the loop continues, and doesn't just stop at 100 numbers. Where did I go wrong?
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim dblA As Double = 0
Dim dblB As Double = 1
Dim dblC As Double
Dim intCounter As Integer
lstSequence.Items.Add(dblA.ToString)
lstSequence.Items.Add(dblB.ToString)
For intCounter = 1 To 100
dblC = dblA + dblB
dblA = dblB
dblB = dblC
lstSequence.Items.Add(dblC.ToString)
Next
End Sub
I just tried this. It works fine.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a As Integer = 0
Dim b As Integer = 1
Dim fib As Integer
Dim userinput, i As Integer
userinput = InputBox("how many interations?")
i = userinput
ListView1.Items.Add(1)
Do
fib = a + b
a = b
b = fib
ListView1.Items.Add(fib)
i = i + 1
Loop While fib < i
End Sub
End Class

Visual Studio 2017 Global Declaration

Here I want to generate a number (n) of random number into a listbox and then using another listbox to show the generated random number in accending order.
Since it is separated into two private class, i tried to use public shared for my array mark() but it doesn't seems to work.
This is my complete code.
I have no issue generating the random number into my array mark() as i declared it in the private sub.
But when I do the sorting using another button, it shows error that I did not declare my array. If I re-declare my array, the number that i stored in it will be gone and the sorting turns into all 0.
Any idea how?
Public Class Form1
Dim n As Integer
Public Shared mark() As Integer
Dim i As Integer
Dim temp As Integer
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim i As Integer
n = Val(TextBox2.Text)
TextBox2.Text = ""
ListBox1.Items.Clear()
i = 0
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Clear()
Dim mark(n - 1) As Integer
For i = 0 To n - 1
mark(i) = Format("#", Rnd() * 100)
ListBox1.Items.Add(mark(i))
Next
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
ListBox2.Items.Clear()
For i = 0 To n - 1
For j = 0 To n - 2
If mark(j) > mark(j + 1) Then
temp = mark(j)
mark(j) = mark(j + 1)
mark(j + 1) = temp
End If
Next
Next
For k = 0 To n - 1
ListBox2.Items.Add(mark(k))
Next
End Sub
End Class

Vb Write a program to print multiples of 2 and 3

For my class, i need to write a program to find multiples of 2 and 3. The code i have would get me multiples of any number inputted into the program. My problem is that nothing is showing up in the message box that i've created and i don't know why. here's the code.
Public Class form1
Private Sub Button1_Click(ByVal Sender)
Dim Number1 As Integer
Dim Number2 As Integer
Dim Multiplier As Integer
Dim Answer As Integer
Dim i As Integer
Number1 = Val(TextBox1.Text)
Number2 = Val(TextBox2.Text)
Multiplier = 1
Do While Multiplier <= 10
For i = Number1 To Number2
Answer = i * Multiplier
ListBox1.Items.Add(i & "*" & Multiplier & "=" & Answer)
Next i
Multiplier = Multiplier + 1
Loop
End Sub
End Class
Any help at all would be appreciated.
I have not tested it but I think, this is what you looking for - all numbers that can be divided by 3 and 2 using multipliers from 1 to 10 over the range of numbers in your text boxes. In your code, I don't see where you weeding out your numbers that can be divided by 2 and 3
Private Sub Button1_Click(ByVal sender as Object, ByVal e as EventArgs) Handles Button1.Click
Dim num1 As Integer = Integer.Parse(TextBox1.Text)
Dim num2 As Integer = Integer.Parse(TextBox2.Text)
' may be need to check num2 > num1
Dim sum As Integer
For mult as Integer = 1 to 10
For i as integer = num1 To num2
total = i * mult
If sum Mod 2 = 0 OrElse sum Mod 3 = 0 Then
ListBox1.Items.Add(i.ToString() & " * " & mult & " = " & sum.ToString())
End If
Next i
Next
End Sub
This is my best guess as to what you are wanting. You said you were wanting multiples of 2 and 3 for any numbers given to the program, that that's what this does. If you wanted multiples of anything else, just add onto the part inside the {} in the coefficients declaration. Instead of using text boxes for input, I suggest using a NumericUpDowninstead; the GUI will be more intuitive to the end user.
Imports System.Text
Public Class Form1
Private Property maxNumb As Integer
Private Property minNumb As Integer
Private coefficients() As Integer = {2, 3}
Private Sub Button1_Click(sender As Button, e As EventArgs) Handles Button1.Click
Dim sb As New StringBuilder
For i = Me.minNumb To maxNumb Step 1
For Each coef As Integer In coefficients
sb.Append(i & " * ").Append(coef).Append(" = ").Append(i * coef)
Me.ListBox1.Items.Add(sb.ToString)
sb.Clear()
Next
Next
Me.ListBox1.Refresh()
End Sub
Private Sub NumericUpDown_ValueChanged(sender As NumericUpDown, e As EventArgs) Handles min_NumericUpDown1.ValueChanged, max_NumericUpDown2.ValueChanged
If sender.Name.Contains("max") Then
Me.maxNumb = sender.Value
Else
Me.minNumb = sender.Value
End If
End Sub
End Class

Index was outside the bounds of the array - VB

Public Class Form1
Dim a As Integer
Dim b As Integer
Dim c As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If CheckBox1.Checked = True Then
a += 1
Else : a += 0
End If
If CheckBox2.Checked = True Then
b += 1
Else : b += 0
End If
If CheckBox3.Checked = True Then
c += 1
Else : c += 0
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim max As Integer = 0
Dim d() As Integer = {a, b, c}
Dim f() As String = {"ch1", "ch2", "ch3"}
For i As Integer = 0 To 2
If max < d(i) Then
max = d(i)
Else : max = max
End If
Next
Label1.Text = f(max)
End Sub
End Class
So, in summary you're using checkboxes to increment numbers every time Button1 is clicked. These values are stored in variables a, b and c, which when Button2 is clicked, are put into an integer array. From the looks of your code you're then trying to find which one of these is the largest value and display a value from another array based off that index. The problem, of course, is that you're taking the value of d and using it as the index of f. The value of max then becomes an arbitrarily large number, easily exceeding the bounds of f which in your example only has three values in it. You might want to try it more like this:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim max As Integer = 0
Dim index as Integer = 0
Dim d() As Integer = {a, b, c}
Dim f() As String = {"ch1", "ch2", "ch3"}
For i As Integer = 0 To 2
If max < d(i) Then
max = d(i)
index = i
Else : max = max
End If
Next
Label1.Text = f(index)
End Sub
This still uses max as the comparer, but takes note of the index of the For loop when it reaches a new highest number, using that to pull out the value of f, ensuring that the value of index is never higher than i.
The values of a,b and c are being incremented ever time you click the first button depending on their checked state. Then when you loop thru and get the max of the variables you are trying to use that as an indec into the f() array. The f() array can only go from 0 to 2 but max can be any value. You cannot use max to select from the array. What are you trying to do ?

Visual Basic (mistakes in code)

I'm new to Visual Studio.I tried to write a simple program in Visual Basic that takes a 13-digit number from a text box and writes its digits to an array.Then it writes the second member of the array (second digit of the number) to another text box, but it doesn't work. Here's the code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim array(12) As Integer
Dim index As Integer = 11
Dim code As Long = TextBox1.Text
Do While index >= 0
array(index) = code Mod 10
code /= 10
index -= 1
Loop
TextBox2.Text = array(1)
End Sub
End Class
Can you tell me what's wrong?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim array(12) As Integer
Dim index As Integer = 11
Dim code As Char() = TextBox1.Text.ToCharArray()
For i As Integer = 0 To code.Count - 1
array(i) = Integer.Parse(code(i))
Next
TextBox2.Text = array(1)
End Sub