How do you pass values from a textbox to a public property in a class? - vb.net

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

Related

How to display public variables in another form

I've already searched this up many times but none of them worked for me so please help. The code I've been trying to use is:
'Making the variables public in form2
Public Module GlobalVariables
'Making the variables public
Public Property Juvenplp As Integer
Public Property Adultplp As Integer
Public Property Senileplp As Integer
Public Property Juvensr As Single
Public Property Adultsr As Single
Public Property Senilesr As Single
Public Property Birthrate As Single
Public Property genmore As Integer
Public Property i As Integer
End Module
Displaying then in Form4
Public Sub TextBox1_TextChanged(sender As Object, e As EventArgs)
InitializeComponent()
GlobalVariables.Juvenplp = Me.Label7.Text
GlobalVariables.Adultplp = Me.Label8.Text
GlobalVariables.Senileplp = Me.Label9.Text
GlobalVariables.Juvensr = Me.Label10.Text
GlobalVariables.Adultsr = Me.Label11.Text
GlobalVariables.Senilesr = Me.Label12.Text
GlobalVariables.Birthrate = Me.Label14.Text
End Sub
The problem is that they're not being displayed.

property return sevral value

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

Removing an object from List (Of T)

I'm not able to remove an object from my List (of contact)
Here are my fields:
Public Class Contact
'Things to remember
Private m_firstName As String = String.Empty
Private m_lastName As String = String.Empty
Private m_address As Address
My list:
Public Class ContactManager
Private m_contactRegistry As List(Of Contact)
Public Sub New()
m_contactRegistry = New List(Of Contact)()
End Sub
My method in ContactManger Class. Here I'm getting error "Value of type 'Integer' cannot be converted to Assignment.Contact" on the index
Public Function DeleteContact(index As Integer) As Boolean
m_contactRegistry.Remove(index)
Return True
End Function
My delete button method on my Main class:
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
'listResults is my listbox
Dim list = listResults.SelectedIndex
'm_contact is an object of the Contact class
m_contacts.DeleteContact(list)
UpdateGUI()
End Sub
The problem is that I don't know how to do the method DeleteContact(index As Integer) without getting an error. Do you guys have a suggestion?
When using an index, you need RemoveAt() rather than Remove()

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)

sort results (find method) not working

I am using ArcGIS. I am trying to sort the data manually after its found. I created a property class and loop through a Tlist to scrub unwanted data. However right before it binds to the data grid, I receive a cast error. I assume something is coming back null. Am I missing something??
Public Class temp
Public Sub New()
End Sub
Public Property DisplayFieldName As String
Public Property Feature As ESRI.ArcGIS.Client.Graphic
Public Property FoundFieldName As String
Public Property LayerId As Integer
Public Property LayerName As String
Public Property Value As Object
End Class
Public Class templst
Public Sub New()
Dim findresult = New List(Of temp)
End Sub
Private _findresult = findresult
Public Property findresult() As List(Of temp)
Get
Return _findresult
End Get
Set(ByVal value As List(Of temp))
_findresult = value
End Set
End Property
End Class
Private Sub FindTask_Complete(ByVal sender As Object, ByVal args As FindEventArgs)
Dim newargs As New templst() 'puts Tlist (temp) into property
Dim templistNUMONLY As New List(Of temp) 'Tlist of temp
For Each r In args.FindResults 'class in compiled dll.
If Regex.Match(r.Value.ToString, "[0-9]").Success Then
templistNUMONLY.Add(New temp() With {.LayerId = r.LayerId,
.LayerName = r.LayerName,
.Value = r.Value,
.FoundFieldName = r.FoundFieldName,
.DisplayFieldName = r.DisplayFieldName,
.Feature = r.Feature})
End If
Next
newargs.findresult = templistNUMONLY
Dim sortableView As New PagedCollectionView(newargs.findresult)
FindDetailsDataGrid.ItemsSource = sortableView 'populate lists here
End Sub
BIND TO GRID HERE: (Error here)
Private Sub FindDetails_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
' Highlight the graphic feature associated with the selected row
Dim dataGrid As DataGrid = TryCast(sender, DataGrid)
Dim selectedIndex As Integer = dataGrid.SelectedIndex
If selectedIndex > -1 Then
'''''''''''''''''CAST ERROR HERE:
Dim findResult As FindResult = CType(FindDetailsDataGrid.SelectedItem, FindResult)
You populate FindDetailsDataGrid with objects of type temp, but you try to cast it to type FindResult instead. You should do:
Dim selectedTemp As temp = CType(FindDetailsDataGrid.SelectedItem, temp)
'*Create find result from selected temp object here*