Click here to Skip to main content
15,909,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone

I have a class called Car:
VB
Public Class Car
    'Property
    Public theColor() As Color

    'Method
    Public speed As Double
    Public Function run()
        Return speed
    End Function
End Class


In another class I create an instance of Car Class:
VB
Public Class SUV
    Dim newCar As New Car

    Private Sub setColor()
        'set theColor property
        newCar.theColor = Color.Red
    End Sub

    Private Sub testDrive()
        'call run() function
        newCar.run()
    End Sub
End Class


From the code above,
If I want to set a property of a class I just write an instance and the property:
newCar.theColor = Color.Red
Also if I want to call a method of a class I just write an instance then call the function or sub:
newCar.run()

But I dont understand how can a line of code be like this, for example:
VB.NET
Dim dgv As New DataGridView
dgv.Rows.Add()

The code above show that an instance (dgv) call property (Rows) than call a method (Add())


Back to my class (the Car And SUV)
How to write the code in my class if i want to call the method like this?

VB
newCar.theColor.resetColor()


And another code:
VB
newCar.theColor.setColor() = Color.Red


I am sorry if the question's title isnt match with what i've asked.

Thanks

What I have tried:

I have no idea how to to try to solve my question above
Posted
Updated 12-Sep-16 23:44pm

When you write this:
VB
Dim dgv As New DataGridView
dgv.Rows.Add(obj)
You create an instance of the DataGridView, and then use it:
dgv.Rows is a Property of DataGridView which returns a n instance of the DataGridViewRowCollection class which is the rows in the DGV: MSDN[^]
The DataGridViewRowCollection has a Method Add which adds a row to the collection, and hence to the DGV.
To do something similar, you'd need an extra class:
VB
Public Class MyColor
	Public Sub ResetColor()
	End Sub
End Class
Public Class Car
	Public Property TheColor() As MyColor
		Get
			Return m_TheColor
		End Get
		Set
			m_TheColor = Value
		End Set
	End Property
	Private m_TheColor As MyColor
End Class
...
Public Sub myMethod()
	Dim newCar As New Car()
	newCar.TheColor.ResetColor()
End Sub



Quote:
an we do the similiar way to create property or method in another language? like java or c#. And is this a part of OOP concept? what did i miss in my learning of programming?


Yes. For example, the above in C# is:
C#
public class MyColor
    {
    public void ResetColor()
        {
        }
    }
public class Car
    {
    public MyColor TheColor { get; set; }
    }
...
    public void myMethod()
        {
        Car newCar = new Car();
        newCar.TheColor.ResetColor();
        }

And it's similar code for Java.
Properties aren't strictly part of OOPs - they are just "syntactic sugar" for the method calls involved that let you treat the property as a variable - but most OOPs languages implement them.

What did you miss? :laugh: I don't know, I wasn't there!
 
Share this answer
 
v2
Comments
Mang Irpan 12-Sep-16 23:57pm    
It's now clear, Thank you.

Can we do the similiar way to create property or method in another language? like java or c#. And is this a part of OOP concept? what did i miss in my learning of programming?
OriginalGriff 13-Sep-16 5:21am    
Answer updated.
Mang Irpan 13-Sep-16 21:53pm    
Thank you for your helpful asnwer
OriginalGriff 14-Sep-16 3:48am    
You're welcome!
I'd strongly recommend to read about Inheritance[^] and Polimorphism[^].

A Car class have to be a base class. A Suv class should inherits from Car (as it is a type of Car). Am i right?

For example:
VB.NET
Public Class Car
	
	Private dc As Integer = 5
	Private c As Drawing.Color = Drawing.Color.Gray
	
	Public Sub New ()
		'default constructor
		
	End Sub
	
	Public Sub New (ByVal doorsCount As Integer, ByVal color As Drawing.Color)
		dc = doorsCount
		c=  color
	End Sub
	
	Public Property DoorsCount As Integer
		Get
			Return dc
		End Get
		Set (ByVal value As Integer)
			dc = value
		End Set
	End Property
	
	Public Property Color As Drawing.Color
		Get
			Return c
		End Get
		Set (ByVal value As Drawing.Color)
			c = value
		End Set
	End Property

End Class

Public Class Suv
	Inherits Car
	
	Public Sub New ()
		'default constructor
		MyBase.DoorsCount = 4
                MyBase.Color = Drawing.Color.Green		
	End Sub
	
	Public Sub New (ByVal doorsCount As Integer, ByVal color As Drawing.Color)
		MyBase.DoorsCount = doorsCount
		MyBase.Color =  color
	End Sub
	
	
End Class



[EDIT#1]

Usage:
VB.NET
Dim carcar As Car = New Car()
Console.WriteLine(New String("-", 25))
Console.WriteLine("Car details:")
Console.WriteLine("  - doors: {0}", carcar.DoorsCount)
Console.WriteLine("  - color: {0}", carcar.Color.ToString())


Dim suvcar As Suv = New Suv()
Console.WriteLine(New String("-", 25))
Console.WriteLine("Suv details:")
Console.WriteLine("  - doors: {0}", suvcar.DoorsCount)
Console.WriteLine("  - color: {0}", suvcar.Color.ToString())

suvcar.DoorsCount = 3
suvcar.Color = Drawing.Color.Yellow

Console.WriteLine(New String("-", 25))
Console.WriteLine("Suv details:")
Console.WriteLine("  - doors: {0}", suvcar.DoorsCount)
Console.WriteLine("  - color: {0}", suvcar.Color.ToString())


Result:
-------------------------
Car details:
  - doors: 5
  - color: Color [Gray]
-------------------------
Suv details:
  - doors: 4
  - color: Color [Green]
-------------------------
Suv details:
  - doors: 3
  - color: Color [Yellow]




[EDIT #2]
As you can see, you don't need getter/setter inside custom method. You can achieve that using only properties.

Try! Good luck!
 
Share this answer
 
v4
Comments
Mang Irpan 13-Sep-16 21:57pm    
I thought i understood what OOP is :D, but actually not yet.
Thanks for you suggestion and example.
Maciej Los 14-Sep-16 0:05am    
If my answer was helpful, you can accept it as a solution.
Oop is very interesting. The proccess of learning is very long...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900