Newbie studying MCSD.NET

mattcya

Member
Joined
Jun 18, 2004
Messages
16
Programming Experience
Beginner
Hi All,

I am new and studying my MCSD.NET i am currently trying to create a constructor in a class that inherits from the base class, that refers to a constructor in the base class

I know that you use the mybase keyword to create the constructor but i am getting stuck with the code.

Example

Public Class Developer

'This will Inherit from Employee

Inherits Employee

Sub New(ByVal name As String, ByVal salary As Double)

'calls the constructor

MyBase.New() needs name as string and salary as double??



End Sub

End
Class

Can someone help me?

Cheers

Matt
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
we would need to see the constructor in the base class (Employee) to know.
If the base classes' signature is the same as the Developer (as I assume it is) you probably would want to just pipe the parameters used to initialize the developer class through to the base class:

VB.NET:
Sub New(ByVal name As String, ByVal salary As Double)
    'calls the constructor of the base class 'Employee'
    MyBase.New(name, salary)
End Sub
As is usually the case with classes built upon other classes, there could be additional parameters in the contructor:

VB.NET:
Private _strLanguage as string

Sub New(ByVal name As String, ByVal salary As Double, _
  ByVal ProgrammingLanguage as string)
    'calls the constructor of the base class 'Employee'
    MyBase.New(name, salary)
    _strLanguage = ProgrammingLanguage 
End Sub
And with the overloaded capabilities of Object-Oriented Programming (VB.NET is an OOP language), you could use both constructors in the same class.
 

mattcya

Member
Joined
Jun 18, 2004
Messages
16
Programming Experience
Beginner
Cool it is sort of making sense to me.

What i need to do is create a constructor and add a value of 1000 to an integer variable called mintid

Any suggestions

Cheers

Matt
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
Where is the variable called mintid? Is it a parameter or a class member variable?

The code I posted above should show how. :)
 

mattcya

Member
Joined
Jun 18, 2004
Messages
16
Programming Experience
Beginner
ok i see, but can you explain a little more detail regarding _strLanguage = ProgrammingLanguage does this mean say for example mintid = ??
 

mattcya

Member
Joined
Jun 18, 2004
Messages
16
Programming Experience
Beginner
here is the code to the employee class if it helps ;) thatnks i might have to post you a beer hehehe.


Public Class Employee

Private Shared mCounter As Integer

Protected mintID As Integer

Private mstrName As String

Private mdblSalary As Double

Public Sub New(ByVal name As String, ByVal salary As Double)

mstrName = name

mdblSalary = salary

mCounter += 1

mintID = 1000 + mCounter

End Sub

ReadOnly Property ID() As Integer

Get

Return mintID

End Get

End Property

Property Name() As String

Get

Return mstrName

End Get

Set(ByVal Value As String)

mstrName = Value

End Set

End Property

Property Salary() As Double

Get

Return mdblSalary

End Get

Set(ByVal Value As Double)

mdblSalary = Value

End Set

End Property

Public Function VacationLength() As String

Return "2 weeks"

End Function

Friend Function CheckCounter() As Integer

Return mCounter

End Function

End
Class



 
Top Bottom