Static/Global variables problems - class

//main.fla
var skill1 = addChild(girl2.skill1);
i want to access the skill1 var in girl2.as, but it didn't work.
The error was shown :
1119: Access of possibly undefined property skill1 through a reference with static type Icons.chars:girl2.
//girl2.as
package Icons.chars
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.net.URLRequest;
public class girl2 extends charsel {
public static var skill1:Loader, url1:URLRequest;
public function girl2 () {
skill1 = new Loader();
url1 = new URLRequest("skills/nami/skill4.png")
skill1.load(url1);
I tried in charsel.as and it worked
//charsel.as
package Icons.chars
{
import flash.geom.Point;
import flash.display.*;
import flash.events.MouseEvent;
public class charsel extends MovieClip {
protected var originalPosition:Point;
public static var player1:MovieClip, player2:MovieClip, player3:MovieClip;
I tried to access player1 and it worked.

I am not sure why you would want to use a static modifier in this case. Note, that even though you have declared skill1 as a static it will actually become available only when you create an instance of the girl2 class - as you creating it in the constructor (also note, that you should name your classes with a capital letter, so this should be Girl2).
So, I would either remove all static modifiers for public variables or, if there are any specific reasons for that, use a static initialiser to instantiate skill1 variable:
public class girl2 extends charsel {
public static var skill1:Loader, url1:URLRequest;
{
skill1 = new Loader();
skill1.addEventListener(...); // event handlers should be static as well
skill1.load(...);
}
public function girl2 () {

Related

Accessing private method from a static one

I'm trying to access a private method from a static one in the same class in Dart.
class MyClass{
void _initFunc() {
/// ...
}
static void info(){
if (condition){
_initFunc();
}
}
}
I got this error
Instance members can't be accessed from a static method.
Can you please explain me why, and how can i do. info() must be static, and _initFunc() must be private.
There is no "this" in a static method. So MyClass.info() doesn't have a "this" to call this._initFunc();
In Dart the static modifier on class members and methods makes them available without creating an instance of a class object. For example special constructors are static because you want to create a instance with them. Consider MyClass foo = MyClass.fromAnotherObject(bar); -> static MyClass fromAnotherObject(){} is static because you don't yet have a MyClass object to call it on.
In your example code, you could change _initFunc() to a public function (remove the "_") and either:
a) instantiate a MyClass object inside your static info() method and call initFunc()
class MyClass{
void initFunc() {
/// ...
}
static void info(){
if (condition){
MyClass myClass = MyClass();
myClass.initFunc();
}
}
}
OR
b) declare initFunc() also static and call it from info()
class MyClass{
static void initFunc() {
/// ...
}
static void info(){
if (condition){
MyClass.initFunc();
}
}
}

How can a static import be null

Since it's initialized in declaration, and the import implies by itself a dependency.
We have a jUnit parent test class like...
public class ServerTestBase extends TestBase {
public static final Client client = new Client();
...
And a Suite with the static import of the client and some init code in a #ClassRule using this client:
import static jwstest.test.ServerTestBase.client;
#RunWith(Suite.class)
#Suite.SuiteClasses({BunchOfSuites.class})
public class ScratchSuite {
#ClassRule
public static final ExternalResource testRule = new ExternalResource() {
#Override
protected void before() throws Throwable {
response = client.call(someService, HttpVerbs.GET).getResponse();
So, again, I don't understand, how in hell client can be null, since it's initialized on declaration, and the static import implies a dependency.
¿Any ideas? Thank you in advance.
The docs say that the field accessible in the #ClassRule has to be public static.
Static import is not such thing.
You can either extend ServerTestBase:
public class ScratchSuite extends ServerTestBase { ... }
... OR declare public static anew:
public static Client myClient = client; //from static import

Swift - Type has no member

I have a basic class named APIGroup that exists for the sole purpose of subclassing (it is not [yet] used for anything but certain static properties that are accessed by the subclasses). It looks somewhat like this:
public class APIGroup {
public static let someProperty : String = "I am a property!"
}
And I have a subclass, Track, which defines a type method search as follows:
public class Track : APIGroup {
public static func search(name: String) -> Void {
print("Track search initiated. Name: \(name)")
print("And the property is: \(someProperty)")
}
}
These two classes are defined in separate files in the same module. In another target, I import the module and try to call:
import MyModule
MyModule.Track.search("testing...")
But this throws: Type 'Track' has no member 'search'. What am I doing wrong?
Putting this code in a Playground works fine for me:
import Foundation
public class APIGroup {
public static let someProperty : String = "I am a property!"
}
public class Track : APIGroup {
public static func search(name: String) -> Void {
print("Track search initiated. Name: \(name)")
print("And the property is: \(someProperty)")
}
}
Track.search("testing...")
If they are in the same module you do not need to import or use the module name.

swift - how to access public constants without having to instanciate class, as in C#

Should not be needed create an instance of a class to access a public constant. I recently started working in Swift, so I must be missing something here.
In this simple example:
public class MyConstants{
public let constX=1;
}
public class Consumer{
func foo(){
var x = MyConstants.constX;// Compiler error: MyConstants don't have constX
}
}
This foo code gives an compile error. To work, I need to create an instance of the MyConstants like this:
public class Consumer{
func foo(){
var dummy = MyConstants();
var x = dummy.constX;
}
}
Adding static to constX is not allowed.
Use struct with static types.structare more appropriate as in enum you can only bind one type of associative values but you can contain the "Type Property of any type" in both.
public struct MyConstants{
static let constX=1;
}
public class Consumer{
func foo(){
var x = MyConstants.constX;
}
}
You should use immutable static variables. Unfortunately classes support computed properties only with the class modifier - the compiler outputs an error stating that class variables are not yet supported.
But in structs it's possible to create static data members:
struct Constants {
static let myConstant = 5
}
and of course it's not required to create an instance, as the immutable static property can simply be accessed as:
Constants.myConstant
If you want a constant, you can also "fake" the as-yet-unsupported class variable with a class computed property, which does currently work:
public class MyConstants{
public class var constX: Int { return 1 };
}
public class Consumer{
func foo(){
var x = MyConstants.constX; // Now works fine.
}
}
For string constants what I do is put the bunch of constants into responsible class file in the following way:
public enum NotificationMessage: String {
case kTimerNotificationId = "NotificationIdentifier"
}
Then for use it from any other point of the code just:
println("\(NotificationMessage.kTimerNotificationId.rawValue)")
Do not forget .rawValue.
I found out the solution below, but hope someone can clarify or post a better solution
enum MyConstantsV2 {
static var constX = 1
}
public class Consumerv2{
func foo(){
var x = MyConstantsV2.constX;
}
}

GWT RPC serializing

I am trying to send over MyClass through RPC, but am getting :
Type MyClass was not assignable to 'com.google.gwt.user.client.rpc.IsSerializable' and did not have a custom field serializer.For security purposes, this type will not be serialized.
I have looked at GWT - occasional com.google.gwt.user.client.rpc.SerializationException and tried their solution, but it did not work.
The difference is that MyClass is located in another project.
The project structure is:
MyApiProject
-contains MyClass
MyClientProject
MyServerProject
I have also tried passing an enum through the RPC from MyApiProject, which also failed.
public class MyClass
implements Serializable
{
private static final long serialVersionUID = 5258129039653904120L;
private String str;
private MyClass()
{
}
public MyClass(String str)
{
this.str = str;
}
public String getString()
{
return this.str;
}
}
in the RemoteService I have:
mypackage.MyClass getMyClass();
in the RemoteServiceAsync I have:
void getMyClass(AsyncCallback<mypackage.MyClass> callback);
I had to change implements Serializable to implements IsSerializable
This usually pops up when you are using another type inside of your class that is not serializable. Check the properties of your class and make sure they are all serializable, post the code of MyClass here and I can look at it as well.
I believe GWT requires an RPC serializable class to also have a public no-argument constructor.
Try removing
private MyClass()
{
}
or set it to
public MyClass()
{
}