Function return string or boolean - vb.net

I want a function to return a String or Boolean. Something like this:
Public Function GetString(Byval What As String) 'As... someting?
If (What = "A") Then
Return "String to return"
Else if (What = "B") Then
Return True
End If
Return False 'Nothing to return
End Function
How can i now do this? If i ask like
If GetString("A") Then
MsgBox(GetString())
End IF
...it returns a string and of course it gives an error on converting string to bool.
I could always return strings and checks it lengths, but it feels bad. Or maybe I'm just into PHP too much?
But is there a way to do it more like this? If i ask for "B" i know it would return a bool, if i ask for "A" i want to alert the string if there was any and so on.

How can i now do this?
You can't.
A function can only return one type, not multiple.
You can return a custom type that contains a string and a boolean.

I would use an Array list. You can store whatever type you need in the list and then parse it on the return. This is really not best practice as explained above, but when you gotta get things done... The end justifies the means. Not recommended.
Public Function GetString(Byval What As String) As ArrayList
Dim b as boolean = True
dim myArrayList as Arraylist = New ArrayList
If (What = "A") Then
ArrayList.Add("String to return")
Else if (What = "B") Then
ArrayList.Add(b)
End If
Return False 'Nothing to return
End Function
Proof of concept below:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim a As Boolean = True
Dim myarraylist As ArrayList = New ArrayList
myarraylist.Add(a)
myarraylist.Add("g")
Debug.WriteLine(myarraylist.GetType.ToString)
Debug.WriteLine(myarraylist(0).GetType.ToString)
Debug.WriteLine(myarraylist(1).GetType.ToString)
If myarraylist(0).GetType.ToString = "System.string" Then
Debug.WriteLine("Function returned a String")
ElseIf myarraylist(0).GetType.ToString = "System.boolean" Then
Debug.WriteLine("Function returned a Boolean")
End If
End Sub

You can return Object, but it is considered very bad form for a function to return 2 data types.

As Oded said, you can't return more than one parameter from a function.
It is not too clear what you are doing from your code example, but you may look into passing parameters by reference. As pointed out in the answer there, passing a parameter by reference is useful for:
when you want to return a state or status of an operation plus the result from the operation.
This is how int.TryParse and similar methods work.

Related

When is it suitable to use a Sub-Procedure instead of a function?

In my class today I was told change some of my sub-procedures to functions, and when I asked why it's better my teacher struggled to answer, generally, i've always thought that functions should only really be used when a value is returned. In the two examples below; is there one method that should be used over the other, or does it not matter? And if it does matter why?
Thanks in advance.
Method 1 (Sub-Proc):
Sub EncryptString(ByVal unkString, ByRef encryptedString)
For i = 1 To Len(unkString)
encryptedString += "*"
Next
End Sub
Method 2 (Function):
[In main I assign the variable "encryptedString" to this function].
Function encryptString(ByVal unkString) As String
For i = 1 To Len(unkString)
encryptString += "*"
Next
End Function
You've misunderstood what they're trying to tell you. In your Function example there is no difference. What your teacher is expecting is like this:
Function EncryptString(ByVal unkString) As String
Dim encryptedString As String = ""
For i = 1 To Len(unkString)
encryptedString += "*"
Next
Return encryptedString
End Function
This is a cleaner and more reusable way than modifying a field, an argument passed ByRef, or the underlying variable of the function
Your example show one of the multiple reason, who initialize the data is unclear. With your sample code, the first option would append to the passed string while the second would create a new string.
The first method would have to specify if it needs an empty string or explain why it appends. While the second method clearly show that a new string will be returned.
Sub Main()
Dim u, e As String
u = "123"
e = "123"
EncryptString1(u, e)
Console.WriteLine(e) ' Display: 123***
u = "123"
e = "123"
e = encryptString2(u)
Console.WriteLine(e) ' Display: ***
Console.ReadLine()
End Sub
Sub EncryptString1(ByVal unkString As String, ByRef encryptedString As String)
For i As Integer = 1 To Len(unkString)
encryptedString += "*"
Next
End Sub
Function encryptString2(ByVal unkString As String) As String
encryptString2 = ""
For i As Integer = 1 To Len(unkString)
encryptString2 += "*"
Next
End Function
Please have option strict on. Also, personally, I rather create a variable instead of using the function name, use .Length instead of Len() and concatenate with & instead of +.
Function encryptString3(ByVal unkString As String) As String
Dim encryptedString As String = ""
For i As Integer = 1 To unkString.Length
encryptedString &= "*"
Next
Return encryptedString
End Function
Or just use the New operator of the String class.
Dim encryptedString as New String("*"c, unkString.Length)
Well, when I was learning this stuff, it was always to use functions to calculate values and subs to do other stuff. I guess for something very general, it doesn't really matter which methodology you use, as you have illustrated in your example. See the link below for further discussion on this topic.
http://analystcave.com/vba-function-vs-vba-sub-procedures/

Is there a neat and clean way to handle nulls with Yields?

While CommitReader.Read()
Yield New Commit() With {
.FirstValue = CommitReader.GetInt32(CommitReader.GetOrdinal("FirstValue")),
.SecondValue = CommitReader.GetString(CommitReader.GetOrdinal("SecondValue")).Trim(),
'Lots of values
End While
I know I can do something like this; however there are 24 properties and I would like to make this part as clean as possible
While CommitReader.Read()
new Commit (){
Dim index As Integer = reader.GetOrdinal("FirstValue")
If reader.IsDBNull(index) Then
FirstValue = String.Empty
Else
FirstValue = reader(index)
End If
index = reader.GetOrdinal("SecondValue")
If reader.IsDBNull(index) Then
SecondValue = String.Empty
Else
SecondValue = reader(index)
End If
}
End While
Is there a better way to handle this type of thing? I am mainly a C# developer so if the syntax is off a little sorry, I am winging it in VB.
It's a shame that SqlDataReader doesn't have the generic Field extension method like DataRow does, but you could define your own extension method (has to be in a module in VB.NET) to help with the null checks, perhaps something like this:
<Extension>
Function GetValue(Of T)(rdr As SqlDataReader, i As Integer) As T
If rdr.IsDBNull(i) Then
Return Nothing
End If
Return DirectCast(rdr.GetValue(i), T)
End Function
And use it something like this:
While CommitReader.Read()
Yield New Commit() With {
.FirstValue = CommitReader.GetValue(Of Integer?)(CommitReader.GetOrdinal("FirstValue")),
.SecondValue = CommitReader.GetValue(Of String)(CommitReader.GetOrdinal("SecondValue")),
'Lots of values
End While
I haven't tested this fully to make sure it handles all data types appropriately (may be worth looking at DataRowExtensions.Field to see how it does it).
Note that you are using String.Empty as the "null" value for strings, while this will use Nothing/null (I also had to remove the .Trim call to avoid NREs). If you want empty string instead, you could use (adding the Trim back in):
.SecondValue = If(CommitReader.GetValue(Of String)(CommitReader.GetOrdinal("SecondValue")), String.Empty).Trim()
You may also want to move the GetOrdinal calls out of the loop to improve performance.
Obviously you have repetition in your code if ... else ... condition.
So you can extract it in another method.
For your case generic extension method seems good candidate.
Public Module Extensions
<Extension>
Public Function GetValueOrDefault(Of T)(originalValue As object,
defaultValue As T) As T
If originalValue = DbNull.Value Then
Return defaultValue
End If
return DirectCast(originalValue, T)
End Function
End Module
Then use it:
While CommitReader.Read() = True
Dim temp = new Commit With
{
Dim index As Integer = reader.GetOrdinal("FirstValue")
FirstValue = reader(index).GetValueOrDefault(String.Empty)
Dim index As Integer = reader.GetOrdinal("SecondValue")
FirstValue = reader(index).GetValueOrDefault(String.Empty)
}
End While
You can create another overload which return "default" value for given type if it is DbNull
<Extension>
Public Function GetValueOrDefault(Of T)(originalValue As object) As T
Return originalValue.GetValueOrDefault(Nothing)
End Function
Nothing in vb.net is default value, for reference types it is null for Integer it is 0 for example.
For using this overload you need provide type parameter explicitly
While CommitReader.Read() = True
Dim temp = new Commit With
{
Dim index As Integer = reader.GetOrdinal("FirstValue")
FirstValue = reader(index).GetValueOrDefault(Of String)()
Dim index As Integer = reader.GetOrdinal("SecondValue")
FirstValue = reader(index).GetValueOrDefault(Of String)()
}
End While
Notice that your solution executing reader twice, for checking is it null and for reading value. This can cause "tiny" performance issue.
So in extension method above we read value only once and then check value for DbNull.
If you concatenate a string with a Null you get the string:
FirstValue = reader(index) & ""
Kind of "unprofessional" but saves a lot of coding time if all you are doing is converting a possible Null to an empty string. Easy to forget however, so later data dependent errors may pop up.

What is the simplest way to determine whether or not a one dimensional array of strings contains nothing but zero length strings?

The obvious first thought is:
Public Function CheckStrings(ByVal input As String()) As Boolean
For Each s As String In input
If s.Length > 0 Then Return True
Next
Return False
End Function
I'm sure there is a simpler way than that though. At least simpler in terms of the code if not necessarily the performance.
End result:
Well, you guys did a pretty good job of simplifying. Well done.
I think I'll still use an extension to make it just that much simpler in the main code. The final result really isn't too bad by itself though.
Here's my final code:
<Extension()> _
Public Function AnyNonZero(ByVal value As String()) As Boolean
If Not value.All(Function(x) String.IsNullOrEmpty(x)) Then Return True
Return False
End Function
You can use this to return true if all elements are zero length.
Dim inputIsEmpty As Boolean = Array.TrueForAll(input, Function(x) x.Length = 0)
Be careful of null references. You may want to use this instead:
Dim inputIsEmpty As Boolean = Array.TrueForAll(input, Function(x) String.IsNullOrEmpty(x))
How about this? It uses your string array named input.
Array.TrueForAll(input, AddressOf ZeroLengthString)
Private Function ZeroLengthString(ByVal s As String) As Boolean
Return s.Length = 0
End Function
Here's a Linq function similar to Array.TrueForAll:
Dim allEmpty = values.All(Function(x) x.Length = 0)
I find it to be a bit more easily readable than Array.TrueForAll.

VB.NET Generic Function

What I want to do is, based on the type of T do different opperations. Below is a simple example of my problem.
Public Shared Function Example(Of T)() As T
Dim retval As T
If TypeOf retval Is String Then
Dim myString As String = "Hello"
retval = myString
ElseIf TypeOf retval Is Integer Then
Dim myInt As Integer = 101
retval = myInt
End If
Return retval
End Function
I get the error "Value of Type 'String' Cannot be converted to 'T'" Same with the integer part. If I cast either to an object before asigning them to retval it works but I think that would defeat my purpose and be less efficient. Any Ideas? Thanks!
It's probably a bit late, but try this:
Public Shared Function CAnyType(Of T)(ByRef UTO As Object) As T
Return CType(UTO, T)
End Function
Public Shared Function ExecuteSQLstmtScalar(Of T)(ByVal strSQL As String) As T
Dim T_ReturnValue As T
' Here we have the result of a DB query '
Dim obj As Object = "Value from DB query cmd.ExecuteScalar"
Dim strReturnValue As Object = obj.ToString();
Try
Dim tReturnType As Type = GetType(T)
If tReturnType Is GetType(String) Then
Return CAnyType(Of T)(strReturnValue)
ElseIf tReturnType Is GetType(Boolean) Then
Dim bReturnValue As Boolean = Boolean.Parse(strReturnValue)
Return CAnyType(Of T)(bReturnValue)
ElseIf tReturnType Is GetType(Integer) Then
Dim iReturnValue As Integer = Integer.Parse(strReturnValue)
Return CAnyType(Of T)(iReturnValue)
ElseIf tReturnType Is GetType(Long) Then
Dim lngReturnValue As Long = Long.Parse(strReturnValue)
Return CAnyType(Of T)(lngReturnValue)
Else
MsgBox("ExecuteSQLstmtScalar(Of T): This type is not yet defined.")
End If
Catch ex As Exception
End Try
Return Nothing
End Function
(the secrect is casting your generic result to object, then casting from type Object to template type T).
PS:
You are responsible to ensure that your code works correctly with nullable types and NOT nullable types, as well as System.DbNull.Value. For example when string is NULL and return value type is Boolean (not nullable). On a sidenote, please also note that VB Nothing is NOT equal NULL, it's equal to C#'s default(T) (e.g. System.Guid.Empty for Guid)
With a generic method, T will be of exactly one type each time. Let's say that you have code calling Example(Of Integer). Now, in your mind, replace T with Integer. The resulting method will contain these lines (amongst others).
Dim retval As Integer
If TypeOf retval Is String Then
Dim myString As String = "Hello"
retval = myString
' more code follows '
Assigning a String to an integer like that will never work. Sure, that code will also never execute, since the If-block prevents that, but the code will still not compile. (As a side not, the above code will fail to compile because the TypeOf keyword is restricted to use with reference types, but that is another story)
Typically when creating generic methods, you will want to do the same thing with whatever input you get, but in a type safe manner. If you want to have different behavior for different types of input, you are usually better off by overloading the methods instead.
retVal = (T) "Hello World!"
Do retval = Ctype(Mystring, T) or retVal = Ctype(MyInt, T)
An alternative solution is encapsulate this kind of logic in a class and use VB CallByName function:
Class Aux(Of T)
Public Value As T
Private dicc As Dictionary(Of String, Object)
Sub New()
dicc = New Dictionary(Of String, Object)
dicc.Add("system.string", "hola")
dicc.Add("system.int32", 15)
dicc.Add("system.double", 15.0)
End Sub
Public Function Test() As T
Dim typeName As String = GetType(T).ToString.ToLower
If dicc.ContainsKey(typeName) Then
CallByName(Me, "Value", CallType.Set, dicc(typeName))
End If
Return Value
End Function
Protected Overrides Sub Finalize()
MyBase.Finalize()
If Not (dicc Is Nothing) Then dicc.Clear()
dicc = Nothing
End Sub
End Class

Can you pass a "type" as an argument?

I want to do something like the following in VB.NET, is it possible?
Function task(value as Object, toType as Type)
Return DirectCast(value, toType)
End Function
Yes. There is System.Type. You may actually want to do a Generic however.
Function SomeFunction(Of T)(obj As Object) As T
'' Magic
End Function
Great Answer - Here is a generic function to do the same:
Public Sub BindListControlToEnum(Of T)(ListCtrl As ListControl)
Dim itemValues As Array = System.Enum.GetValues(GetType(T))
Dim itemNames As Array = System.Enum.GetNames(GetType(T))
For i As Integer = 0 To itemNames.Length - 1
Dim item As New ListItem(itemNames(i), itemValues(i))
ListCtrl.Items.Add(item)
Next
End Sub
Call it like this:
BindDropdownToEnum(Of MyClass.MyEnum)(MyRadioButtonListControl)
you want to use the
function task(of myType)(value as myType) as MyType
''stuff
return value
end function
Yes, though, depending on your requirements, you may want to use CType to do any type casting/conversion. CType will work so long as there is a valid type conversion, whereas DirectCast requires that value be of the type toType.
I had to do something similar today (essentially filling in the '' magic from the accepted answer):
Private Function Convert_Value_Or_Fallback(Of T)(ByRef value As Object, ByRef fallback As Object) As Object
Try
Return DirectCast(value, T)
Catch ex As Exception
Return fallback
End Try
End Function
'call it like this:'
Convert_Value_Or_Fallback(Of Double)(value, 0)