How do i pass a variable to another class?
These two classes are not my main.
The variable I want to use in class1 is test which is int 5 from class2.
You Can make them public(or, eben better, use get set) and then you habe access to them.
public int Name;
Related
Lets say you have a class Bird that has a const constructor:
class Bird {
const Bird();
void fly() {}
}
You need to use it in a place but you have two options:
1.
const bird = Bird();
void doIt() {
bird.fly();
}
void doIt2() {
const bird = Bird();
bird.fly();
}
Questions :)
Whats the of const constructors? it will avoid the creation of the objects in both examples?
Is there any difference between 1. and 2. related to const?
Im thinking that there are no difference in terms of performance but Im not really sure how to measure it
How can I measure that?
There is no difference in the value or the efficiency of accessing it.
The only difference is that in version 1, other code can refer to the bird variable, and in version 2, it's a local variable inside the function, and cannot be seen from the outside.
Just like any other variable, its scope is defined by where it's declared, not what its value is.
It doesn't matter, for that purpose, whether it's a const variable or not.
Because it is a const variable, it also means that the value is computed at compile-time, so accessing it takes no time at runtime.
There is nothing to measure, because accessing a constant variable at runtime, no matter where it is, just means loading a reference to the already existing value.
In the first example you have an instance variable. Instance variables are variables that are defined in the class, for which each instantiated object of that class has a separate copy or instance of the variables.
In the second example you have a local variable. After the calculate function executes, the local variables will no longer exist except of cases of closures.
By the way.
Use final for local variables that are not reassigned and var for those that are.
Use var for all local variables, even ones that aren’t reassigned. Never use final for locals. (Using final for fields and top-level variables is still encouraged, of course.)
https://dart.dev/guides/language/effective-dart/usage
I want to be able to retrieve the names for the types in an enumeration without having to actually assign a variable to them. Hence, given an enumeration like this
class my_class;
typedef enum bit {
ONE,
TWO
} fsm_state_t;
endclass
I know I can access the name of a declared variable like this:
class another_class;
...
my_class::fsm_state_t state = my_class::ONE;
print(state.name());
...
endclass
Is it possible to access the names of the enum without actually having to declare and assign a variable? What I mean is something like this:
class another_class;
...
print(my_class::ONE);
print(my_class::TWO);
...
endclass
No, built-in methods cannot be called on types.
if someday the type is changed, the compiler notifies that the print
must be changed as well.
By simply "using" the enumeration within your code, if it goes away you'll get a compile error. That seems to be what you're duplicating. A more practical duplication would be to value check every enum:
class another_class;
...
if (my_class::ONE!=0) print("ONE has changed!");
if (my_class::TWO!=1) print("TWO has changed!");
...
endclass
EDIT: or create a wrapper class for enums
virtual class enum_wrap#(type T);
static function string name(T obj);
return obj.name();
endfunction
endclass
program testbench;
initial begin
typedef enum {ZERO, ONE, TWO, THREE} numbers_t;
$display("ENUM without variable: %s", enum_wrap#(numbers_t)::name(THREE));
end
endprogram
prints:
ENUM without variable: THREE
I'm still figuring out how to use p5.js. In regular java you have to declare each variable using its data type, ex. int foo = 0.
In p5, I know you can just use var foo but you can also declare variables using this.foo. If someone could clarify when is the proper time to use var and when i can use this, that would be very helpful.
For example, if I want to declare a variable inside a method, should i use var foo = thing or could I declare it using this.foo = thing? What should I use when declaring global variables or when referring to objects passed into methods?
Thanks!
First of all, p5 is not a language, it is a Javascript library, you are coding in Javascript, not p5.
Coming to your question, if you want to use some function as a data type, similar to a class in java, and want all the "instances" of that to have their own different variables, you use this. If they are just variables you use in someway but don't need to be specific for each instance, or if the function is not a constructor function and is not to be used as a data type, you will just use var then.
Again, there is no class stuff in javascript, you will have to write what is called a constructor function in order to "simulate" a java class, but be aware that a constructor function should not return anything. Here is an example of car class in java:
class car {
int speed = ___;
String model = ___;
static int numOfWheels = ___;
}
This is what it will look like in javascript (a constructor function):
function car() {
this.speed = ____;
this.model = ____;
var numOfWheels = ___;
}
If you declare a variable without this, it can be roughly compared to a static variable in a java class in the sense that it will be constant among all the instances.
So basically, at least in most cases, you will use this.varName usually inside constructor functions, i.e., functions that you will use to construct objects.
What should I use when declaring global variables or when referring to objects passed into methods?
Global variables will almost always be var something = something. When referring to objects passed into functions, just use the dot notation to refer to its properties like passedObject.someProperty
I would recommend you to learn Javascript before jumping into p5 directly, here are some resources that I found useful when I started learning Javascript-
w3 School
JavaScript Info Website
TheNewBoston
Given:
A class named B.
2 persistent records of class B, id = {"B1", "B2"}.
A class named A:
class A Extends %Persistent
{
Property rB As B;
ForeignKey FKB(rB) References B();
}
An instance of class A, named objA, with objA.rB = "B3" (which is an invalid value because B has no "B3" persistent record)
I want that:
When objA is going to be saved (objA.%Save()), substitute with an empty string the rB invalid value and save it.
So far:
I have tried to make the substitution using callback methods ( %OnBeforeSave), but it seems that %ValidateObject is being called before.
Is there a way of doing value changes before %ValidateObject using callback methods?
Thanks in advance.
You can modify objects in callback method %OnAddToSaveSet:
http://docs.intersystems.com/cache201511/csp/docbook/DocBook.UI.Page.cls?KEY=GOBJ_callbacks#GOBJ_cb_onaddtosaveset
I want to have a static variable in Cocoa.
After looking at How do I declare class-level properties in Objective-C?, I am unclear whether there is anything wrong with what I have always done so far, i.e.
// Foo.m
static NSString* id;
#interface Foo ()
instead of
// Foo.h
#interface Foo {
}
+(NSString*) id;
// Foo.m
+(NSString*) id
{
static NSString* fooId = nil;
if (fooId == nil)
{
// create id
}
return fooId;
}
Obviously, the second approach offers an opportunity for initializing the id. But if I initialize the id myself somewhere else in the code, within, say a getter:
-(NSString*) getId
{
if (id==nil) {
id = ... // init goes here
}
return id;
}
Then is there anything wrong with the simple static declaration approach as opposed to the more complex class function approach? What am I missing?
First, what you are asking for is a global variable, a static is similar but a little bit different...
Putting a static declaration outside of any #interface in a header (.h) file will create a different variable in each implementation (.m) file you include the header in - not what you want in this case.
So static on a declaration creates a variable whose lifetime is that of the whole application execution but which is only visible within the compilation unit (e.g. the implementation file) in which it appears - either directly or via inclusion.
To create a global variable visible everywhere you need to use extern in the header:
extern NSString *id;
and in your implementation repeat the declaration without the extern:
NSString *id;
As to what is wrong with global variable vs. class methods, that is a question on program design and maintainability. Here are just a few points to consider:
With a method the value cannot be changed unless you provide a setter method as well as getter method. Variables are always read-write.
Namespace pollution: a class method is only valid when paired with its class name ([YourClass id]); the variable name is valid everywhere it's included simply as id; that both pollutes the name space and loses the connection between id and YourClass - which leads us to...
Encapsulation: globals variables break strong encapsulation, and encapsulation aids program design and maintenance - this is a big topic.
That said, there can be a time and a place for globals, sometimes...
After question updated
A static variable declared in the implementation is effectively a "class variable" - a variable shared by all instances of the class.
The pros'n'cons of a class variable vs. setter & getter class methods are exactly the same as the pros'n'cons of an instance variable vs. properties & setter/getter instance methods.
Class setters/getters allow for validation and other logic to be executed on each read/write; and localization of memory management - in short the abstraction and encapsulation benefits of any method.
Therefore whether you use a variable or a setter/getter depends on your application. It is the same question as whether you use an instance variable or setter/getter/property.