ColdFusion 9 CFScript Private Properties and Public Properties - oop

Is there a way to make isDevMode, devModeToEmailAddress, devModeFromEmailAddress to be private properites?
Code:
/**
* email
* #accessors true
*/
component email output="false" hint="This is email object." {
/* properties */
property name="toEmailAddress" type="string";
property name="fromEmailAddress" type="string";
property name="subject" type="string";
property name="body" type="string";
property name="attachments" type="array";
/*
private isDevMode
private devModeToEmailAddress
private devModeFromEmailAddress
*/
}

You can add setter="false" and getter="false" to prevent getters and setters, but you can't restrict access to the properties directly. Your best bet is put those into your constructor in the component's local scope.
/**
* email
* #accessors true
*/
component email output="false" hint="This is email object." {
isDevMode = false;
devModeToEmailAddress = "foo#foo.com";
devModeFromEmailAddress = "bar#foo.com";
/* properties */
property name="toEmailAddress" type="string";
property name="fromEmailAddress" type="string";
property name="subject" type="string";
property name="body" type="string";
property name="attachments" type="array";
}
Then, when you need to use those, just reference variables.isDevMode in any function to pick up the value. If you need to set those at runtime, you can set them in the init() method for your function. I usually do it like this:
component email output="false" hint="This is email object." {
instance = {};
/* properties */
property name="toEmailAddress" type="string";
property name="fromEmailAddress" type="string";
property name="subject" type="string";
property name="body" type="string";
property name="attachments" type="array";
public email function(required boolean isDevMode, required string devModeToEmailAddress, required string devModeFromEmailAddress){
variables.Instance.isDevMode = Arguments.isDevMode;
variables.Instance.devModeToEmailAddress = Arguments.devModeToEmailAddress;
variables.Instance.devModeFromEmailAddress = Arguments.devModeFromEmailAddress;
{
}
Then, any time I need those values I just get variables.Instance.isDevMode. I also create a generic get() method that will return the variables.instance so I can see what's in there.
public struct function get(){
return Duplicate(variables.Instance);
}
But because these are in the components local variables scope, they can't be modified from outside the component.

Related

In Blazor handle Blazor component InputNumber property readonly via code

<InputNumber readonly
id="ProductShares"
class="form-control"
placeholder="Product Shares"
oninput="#Calculation"
#bind-Value="product.ProductShares" />
I want to update InputNumber to readonly in code. That is on event oninput I am calling Calculation method and I want to programmatically update readonly property.
public async void Calculation(ChangeEventArgs e)
{
// Something like
if(condition)
{
ProductShares.readonly = true; // ProductShares is the id of the InputNumber
}
else
{
ProductShares.readonly = false;
}
}
You can set bool value to readonly attribute like this
This is your html
<InputNumber readonly="#ReadOnly"
id="ProductShares"
class="form-control"
placeholder="Product Shares"
oninput="#Calculation"
#bind-Value="product.ProductShares" />
This is your code behind
#code{
private bool ReadOnly=true;
}

How to define protected field with public accessor in Kotlin

I have the following situation:
data class Person(val name: string=""):Entity { }
open class Entity() { var id: Long=0 }
In this way, id is a public property, and the associated field is private (is not visible in Person class).
I'm working on an annotation processor and the annotation that I've defined works on fields. How can I define the property id as protected field with public accessor?
You can set as a public variable and work with the scope of its setter, in this case, set the setter as protected using:
var yourField: Any = /** initial value **/
protected set
Read more about Visibility modifiers here

Object obtained from WCF service has null private fields, even though they are initialized in the class [duplicate]

I need to initialize private readonly field after Deserialization. I have folowing DataContract:
[DataContract]
public class Item
{
public Item()
{
// Constructor not called at Deserialization
// because of FormatterServices.GetUninitializedObject is used
// so field will not be initialized by constructor at Deserialization
_privateReadonlyField = new object();
}
// Initialization will not be called at Deserialization (same reason as for constructor)
private readonly object _privateReadonlyField = new object();
[DataMember]
public string SomeSerializableProperty { get; set; }
[OnDeserializing]
public void OnDeserializing(StreamingContext context)
{
// With this line code even not compiles, since readonly fields can be initialized only in constructor
_privateReadonlyField = new object();
}
}
All what I need, that after Deserialization _privateReadonlyField is not null.
Any suggestions about this - is it possible at all?
Or I need to remove "readonly" key, which is not a good option.
Serialization is able to read in values for read-only fields because it uses reflection, which ignores accessibility rules. It can be argued that the following is, therefore, justified as part of the serialization process, even though I would recommend strongly against it in almost any other circumstance:
private readonly Doodad _oldField;
[OptionalField(VersionAdded = 2)]
private readonly Widget _newField;
[OnDeserialized]
private void OnDeserialized(StreamingContext context)
{
if (_oldField != null && _newField == null)
{
var field = GetType().GetField("_newField",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.DeclaredOnly |
System.Reflection.BindingFlags.NonPublic);
field.SetValue(this, new Widget(_oldField));
}
}
Any field declared as private readonly can be instantiated in the same line where it was declared or inside a constructor. Once that is done it cannot be changed.
From MSDN:
The readonly keyword is a modifier that you can use on fields. When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.
That means that you will have to remove readonly keyword to get it to work.

Constants in EL 3.0 - Property not found

I'm trying to reference constant with EL on my JSF page (https://java.net/projects/el-spec/pages/StaticField), but I'm stuck on this exception:
javax.servlet.ServletException: /faces/signup.xhtml #18,85 maxlength="#{signUpBean.USERNAME_MAXLENGTH}": Property 'USERNAME_MAXLENGTH' not found on type com.foo.SignUpBean
I'm using Tomcat 8.0.0-RC1 and here is my backing bean and input declaration:
Bean:
#ManagedBean
#RequestScoped
public class SignUpBean implements Serializable {
public static final int USERNAME_MAXLENGTH = 30;
...
}
Input field on my page:
<input type="text" jsf:id="username" jsf:value="#{signUpBean.username}" maxlength="#{signUpBean.USERNAME_MAXLENGTH}" />
Update:
With maxlength="#{(com.foo.SignUpBean).USERNAME_MAXLENGTH}" I'm getting java.lang.NullPointerException: Argument Error: Parameter value is null
First, off, see BalusC's updated answer for how to properly use constants in EL 3.0 expressions.
Now, with that said, if you just want to get your code working right now with the released version of GlassFish 4.0, you can modify your backing bean in the following way. Your backing bean doesn't have a getter/setter for your field. Backing beans need to have JavaBeans-style properties with getters/setters.
#ManagedBean
#RequestScoped
public class SignUpBean implements Serializable {
private final int USERNAME_MAXLENGTH = 30;
private String username;
...
public int getUSERNAME_MAXLENGTH() {
return USERNAME_MAXLENGTH;
}
public void setUSERNAME_MAXLENGTH(int i) {
// don't set anything, because this is a constant
}
public String getUsername() {
return username;
}
public void setUsername(String u) {
username = u;
}
}
Then your Facelets tag:
<input type="text"
jsf:id="username"
jsf:value="#{signUpBean.username}"
jsf:maxlength="#{signUpBean.USERNAME_MAXLENGTH}" />
That is, don't make the field static. I do recommend switching this over to the correct syntax once JSF is updated in the RI/GlassFish 4.0.
Edit: fixed input tag to use jsf:maxlength.

Protected property setter on .aspx not working

Could any body explain why if I have protected property in control it couldn't be set from .aspx?
Control:
public partial class SomeUserControl : UserControl
{
protected bool SomeProperty
{
get { return ViewState["SomeProperty"] != null && (bool) ViewState["SomeProperty"]; }
set { ViewState["SomeProperty"] = value; }
}
...
}
Declaration in .aspx:
<custom:SomeUserControl ID="SomeUserControl1" runat="server" SomeProperty="true"/>
When I am trying to debug setter never called.
Just realized that it wasn't visible with intellisense. And this property recognized like as HTML attribute.