Click here to Skip to main content
15,886,038 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi all,

I have 2 simple classes : Events and BMD. And here is the code :

Events:
VB
Public Class Events
    Implements IEvents

    Private bmdd As BMD

    Public Sub New(ByRef master As Object)
        Me.bmdd = master
    End Sub

    Public Function getevents1() As String Implements IEvents.getevents
        Return ""
    End Function
End Class


in the BMD i have this property where _eventfield is object of type Events :

VB
Public Property _eventproperty() As IEvents Implements IBMD._eventproperty
       Get
           If _eventfield Is Nothing Then
               _eventfield = New Events(Me)
           End If
           Return _eventfield
       End Get
       Set(ByVal value As IEvents)
           If _eventfield Is Nothing Then
               _eventfield = New Events(Me)
           End If
       End Set
   End Property


So when i do :
VB
_eventfield = New Events(Me)


It is calling the constructor of Events.

So my question is what does
VB
Me.BMD= master
means ?

Thanks.
Posted
Updated 11-Feb-12 4:08am
v2

It assigns an Object to a variable... However, the variable is never used, the variable is of type BMD while the object assigned to it is of the type Object (which may or may not be of the type BMD). I suggest turning Option Strict On[^] and see the error appearing! A GetEvents that returns a String is peculiar to say the least. This piece of code is just strange and has actually nothing to do with OOP... Assigning values to variables is the absolute basics. For more information on OOP I suggest reading this: Introduction to Object Oriented Programming Concepts (OOP) and More[^]
But perhaps you'd be more helped by grabbing some beginner books or articles on programming in VB.NET... Such as this one[^].
 
Share this answer
 
v2
Comments
mhamad zarif 11-Feb-12 10:17am    
thanks, but my problem is that how it is assigning and object of type Events to BMD ? knowing that when i debug the code, the master is appearing to be of type BMD which is confusing me.
Sander Rossel 11-Feb-12 10:28am    
You are missing the point that BMD is ALSO an Object.
So you are saying the constructor needs an Object, and you are passing in a BMD, which is a perfectly valid Object. However, EVERYTHING in .NET is an Object. So this might go very wrong if you are passing in, for example, a String to the constructor. A String is an Object, so the compiler won't complain. Until your String is assigned to a variable that holds a BMD during runtime.

Imagine this: You are asking a friend if he likes a cup of tea. He says sure and you give him a cup of green tea. When taking his first sip he spits it out and gets mad at you because he really hates green tea! Despite green tea being a tea it's not the kind of tea your friend was expecting. Had you given him a lemon tea he might have been happy.
The thing with your code is that BMD is an Object, but the master variable is not necissarily holding the correct Object.
mhamad zarif 11-Feb-12 11:22am    
But it is not giving me any error
mhamad zarif 11-Feb-12 11:25am    
Also the master object is of type Events.
Sergey Alexandrovich Kryukov 11-Feb-12 14:59pm    
Then why did not you use the parameter type as Events? And what would be the sense of this parameter?

All wrong. You've just totally lost. This cannot be fixed. As Naering advised, read the book/manual.

You need to understand the very elementary basics: types (run-time and compile-time), methods, assignment, parameters, types and instances, static and instance methods, etc. You need to start from scratch.

--SA
First of all, Me means the reference to the object implicitly passed as a hidden parameter to an instance (non-static) method in order to allow this method to assess the instance. In other words, suppose you have this:
VB
myInstance.MyMethod(someParameter);

This is equivalent to this pseudo-code:
VB
TheClassOfMyInstance.MyMethod(myInstance, someParameter);

This pseudo-code could be used as a real code demonstrating simulation of an instant method by a static version of the method MyMethod.

Now, the assignment me.bmdd = master is wrong, because master is Object, but bmdd is BMD. You cannot assign more specialized instance to less specialized, because BMD has more members then Object and an attempt to access some of those extra members would cause a fatal error. Fortunately, the system won't let you go that far. If you force this assignment by casting to BMD, the cast itself will trow exception in all cases where the instance of master is not of the run-time type BMD or derived from this type.

Note that it is important to distinguish compile-type name and run-time name.

I agree with Naerling about learning some programming and language.

It's very bad to hard-code any immediate constants, especially strings; even instead of "" should be string.Empty.

Actually, the signature of the constructor New makes no sense due to the reason explained above. You could use Public Sub New(ByRef master As BMD). In comments you say that the actual parameter is of the type Events. If so, the constructor would never work at all. And if you used the signature Public Sub New(ByRef master As Events) it would make no sense.

A couple of notes:

The following names violate very useful Microsoft naming conventions: Events (class name can not be plural), _eventfield (don't use underscore), getevents1 (you should use camel case for method names, not numerals).

—SA
 
Share this answer
 
v3
Comments
mhamad zarif 11-Feb-12 16:01pm    
Thank you for your help my friend. Actually this code is what i saw in my new job that i told you about and it is not generating any error at all even when i debug the code. And my problem was is that the master is an object of type Event and iam doin me.Bmd=master so how it is not generating any error. Anyway i reel that i will not be able to stay at this job because they have a very high level and works oop in a hard way other than the way i do. Please give me an advice. You know how much i do trust you.
Sergey Alexandrovich Kryukov 11-Feb-12 17:54pm    
If you say "no errors" you should explain is it about compilation or run-time exception. Even the absence of both does not mean the code is correct.

The code with assigning events in constructor has nothing to do with the patterns of using events. So, in practice, the discussion of this constructor does not make much sense. You can read about using events in any language manual. You really need it, because events are used everywhere. And you need to know delegates before you proceed with events. Events are closer to advanced part of learning, and you still have a problem with types and objects/instances, methods, etc.

So, I don't really know how about your employment, or I'm afraid to make a mistake. You practically need to learn programming from scratch; and pretty high level of OOP is a commonplace these days. Real OOP starts from virtual methods and late binding. You can start effective work only if you came to this point and made yourself very comfortable with this. But it looks like you just need to start from scratch and complete the education. I have no ideas how much time you might need to come to speed. I would say, an year, quite optimistically. But I'm afraid to be wrong and hurt you anyhow.

I sincerely wish you to overcome your problems, but it means regular hard work.
--SA
Sander Rossel 11-Feb-12 17:32pm    
My 5. All valid points. And I must confess I still use the _underScore naming convention for fields :)
Sergey Alexandrovich Kryukov 11-Feb-12 17:42pm    
Thank you, Naerling. We all have a room for improvements. :-)
--SA
Sander Rossel 11-Feb-12 17:47pm    
I think if the only coding 'horror' you'd find was an underscore for field variables you'd be a very very happy and spoiled programmer ;p

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