Confused As To Why This Function Does Not Call? - vb.net

I'm trying to call this function into a procedure and am confused on why this is not working, I've looked up how to format a function call and don't understand my mistakes.
Structure Stock
Dim category As String
Dim price As Integer
Dim size As String
Dim sku As String
Dim color As String
End Structure
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim stockArray() As String =
IO.File.ReadAllLines("stockInventory.txt")
End Sub
Private Sub btnNewSave_Click(sender As Object, e As EventArgs) Handles btnNewSave.Click
Dim answer
answer = check(sku, stockArray)
End Sub
Function check(sku, stockArray) As Boolean
Dim flag As Boolean
Dim numVar = -1
numVar = Array.IndexOf(stockArray, txtSKU.Text)
If numVar = -1 Then
flag = False
End If
If numVar <> -1 Then
flag = True
End If
Return flag
End Function
It gives me an error saying,
sku is not declared. It may be inaccessible due to its protection
level
I have a structure where sku is defined as being a string. Do I have to declare it again in this sub for it to work?

You can fine-tune this code a bit:
Function check(ByVal stockArray as String()) As Boolean
Return Array.IndexOf(stockArray, txtSKU.Text) >= 0
End Function
It'll return if the element is in the Array.
If your file is very very large and you are doing many searches, I recommend storing in a HashSet instead of an array to get O(1) searching, instead of the O(n) you are getting.
I notice your stock array, has to be declared at the class level, not the method level, to be accessible by other methods.

Related

How to use Function return

I'm wondering if this is the right way to do this.
I have this sample code that should represent a user action, and a calculation done in a function, this function should then return the value to my sub?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Integer = 1
Test_function(i)
MsgBox(i)
End Sub
Private Function Test_function(ByVal i As Integer)
i = i + 1
Return (i)
End Function
When i run this piece of code I get:
i = 1 in sub
i = 2 in function
i = 1 in sub?
How do you get the i = 2 into my sub? Or is this not the correct way of using this?
I think what you're asking is why does i not get changed by the call to Test_function.
Let's break down your code.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Integer = 1 'creating a variable named i, value 1.
Test_function(i) 'passing the value of the variable i to test function, but not doing anything with what the function returns.
MsgBox(i) 'calling messagebox with the value i.
End Sub
Private Function Test_function(ByVal i As Integer) 'creating a new variable also named i with the value being passed in.
i = i + 1 'incrementing the value of the i variable by 1.
Return (i) 'returning i
End Function
So there are a few concepts that you're misunderstanding as far as I can tell- what ByVal means, perhaps the concept of variable scoping, as well as what Return does.
The obvious answer is that you're not using the value returned by Test_function. If you had i = test_Function(i), then i would be incremented by the call to Test_function
Another approach would be to pass i ByRef instead of ByVal- if you did that, the i in the scope of your Test_function method would be the same as the i in the scope of your Button1_Click method. But because you are passing it ByVal, the i variables are actually two completely different variables.
There is nothing to wonder, it's only a simple misunderstanding. you are printing the actual value of i if you Call like this:
MsgBox(Test_function(i))
Then both will be the same;
or you can check this in following way:
Dim tempVal = Test_function(i)'<-- will give 2
MsgBox(i)'<--- will give 1
this is because you are passing the value of i not i as reference, if you pass it as reference then also both will be the same.
So if you change the function signature as follows:
Private Function Test_function(ByRef i As Integer)
i = i + 1
Return (i)
End Function
same function call will give you both values as 2
Dim tempVal = Test_function(i)'<-- will give 2
MsgBox(i)'<--- will give 2
Reference ByVal and ByRef

Listbox Sorting Issue

I am having trouble sorting a listbox the way I am expecting it to work even using the sorted = true property. Assuming i have files named as such, when i sort by "Name" in a Windows Folder view (outside of vb) it sorts like so:
1180741
1179715
1162371
1141511
1131750
1117362
1104199
1082698
1062921
1043875
991514
970621
963154
952954
948067
917669
904315
899902
892398
882024
This is how i need it to be sorted in my list. However, with the sorted = true property, vb.net decides to sort like this:
1043875
1062921
1082698
1104199
1117362
1131750
1141511
1162371
1179715
1180741
882024
892398
899902
904315
917669
948067
952954
963154
970621
991514
I cannot understand why windows sorts the list correctly but VB does not. Any help would be most appreciated.
*This example assumes that each object in the listbox is a string representation of an integer, if in the case that it is not, the item will be skipped, you can modify this to handle such a case in another way if you want....
Example:
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Dim r As New Random(Now.Millisecond)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'We simulated a populated listbox
For I As Integer = 0 To 1000
Dim n As Integer = r.Next(0, Integer.MaxValue)
ListBox1.Items.Add(n.ToString)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SortListBoxNumbers(ListBox1)
End Sub
Sub SortListBoxNumbers(listbox As ListBox)
Dim numbers As New List(Of Integer)
For Each item As Object In listbox.Items
Dim n As Integer = 0
If item.GetType = GetType(String) Then
If Integer.TryParse(DirectCast(item, String), n) Then
numbers.Add(n)
End If
End If
Next
listbox.Items.Clear()
numbers = SortNumbers(numbers)
For Each n As Integer In numbers
listbox.Items.Add(n.ToString)
Next
End Sub
Function SortNumbers(numbers As List(Of Integer)) As List(Of Integer)
Dim result As New List(Of Integer)
Dim count As Integer = numbers.Count
Do
Dim highN As Integer = Integer.MinValue
For Each n As Integer In numbers
If n > highN Then highN = n
Next
numbers.Remove(highN)
result.Insert(result.Count, highN)
Loop Until result.Count = count
result.Reverse()
Return result
End Function
End Class

the function does not seem to change value when called upon 3 times

I am writing code in visual basic. the goal of the program is to get a function to randomly pick a string from a array when called upon. the problem is it is not choosing a different value when the button calls it to do so. I have no idea why it is doing this.
Also anyone know how to get multlines to print in a label or text? here is my code.
Public Class lbl
Public Function Noun() As String
Dim rand As New Random
Dim index As Integer
Dim nouns() As String = {"boy", "girl", "dog", "town", "car"}
Dim sentence As String ' used to build a sentence
index = rand.Next(5)
sentence = nouns(index)
Return sentence
End Function
Public Function outs(ByVal n1 As String) As String
Dim result = n1
Return result
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
lblRandom.Text = outs(Noun()) & outs(Noun())
lblRandom1.Text = outs(Noun())
End Sub
Public Shared Function RandomNumber(ByVal lowerBound As Integer, ByVal upperBound As Integer) As Integer
Return CInt(Math.Floor((upperBound - lowerBound + 1) * Rnd())) + lowerBound
End Function
Public Function Noun() As String
Dim rand As New Random
Dim index As Integer = -1
Dim nouns() As String = {"boy", "girl", "dog", "town", "car"}
Dim sentence As String ' used to build a sentence
index = RandomNumber(0, 4)
sentence = nouns(index)
Return sentence
End Function
Public Function outs(ByVal n1 As String) As String
Dim result = n1
Return result
End Function
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
lblRandom.Text = outs(Noun()) & outs(Noun())
lblRandom1.Text = outs(Noun())
End Sub
The problem is that you re-create a new random generator each time the Noun() function is called. The random number generator should be a global variable.
The problem is that the .NET random number generator generates random numbers based on a seed. The seed is an integer based on the current system date and time. If you call the random number generator a number of times in quick succession, the same seed is likely to be used and the same "random" number will be generated. The solution is to declare the Random object outside of the procedure in which you wish to generate the random number and to pass a seed to its constructor which is built from the current (random) date and time.
To answer your other questions. Label controls will automatically wrap text if the text contains carriage returns. I have added a carriage return below for lblRandom.
To wrap text in a text box, set its Multiline property to True.
The code that #Aly El-Haddad posted will also work but it contains some VB6 legacy code. I have tried to keep it as .NET as possible. I have also eliminated the superfluous "outs" procedure and made it flexible so that you can add or delete elements in the "nouns" array. Note that with the line "index = rand.Next(5)", you are including element 5 of the array which doesn't exist. The upper bound of the zero-based array is 4 so an "Index was outside the bounds of the array" error will be thrown when your random number generator returns 5.
Private myRandom As New Random(CType(DateTime.Now.Ticks Mod Int32.MaxValue, Integer))
Private Function Noun() As String
Dim index As Integer
Dim nouns() As String = {"boy", "girl", "dog", "town", "car"}
index = myRandom.Next(nouns.GetLowerBound(0), nouns.GetUpperBound(0))
Return nouns(index)
End Function
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
lblRandom.Text = Noun() & Constants.vbNewLine & Noun()
lblRandom1.Text = Noun()
End Sub

Selecting variable by name

I'm not sure of the appropriate search term so please close and point me to a duplicate if that's the case.
I have a structure with has multiple variables. I've created an array of the type of the structure. I've added data to a few indexes of the array. I now want to select the a specific variable based on the name of the variable.
He's an example of the structure:
structure struc
dim name as string
dim lvl as integer
dim capacity as integer
end structure
And the deceleration of the array:
dim vills(3) as struc
You should be using public fields, not variables Actually you have the terminology correct according to MSDN, the only thing you are missing is the access modifier i.e.
Public Structure Struc
Public Name As String
Public Lvl As Integer
Public Capacity As Integer
End Structure
At the moment your variables are private which means they aren't accessible from outside your Structure.
Looking at your comments it looks like you are trying to access the property by name dynamically rather than knowing it at compile time. There are a few ways of doing this, most of which involve some Reflection.
You should perhaps have a look at ExpandObject - it's effectively a key/value dictionary with the characteristics of a normal class-type object so you get the best of both worlds e.g.
Dim struct As Object = New ExpandoObject()
struct.name = "SomeValue"
struct.lvl = 3
struct.capacity = 100
Console.WriteLine(struct["name"])
Console.WriteLine(struct["lvl"])
As Tony Hopkinson mentioned, it is possible via Reflection:
Public Class Form1
Structure struc
Dim name As String
Dim lvl As Integer
Dim capacity As Integer
End Structure
Dim vills(3) As struc
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i As Integer = 0 To vills.Length - 1
vills(i).lvl = i * 10
Next
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim fi As Reflection.FieldInfo
Dim fieldName As String = "lvl"
For i As Integer = 0 To vills.Length - 1
fi = vills(i).GetType.GetField(fieldName, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public)
If Not IsNothing(fi) Then
Dim value As Object = fi.GetValue(vills(i))
Debug.Print(i & ": " & value.ToString)
End If
Next
End Sub
End Class
Up to you to decide if it's worth it...
I really think that if this is an important and frequent requirement (find an element by key) then you should think twice and change your array to a Dictionary(Of string, struc) and use something like this
Dim vills = new Dictionary(of String, struc)
Dim s = new struc()
s.name="k1"
s.lvl=1
s.capacity=1
z.Add(s.name, s)
....
struc c = vills("k1")
if(c IsNot Nothing) Then
Console.WriteLine(c.lvl.ToString())
End If
but if you still want to use an array you could search your struc array by name using Linq
Structure struc
Public Dim name as string
Public Dim lvl as integer
Public Dim capacity as integer
End Structure
Sub Main
Dim vills(3) as struc
....
Dim c as struc = wills.Where(Function(x) (x.name="keyName")).Single()
if(c.name IsNot Nothing) then
Console.WriteLine("Found")
End If
End Sub

VB.net Logic Error

This is supposed to be a program that tells how long it would take you to download a given size of file after the user inputs their download speed. intOne doesn't seem to be affected at all by the function that I have created to determine what intOne's value should be. I'm reasonably sure that the equation I'm using is correct.
Public Class Form1
Private tSize As System.Drawing.Size
Private checkUserSpeed As Long = 0
Private checkBitByte As Integer = 0
Private setSize As Integer = 0
Private checkUserSize As String = ""
Private Answer As Double = 0
Private ReadOnly Property checkDwnldSize() As String
Get
Return ComboBox1.Text
End Get
End Property
Function checkDownloadSize(ByVal checkUserSize As String)
Dim suffixes As New List(Of String)({"b", "k", "m", "g", "t", "p", "e", "z"})
Dim index As Long = suffixes.IndexOf(checkUserSize.Substring(0, 1).ToLower) > -1
If index > -1 Then
Return checkUserSpeed / 1024 ^ index
Else
Return False
End If
End Function
Function checkForByte(ByVal checkUserSize)
If Microsoft.VisualBasic.Right(checkUserSize.ToLower, 7) = "byte(s)" Then
checkBitByte = 1
checkDownloadSize(checkUserSize)
Return True
End
Else
Return False
End If
Return checkBitByte
Return checkUserSpeed
End Function
Function checkForBit(ByVal checkUserSize)
If Microsoft.VisualBasic.Right(checkUserSize.ToLower, 6) = "bit(s)" Then
checkBitByte = 8
checkDownloadSize(checkUserSize)
Return True
End
Else
checkForByte(checkUserSize)
Return False
End If
Return checkBitByte
Return checkUserSpeed
End Function
Function Calculate(ByVal checkUserSpeed, ByVal checkUserSize)
checkForBit(checkUserSize)
Return Answer
End Function
Private Sub FitContents()
tSize = TextRenderer.MeasureText(TextBox3.Text, TextBox3.Font)
TextBox3.Width = tSize.Width + 10
TextBox3.Height = tSize.Height
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Calculate(Convert.ToInt32(TextBox3.Text), ComboBox3.Text)
Answer = checkBitByte * ((1024 ^ setSize) / checkUserSpeed)
TextBox1.Text = Answer
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
Call FitContents()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form2.Show()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Form3.Show()
End Sub
End Class
Help would be greatly appreciated.
You never assign any value to intOne, so it starts with value 0. So this:
intOne = intOne / 1024 ^ 3
is the same as this:
intOne = 0 / 1024 ^ 3
which is of course this:
intOne = 0
As an aside:
Don't use Dim at the class/form level, use Private.
Always use As <ReturnType> when decalring you functions
Turn on Option Strict in your language settings.
Dim intTwo As Integer = Nothing doesn't do anything useful. Use
Dim intTwo As Integer = 0 instead.
Always declare your variables as a type as well (Dim tSize is missing its type)
Followup to your edits:
This:
Function checkForByte(ByVal checkUserSize)
Should be this:
Function checkForByte(ByVal checkUserSize As Long)
Fix this in all places in your code. Everything should have an As DataType. If you turned on Option Explict and Option Strict like was suggested the compiler will locate these problems for you.
Next, do not name local variables and/or parameters the same as your class/form level private variables. I personally prefix my instance variables like this:
Private _checkUserSpeed As Long = 0
Note the underscore. Some people use a lowercase m instead:
Private mCheckUserSpeed As Long = 0
Pick something and be consistent. This makes it clear the scope of a variable.
Also this code is bad, the second Return will never be hit:
Return checkBitByte
Return checkUserSpeed
Finally, assuming you are trying to populate the variable _checkUserSpeed with a value, you are still not ever doing it. Search your code and look for any place you are doing this:
_checkUserSpeed = something
You won't find it. So that variable will always be 0.
None of these are really VB.NET specific issues. All of these best-practices would apply equally to C# or any other langue.
You should look into cleaning up your code first, to improve readability. Avoid code repetition at all costs. If you think you cannot do otherwise, lean towards data repetition instead, i.e. create an XML file to store your values, but keep the code clean. Any errors you find later will then stand out like white crows.
For example, something like this:
Function checkDownloadSize(ByVal strOne As String)
Dim suffixes As New List(Of String)({"b", "k", "m", "g", "t", "p", "e", "z"})
Dim index As Long = suffixes.IndexOf(strOne.Substring(0, 1).ToLower) > -1
If index > -1 Then
Return intOne / 1024 ^ index
Else
Return False
End If
End Function
roughly replaces your two pages of checkDownloadSize and checkSize.
Notice how every function here is called only once. If you later decide to swap ToLower with your custom implementation, it needs to be done only once. Not only a performance improvement, it also makes your code more maintainable in the long run.
Another point is that I am not using Microsoft.VisualBasic namespace in the above code, and switched to Substring instead. This will ease your burden with migration to C#, and also make native C# devs understand your code better.