property return sevral value - vb.net

I want to get several value from a read only property.in Below is my code
Public Class Class1
ReadOnly Property Ca As New Class2
End Class
Public Class Class2
ReadOnly Property getass(q As Integer, ww As String) As Integer
Get
Codes that return q And ww
End Get
End Property
End Class
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim a As New Class1
Dim ret As Integer
Dim qq As Integer = Nothing
Dim qqq As String = Nothing
ret = a.Ca.getass(qq, qqq)
End Sub
End Class
I want finally get qq=q and qqq=ww...
thanks

You don't want to use a Property for this purpose.
Instead just declare a Sub that modifies the passed parameters like this:
Public Class Class2
Sub getass(ByRef q As Integer, ByRef ww As String)
Dim _q as Integer
Dim _w as String
'do whatever you want
'then assign the final values and end sub
q = _q
ww = _ww
End Sub
End Class
Then use the sub like this:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim a As New Class1
Dim qq As Integer = Nothing
Dim qqq As String = Nothing
a.Ca.getass(qq, qqq)
'At this point your local qq and qqq will have the value setted by the getass Sub
End Sub
End Class
Please consider that this is not a good design pattern for your final intent of having a student ID and Name.
Consider creating a Class "Student" with all the properties you want and make Class2 (I can suppose is a classroom or something like that) return a "Student" object.
Or you could use a KeyValuePair structure
EDIT:
If you still want to do this through an interface try this:
Public Class Class2
Public ReadOnly Property getass(ByRef q As Integer, ByRef ww As String) as Integer
Get
Dim _q as Integer
Dim _w as String
'do whatever you want
'then assign the final values and end sub
q = _q
ww = _ww
return ID 'ID is what you want (Integer)
End Get
End Property
End Class

Related

How do you pass values from a textbox to a public property in a class?

I need to pass code that user enters into a Textbox to a Public Property within a class. Here is my code.
Form2.vb Code
Public Class Form2
Dim class2A As part2Class = New part2Class()
Dim class2B As part2BClass = New part2BClass()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a As Integer = CType(TextBox1.Text, Integer)
Dim b As Integer = CType(TextBox1.Text, Integer)
part2Class._Num1 = a
part2Class._Num2 = b
End Sub
Here is my code in part2Class.vb
Public Class part2Class
Public Property _Num1
Public Property _Num2
Public Overridable Function Calculate() As Integer
Return _Num1 + _Num2
End Function
End Class
I get an error saying "Reference to a non-shared member requires an object reference." How do I pass the values from the textboxes to the public property values?
Thanks!
You need to create an instance of a class first before accessing it's members:
Dim objpart2Class as part2Class = new part2Class()
objpart2Class._Num1 = a
objpart2Class._Num2 = b

ArrayList change values when i add a new one VB.NET

So here is my problem. I have a Class of Products and i have created an ArrayList of that class.
I can add lot of new Products to that Array but the problem is that the Array change all the Values of the Array as the last one i have added.
Product Class:
Public Class Product
Dim cod_prod As String
Dim state As Boolean
Public Sub New(ByVal cod As String, ByVal est As Boolean)
cod_prod = cod
state = est
End Sub
Public Sub New()
cod_prod = ""
state = False
End Sub
Public Function get_cod_prod() As String
Return cod_prod
End Function
Public Function get_state() As Boolean
Return state
End Function
Public Sub set_cod_prod(ByVal cod As String)
cod_prod = cod
End Sub
Public Sub set_state(ByVal est As Boolean)
state = est
End Sub
End Class
And this class is were i add a new Product to the ArrayList.
Dim array_prod As New ArrayList
Dim nproducts As Integer = 0
Public Sub add_prod(ByVal prod As Producto)
array_prod.Add(prod)
nproducts += 1
End Sub
Thanks for help.
Ok, looking a bit more on related questions i have found this.
Retrieving data from a VB.NET arraylist of objects
So i solve it changing the method like this:
Public Sub add_prod(ByVal prod As Producto)
Dim nprod As New Producto
nprod.set_cod_prod(prod.get_cod_prod)
nprod.set_state(prod.get_state)
array_prod.Add(nprod)
nproductos += 1
End Sub

Assign a value to a property depending on initialized Class

I try to assign a value (Test1) to a Property (Wealth) dynamically so that depending on the initialized Class the calculated value is different. But all I get as a result is 0. Could anyone explain me why and how I can solve the problem.
Public Class Class1
Private _test1 As Integer
Overridable ReadOnly Property Test1 As Integer
Get
Return _test1
End Get
End Property
Public ReadOnly Property Wealth As Integer
Get
Dim rnd As New Random
Dim val As Integer = rnd.Next(1, 6)
Return val * _test1
End Get
End Property
End Class
Public Class Class2
Inherits Class1
Public Overrides ReadOnly Property Test1 As Integer
Get
Return 3
End Get
End Property
End Class
Initialisation:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim t As New Class2
MsgBox(t.Wealth.ToString)
End Sub
End Class
Don't use the private variable, you need to reference the property itself.
Public Class Form1
Public Class Class1
Overridable ReadOnly Property Test1 As Integer
Get
Return 0 'Default value'
End Get
End Property
Public ReadOnly Property Wealth As Integer
Get
Dim rnd As New Random
Dim val As Integer = rnd.Next(1, 6)
Return val * Test1 'Changed! Uses the Property name, so that if it is overridden it uses the new version'
End Get
End Property
End Class
Public Class Class2
Inherits Class1
Public Overrides ReadOnly Property Test1 As Integer
Get
Return 3
End Get
End Property
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim t As New Class2
MsgBox(t.Wealth.ToString)
End Sub
End Class
Sounds like you need a constructor.
Public Class Class1
Public Sub New(int As Integer)
Me.test1 = int
End sub
...
Then when you declare it
Dim t As New Class1(5)
MsgBox(t.Wealth.ToString)

Passing Simple Integer to Form1 From Form2

Just trying to figure out how to pass one simple integer(StartingTeam) from Form2 to Form 1.
Form 2 Code
Public Class frmTeamChoose
Public StartingTeam As Integer
Public Sub btnTeam1_Click(sender As Object, e As EventArgs) Handles btnTeam1.Click
StartingTeam = 1
End Sub
Public Sub btnTeam2_Click(sender As Object, e As EventArgs) Handles btnTeam2.Click
StartingTeam = 2
End Sub
End Class
Form 1 is called Form1
Although you need to provide more info I 'll try to help you:
I suppose that you have open Form2 from Form1:
'In form1:
Dim k as integer=Form2.StartingTeam
An improved solution is to create a property:
Form 2 code:
Private miStartingTeam
Public Property StartingTeam As Integer
Get
Return miStartingTeam
End Get
Set(ByVal value As Integer)
miStartingTeam = value
End Set
End Property
Then your code as it is.
In Form1:
'Open Form2
Dim f2 as new Form2
'f2.StartingTeam=1 'if you want to set a value before f2 opening
f2.Show
'Get StartingTeam from f2
dim k as integer=f2.StartingTeam
Let me know if you need anything else

VB.Net Create Extension As DataTable(Index)

I have a question related with VB.Net Code.
I see that the DataTable we can use:
DataTable(0) ' This return a DataRow with the selected index
In the intellisense I see that this functionality can achieve with a extension... but, If I create a extension, always I need refer the Extension before to use it
Public Module asdadsdas
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)>
<System.Runtime.CompilerServices.ExtensionAttribute()>
Friend Function MyExt(ByVal pMyObject As MyObject, ByVal ColumnName As String) As MyObject.ColumnData
Return pMyObject.Columns(0)
End Function
End Module
Public Class MyObject
Friend Structure ColumnData
Friend vNombre As String
Friend vApellido As String
Friend vTelefono As String
Public Property Nombre As String
Get
Return Me.vNombre
End Get
Set(ByVal value As String)
Me.vNombre = value
End Set
End Property
End Structure
Friend Columns() As ColumnData
Public Sub add(ByVal MyColumn As String)
ReDim Columns(0)
Columns(0).vNombre = MyColumn
End Sub
End Class
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Test As New MyObject
Test.add("TEST 001")
' HERE IS THE PROBLEM
Test("TEST 001")
' CORRECT USE
Test.MyExt("TEST 001")
End Sub
End Class
Now, my concrete question: How I can make a default extension in a specific Object?
#competent_tech: man you are a genius... thanks by your comment!
This is the solution of my problem:
Public Class ColumnData
Friend Name As String
Friend LastName As String
Friend Phone As String
End Class
Public Class MyColumns
Friend Data() As ColumnData
Default Property Item(ByVal ColumnName As String) As ColumnData
Get
Return Data(0)
End Get
Set(ByVal value As ColumnData)
End Set
End Property
Public Sub add(ByVal Name As String, ByVal LastName As String, ByVal Phone As String)
If Data Is Nothing Then
ReDim Data(0)
Data(0) = New ColumnData
End If
With Data(0)
.Name = Name
.LastName = LastName
.Phone = Phone
End With
End Sub
End Class
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Test As New MyColumns
Test.add("Name 001", "bolanos.m", "500-2004-1000")
Debug.Print(Test("Name 001").LastName & " - " & Test("Name 001").Phone)
' bolanos.m - 500-2004-1000
Test("Name").LastName = "BOLANOS.M MODIFY"
Debug.Print(Test("Name 001").LastName & " - " & Test("Name 001").Phone)
' BOLANOS.M(MODIFY - 500 - 2004 - 1000)
End Sub
End Class
Why not just add default properties to the MyObject class?
Default Public Property IndexedColumn(index As String) As ColumnData
Get
Return Columns(index)
End Get
Set(value As ColumnData)
Columns(index) = value
End Set
End Property
Default Public Property IndexedColumn(index As Integer) As ColumnData
Get
Return Columns(index)
End Get
Set(value As ColumnData)
Columns(index) = value
End Set
End Property
You will need to change the exposure level of the structure and the access will be:
Dim oColumnData = Test("TEST 001")