What I want is to return 2 values from the database with a function and then store the values in variables so I can work with them. This is my code.
Function Buscar_Registro(ByVal xId As Integer) As String
Dim a, b As String
'convertir cadena
Dim Id As Integer
Id = xId
'conexión
Dim Conexion As OleDbConnection = New OleDbConnection
Conexion.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Visual\2000Phrases\2000 Phrases.accdb"
'cadena SQL
Dim CadenaSQL As String = "SELECT * FROM Data WHERE Id = " & Id
'Adaptador
Dim Adaptador As New OleDbDataAdapter(CadenaSQL, Conexion)
'Data set
Dim Ds As New DataSet
'Llenar el Data set
Conexion.Open()
Adaptador.Fill(Ds)
Conexion.Close()
'Contar registro
If (Ds.Tables(0).Rows.Count = 0) Then
Return False
Else
a = Ds.Tables(0).Rows(0)("Nombre").ToString()
b = Ds.Tables(0).Rows(0)("Apellido").ToString()
Ds.Dispose()
Return a
Return b
Return True
End If
End Function
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
Randomize()
Dim value As Integer = CInt(Int((20 * Rnd()) + 1))
TextBox3.Text = Buscar_Registro(value)
TextBox4.Text =
End Sub
I dont' know how to do it. The function returns only the value of "a"
Thanks
To return more values you need to change your function "as object"
Function Buscar_Registro(ByVal xId As Integer) As Object
and then you can put your return values into an object this way:
Return{a, b, true}
You'll get your values this way:
Dim mObj as object = Buscar_Registro(yourInteger)
you'll have:
a in mObj(0)
b in mObj(1)
True in mObj(2)
adapt it to your needs
EDIT (message to those that downvoted):
Creating a class an using a specific Object (the one created) to make a Function able to return multiple elements is surely the best choice.
Anyway, if someone doesn't know that it's possible to use the method that I showed in my answer, he is probably not (yet) able to create a class. So I think it's better give an usable (but not perfect) answer instead of a perfect (but unusable for the one who asked) answer.
This is what I think. Anyone can think differently.
Your best option here is to create your own class with the data you need and return that.
Public Class Data
Public Property Nombre As String
Public Property Apellido As String
End Class
And then do:
Function Buscar_Registro(ByVal xId As Integer) As Data
....
If (Ds.Tables(0).Rows.Count = 0) Then
Return Nothing
Else
a = Ds.Tables(0).Rows(0)("Nombre").ToString()
b = Ds.Tables(0).Rows(0)("Apellido").ToString()
Ds.Dispose()
return new Data() With {.Nombre = a, .Apellido = b}
End If
End Function
As of VB 15 you can use ValueTuple
Function Buscar_Registro(ByVal xId As Integer) As (Nombre As String, Apellido As String)
....
If (Ds.Tables(0).Rows.Count = 0) Then
Return (Nothing, Nothing)
Else
a = Ds.Tables(0).Rows(0)("Nombre").ToString()
b = Ds.Tables(0).Rows(0)("Apellido").ToString()
Ds.Dispose()
Return (a, b)
End If
End Function
I'm new to .net but wouldn't "ByRef" be the easiest way?
Function Buscar_Registro(ByVal xId As Integer, ByRef a As String, ByRef b As String)
Related
I can't understand what is happening with the following code in VB.NET. When I run this code:
Public Function test() As Boolean
Dim a As Integer = 1
Dim b As Object = a
Dim c As Object = b
Return Object.ReferenceEquals(b, c)
End Function
Then the function returns True. However, if I run this:
Structure TTest
Dim i As Integer
Dim tid As Integer
Sub New(ByVal _i As Integer, ByVal _tid As Integer)
i = _i
tid = _tid
End Sub
End Structure
Public Function test_2() As Boolean
Dim a As New TTest(1, 1)
Dim b As Object = a
Dim c As Object = b
Return Object.ReferenceEquals(b, c)
End Function
Then it returns False. In both functions, I declare two value type variables, an Integer on the first and a custom Structure on the second one. Both should be boxed upon object assignment, but in the second example, it seems to get boxed into two different objects, so Object.ReferenceEquals returns False.
Why does it work this way?
For primitive types, .Net is able to re-use the same "box" for the same values, and thus improve performance by reducing allocations.
Same with strings, it's .NET way to optimize thing. But as soon as you use it, the reference will change.
Sub Main()
Dim a As String = "abc"
Dim b As String = "abc"
Console.WriteLine(Object.ReferenceEquals(a, b)) ' True
b = "123"
Console.WriteLine(Object.ReferenceEquals(a, b)) ' False
Console.ReadLine()
End Sub
Why do I keep getting two input boxes instead of one? What am I doing wrong? Is it how I am passing values through functions? If so, how can I fix this?
Private Sub Calculate_Click(sender As Object, e As EventArgs) Handles Calculate.Click
'Dim ready_ship As Integer = GetInStock()
Dim display_spools As Integer = ReadyToShip()
Dim display_backOrders As Integer = BackOrdered()
lbl_rship.Text = display_spools.ToString()
lbl_backo.Text = display_backOrders.ToString()
End Sub
Function GetInStock() As Integer
Dim amount_Spools As String = Nothing
amount_Spools = InputBox(" Enter the number of spools currently in stock: ")
Return CInt(amount_Spools)
End Function
Function ReadyToShip() As Integer
Dim ready_ship As Integer = GetInStock()
Dim a As Integer
a = CInt(ready_ship)
Return a
End Function
Function BackOrdered() As Integer
Dim b As Integer = ReadyToShip()
Dim c As Integer
c = b - CInt(TextBox1.Text)
Return c
End Function
End Class
Your Calculate_Click event is calling ReadyToShip() and BackOrdered() functions which are both going to GetInStock() function, which is displaying the input box. So it will be displayed twice.
This class would be better served using properties, they are easier to manage and will help avoid this kind of method duplication.
When I get to the Read loop I get an index out of bounds error. I think its on the reader ordinal value, but I am not sure why I am getting it.
Private Function Create(Reader As SqlDataReader) As IEnumerable(Of MyObject)
SetOrdinals(MyObjectReader)
Dim MyObjects = New List(Of MyObject)
While MyObjectReader.Read()
Dim Temp = New MyObject() With {
.FirstValue = MyObjectReader.GetValue(Of Integer)(MyObjectReader(FirstValue_Ord)),
.SecondValue = If(MyObjectReader.GetValue(Of String)(MyObjectReader(SecondValue_Ord)), String.Empty).Trim(),
.ThirdValue = If(MyObjectReader.GetValue(Of String)(MyObjectReader(ThirdValue_Ord)), String.Empty).Trim(),
MyObjects.Add(Temp)
End While
Return MyObjects
End Function
Private Sub SetOrdinals(MyObjectReader As SqlDataReader)
FirstValueOrd = MyObjectReader.GetOrdinal("FirstValue")
SecondValue_Ord = MyObjectReader.GetOrdinal("SecondValue")
ThirdValue_Ord = MyObjectReader.GetOrdinal("ThirdValue")
End Sub
End Class
Public Module Extensions
<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
End Module
You should just be passing in the ordinal to the GetValue calls:
While MyObjectReader.Read()
Dim Temp = New MyObject() With {
.FirstValue = MyObjectReader.GetValue(Of Integer)(FirstValue_Ord),
.SecondValue = If(MyObjectReader.GetValue(Of String)(SecondValue_Ord), String.Empty).Trim(),
.ThirdValue = If(MyObjectReader.GetValue(Of String)(ThirdValue_Ord), String.Empty).Trim()
}
MyObjects.Add(Temp)
End While
Here my version :)
Private Function Create(reader As SqlDataReader) As IEnumerable(Of MyObject)
Dim objects As New List(Of MyObject)()
Dim ordinals As New Ordinals(reader)
While reader.Read()
Dim Temp As New MyObject With
{
.FirstValue = reader.GetValueOrDefault(Of Integer)(ordinals.FirstValue),
.SecondValue = reader.GetValueOrDefault(ordinals.SecondValue, "").Trim(),
.ThirdValue = reader.GetValueOrDefault(ordinals.ThirdValue, "").Trim()
}
objects.Add(Temp)
End While
Return MyObjects
End Function
Private Class Ordinals
Public Property FirstValue As Integer
Public Property SecondValue As Integer
Public Property ThirdValue As Integer
Public Sub New(reader As SqlDataReader)
FirstValue = reader.GetOrdinal(nameOf(FirstValue))
SecondValue = reader.GetOrdinal(nameOf(SecondValue))
ThirdValue = reader.GetOrdinal(nameOf(ThirdValue))
End Sub
End Class
Public Module Extensions
<Extension>
Function GetValueOrDefault(Of T)(reader As SqlDataReader, ordinal As Integer) As T
Return reader.GetValueOrDefault(Of T)(ordinal, Nothing)
End Function
<Extension>
Function GetValueOrDefault(Of T)(reader As SqlDataReader,
ordinal As Integer,
defaultValue As T) As T
Dim value = reader(ordinal)
If value = DbNull.Value Then
Return defaultValue
End If
Return DirectCast(value, T)
End Function
End Module
Because extension method execute checking for DbNull.Value against already extracted object, we get rid from reading same value twice from SqlDataReader.
SqlDataReader.IsDbNull(index) reads value before checking for DbNull.
Extension method have two overloads:
- One which return default value of given type, if value is DbNull.Value. Nothing in vb.net is default value of type.
- And one which takes parameter for default value you want return if value is DbNull.Value. Possibility pass default value makes lines where you create new object shorter and more readable. We get rid of inline if statement.
Your extension method with name GetValue have "side effects". By name consumer of this method expect to get value from SqlDataReader. So he can expect to get DbNull.Value if database query return NULL, but instead he get null for string or 0 for integer. Name GetValueOrDefault is little bid more informative, so you don't need to go inside method to check what is doing.
I have trouble rounding some cells from my datatable.
I want this round to 2 decimal places, as you can imagine. I will explain quickly how I charge data to the DataTable :
I have this function stored in a class:
Protected Friend Function cargarPref(ByVal id_Pref As String) As DataTable
Dim cmd As String = "Select Material,Cubicaje,SubTotal,ITBM,Total from Preferencia WHERE Id_Preferencia=#id_Pref"
Dim t As New DataTable
Try
con.Open()
comando = New OleDbCommand(cmd, con)
comando.Parameters.AddWithValue("#id_Pref", id_Pref)
adapter = New OleDbDataAdapter(comando)
adapter.Fill(t)
comando.Dispose()
adapter.Dispose()
con.Close()
Catch ex As Exception
MsgBox("Error en la consulta: " + ex.Message, MsgBoxStyle.Critical)
End Try
Return t
End Function
Ok, now I call it in my windows form:
Private Sub DataCliente_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataCliente.CellContentClick
If (Not IsNothing(DataMate)) Then
DataMate.DataSource = Nothing
mt.Clear()
End If
Dim index As Integer = 0
If (DataCliente.Columns(DataCliente.CurrentCell.ColumnIndex).Name.Equals("Empresa")) Then
index = Me.DataCliente.CurrentRow.Index
lblid.Text = DataCliente.Rows(index).Cells("Id_Cliente").Value
lblempresa.Text = DataCliente.Rows(index).Cells("Empresa").Value
lbldirecc.Text = DataCliente.Rows(index).Cells("Direccion").Value
lblcorreo.Text = DataCliente.Rows(index).Cells("Correo").Value
lbltel.Text = DataCliente.Rows(index).Cells("Telefono").Value
lblpreyd.Text = Math.Round(CDbl(DataCliente.Rows(index).Cells("PrecioYD").Value), 2).ToString("N2")
End If
mt = data.cargarPref(DataCliente.Rows(index).Cells("Id_Preferencia").Value) <--HERE!!!
DataMate.DataSource = mt
PanelMaterial.Enabled = True
End Sub
Now, watch as my values are in my database access, Along With the datagrid of the program!
For some strange reason ... the data I have taken from the database , the program treats them as if they were integers.
How I can then round off those values that are within the datatable, before printing in datagridview?
I finally discovered a solution , and I am going to show you how to solve this problem, if you need it (Before you do this , you must define all the columns you want to see on your datagridview !):
First, I create a class with public properties:
Public Class Preferencia
Public Property Material() As String
Public Property Cubicaje() As String
Public Property SubTotal() As String
Public Property ITBM() As String
Public Property Total() As String
End Class
Second , I create 2 functions and one procedural method :
Private Function setPreferencia(ByVal material As String, ByVal cubicaje As String, ByVal subtotal As String, ByVal itbm As String, ByVal total As String) As Preferencia
Dim item As New Preferencia
item.Material = material
item.Cubicaje = FormatNumber(cubicaje, 2)
item.SubTotal = FormatNumber(subtotal, 2)
item.ITBM = FormatNumber(itbm, 2)
item.Total = FormatNumber(total, 2)
Return item
End Function
Private Function registrarPreferencia() As List(Of Preferencia)
Dim lista As New List(Of Preferencia)
For Each itm In mt.Rows
lista.Add(setPreferencia(itm(0), itm(1), itm(2), itm(3), itm(4)))
Next
Return lista
Protected Friend Sub FillGrid()
DataMate.AutoGenerateColumns = False
DataMate.DataSource = registrarPreferencia()
DataMate.Columns("Material").DataPropertyName = "Material"
DataMate.Columns("Cubicaje").DataPropertyName = "Cubicaje"
DataMate.Columns("SubTotal").DataPropertyName = "SubTotal"
DataMate.Columns("ITBM").DataPropertyName = "ITBM"
DataMate.Columns("Total").DataPropertyName = "Total"
End Sub
Now the only thing is to call the method " FillGrid ()" when you want to print the data. And finaly, I resolved it!
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