Self or no Self

I have created a class called TpSensor and have a constructor used to set passed properties for each New sensor created (Sensors.Append(New TpSensor(“Storage Area”,2,“PWA2”,“LowTemp”))). I am curious why this works:

Self.Sensor_Name = Sensor_Name
Self.Sensor_Number = Sensor_Number
Self.Sensor_Bank = Sensor_Bank
Self.Sensor_Conversion_Type = Sensor_Conversion_Type

and this does not:

Sensor_Name = Sensor_Name
Sensor_Number = Sensor_Number
Sensor_Bank = Sensor_Bank
Sensor_Conversion_Type = Sensor_Conversion_Type

I thought the default was Self.

You need self to reference a property with same name as the parameter name.

1 Like

Unless a local variable, such as a parameter, has the same name.

1 Like

or you could rename your arguments
Sensor_Name = Name

3 Likes

Excellent piece of information and in my case I would be correct to pass it with another name. I will leave it as is so I remember it. Thanks

I’ll sometimes name them with the additional word “input” if I can’t come up with anything clever.
Method MyMethod(Input_Sensor_Name as String, Input_Sensor_Number as Integer)

Is there a reason you would not want to use Self. to allow it to work with the same parameter and property name?

Clarity in naming. I hate bad naming. If two things have the same name, one needs to be more specific.

Yes, I might think it could lead to confusion.

In the statement:
Sensor_Name = Sensor_Name

The complier cannot be sure which ‘Sensor_Name’ you are referring to (on either side of the equation), so as a default it always uses the most local instance of the identifier, which in this case, is a variable.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.