Overriding/Inheriting Class Constructor Methods...

C

Carl Fenley

I've been programming exclusively in C# for the last few years but am now
working on a project where I am required to write all code in VB.NET.

I'm trying to create a class with multiple overrideable contructor methods.
The problem is that I don't know how to even define a contructor in VB.NET,
let alone inherit the default contructor. Here is what I have so far:

Public Class AppSnippets
Private _instances As Integer
Private _appProcess As Process
Private _sysProcesses As Process()

Sub New()
Me._instances = 0
End Sub

Sub New(ByVal appProcess As Process)
MyBase.New()
Me._appProcess = appProcess
End Sub
End Class

I don't really know if "Sub New()" is the constructor. If it is, I need the
constructor that accepts the Process parameter to call the previous
constructor first. Any help and clarification would be greatly appreciated.

carl
 
J

Jay B. Harlow [MVP - Outlook]

Carl,
| I don't really know if "Sub New()" is the constructor.
Yes "Sub New" is the constructor.


| If it is, I need the
| constructor that accepts the Process parameter to call the previous
| constructor first. Any help and clarification would be greatly
appreciated.
You can use MyBase.New to call your parent's constructor.

You can use MyClass.New to call one of your constructors.

For example:

| Sub New(ByVal appProcess As Process)
| MyClass.New()
| Me._appProcess = appProcess
| End Sub
| End Class

Hope this helps
Jay

| I've been programming exclusively in C# for the last few years but am now
| working on a project where I am required to write all code in VB.NET.
|
| I'm trying to create a class with multiple overrideable contructor
methods.
| The problem is that I don't know how to even define a contructor in
VB.NET,
| let alone inherit the default contructor. Here is what I have so far:
|
| Public Class AppSnippets
| Private _instances As Integer
| Private _appProcess As Process
| Private _sysProcesses As Process()
|
| Sub New()
| Me._instances = 0
| End Sub
|
| Sub New(ByVal appProcess As Process)
| MyBase.New()
| Me._appProcess = appProcess
| End Sub
| End Class
|
| I don't really know if "Sub New()" is the constructor. If it is, I need
the
| constructor that accepts the Process parameter to call the previous
| constructor first. Any help and clarification would be greatly
appreciated.
|
| carl
|
|
 
A

alantolan

More FYI for than anything else...

Just in case you see this and are wondering - with the constructor you
can use Me.New() interchangeably with MyClass.New(). They act the same
way as ctors cannot be overriden

Haven't used C# enough to know if it is different or not but in VB.Net,
integers are auto-initialized to 0. There is no need to explicitly set
them.
 
H

Herfried K. Wagner [MVP]

Chad Z. Hower aka Kudzu said:
Thats a .NET thing, not a C# or VB thing.

When attempting to compile the code below, the compiler will answer with a
CS0165 compiler error ("Use of unassigned local variable 'var'"):

\\\
int x;
Console.WriteLine(x);
///
 
C

Chad Z. Hower aka Kudzu

Herfried K. Wagner said:
When attempting to compile the code below, the compiler will answer
with a CS0165 compiler error ("Use of unassigned local variable
'var'"):

\\\
int x;
Console.WriteLine(x);

For locals yes, but not fields. He didnt specify which he was using, I assumed fields which may
have been an error.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
 
A

alantolan

We were talking about fields ( _instances, in the AppSnippets class)
but in VB.Net

Dim x as integer
Console.Writeline(x)

works fine in both debug and release builds.
 
H

Herfried K. Wagner [MVP]

Chad Z. Hower aka Kudzu said:
For locals yes, but not fields. He didnt specify which he was using, I
assumed fields which may
have been an error.

ACK, you are right. I should have read the whole thread more carefully ;-).
 
J

Jay B. Harlow [MVP - Outlook]

alantolan,
Yes they "act the same" in constructors.

However! There in lies the danger!

If you get use to the constructor semantics of Me.New & MyClass.New and
apply it to an overridable method, Me.MyMethod & MyClass.MyMethod you might
get burnt, as they now have different semantics.

I would recommend using MyClass.New to be consistent with MyClass.MyMethod.

Hope this helps
Jay

| More FYI for than anything else...
|
| Just in case you see this and are wondering - with the constructor you
| can use Me.New() interchangeably with MyClass.New(). They act the same
| way as ctors cannot be overriden
|
| Haven't used C# enough to know if it is different or not but in VB.Net,
| integers are auto-initialized to 0. There is no need to explicitly set
| them.
|
 
A

alantolan

It was an informational comment in case the OP saw code using Me.New()
and was wondering about it.

====

On the topic of MyClass though, it always struck me as solution looking
for a problem. I would be interested in hearing opinions on its
usefulness.


When developing a class I get to choose which methods are overridable
or not and methods should only be overridable if I am planning for them
to be overridden (and want/need them to be).

By using MyClass it seems that I am saying, 'Nope, I made a mistake, it
really shouldn't be overridable - always use the version here.'


Why wouldn't I just not make the function overridable?

I can probably come up with some classroom examples of where is can be
used, but I was wondering if anyone out there has any 'real-world'
examples for it.

Thanks,
Alan.
 
J

Jay B. Harlow [MVP - Outlook]

Alantolan,
| When developing a class I get to choose which methods are overridable
| or not and methods should only be overridable if I am planning for them
| to be overridden (and want/need them to be).
You don't get to choose when you inherit from a framework class & your
descendents (classes that inherit from you) may need to override the method.

| I can probably come up with some classroom examples of where is can be
| used, but I was wondering if anyone out there has any 'real-world'
| examples for it.
Unfortunately I cannot think of a good "real world" example, nor a text book
example.

IMHO Your example is all the more reason to use MyClass & Me appropriately,
if you get use to Me thinking its OK, you may not realize or forget to
realize that wait, it does something different...

Hope this helps
Jay

|
| It was an informational comment in case the OP saw code using Me.New()
| and was wondering about it.
|
| ====
|
| On the topic of MyClass though, it always struck me as solution looking
| for a problem. I would be interested in hearing opinions on its
| usefulness.
|
|
| When developing a class I get to choose which methods are overridable
| or not and methods should only be overridable if I am planning for them
| to be overridden (and want/need them to be).
|
| By using MyClass it seems that I am saying, 'Nope, I made a mistake, it
| really shouldn't be overridable - always use the version here.'
|
|
| Why wouldn't I just not make the function overridable?
|
| I can probably come up with some classroom examples of where is can be
| used, but I was wondering if anyone out there has any 'real-world'
| examples for it.
|
| Thanks,
| Alan.
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top