instantiating an inner class from the main method - instantiation

How can I instantiate an inner class? for example:
QuoteArray quotes1 = new QuoteArray(1);
QuoteArray.Quote quote1 = new QuoteArray.Quote("quote","char","actor","film","year");//error here
Where Quote is an inner class of QuoteArray. None of this really makes sense to me. I know how to instantiate Quote from within QuoteArray, but I don't know how to do this in my main method.

QuoteArray.Quote quote1 = new QuoteArray().new Quote("quote","char","actor","film","year");
Well this makes no sense to me, but it works.

Related

making a variable equal a Arraylist value within a class (Processing)

Hi this is my first time making a question so hopefully i have done this right :)
In my code Im trying to make a Arraylist that is within a class that only holds floats so I made this: (some information is taken away to make it easier to read)
class object {
private ArrayList yc=new ArrayList<Float>();
private ArrayList xc=new ArrayList<Float>();
object(float xer,float yer,float rer){
xc.add(10.0);
x=xer;
y=yer;
r=rer;
}
void update(){
print(xc.get(0))
x=xc.get(0);
}
}
everything else except x=xc.get(0) works outside of the class this assignment works but inside the class it doesn't
hope this makes sense thanks.
It seems like when you initialised the variable xc you specified the type of the ArrayList to Float. However, when declaring xc, you did not specify the type.
When you try to assign variable x with xc.get(0), you are basically assigning any Object to variable x that was declared as a Float.
To solve this issue you can declare your variable xc specifying the type of the ArrayList: private ArrayList<Float> xc = new ArrayList<Float>();
Also, processing offers the helper class FloatList as well if this suits your program.
This should solve your issue, depending on the rest of your code.

Non static method cannot referenced - Inner class

switch (position)
{
case 0:
feld.setzeDeutsch();
break;
This is a part of an Inner Class, but it says to make the setzeDeutsch method static.. I cant make this method static because there is a counter variable..
What can I do? Can I make the variable static?
You need to pass an instance of the outer class to the inner class so that the inner class can access it. Do not make the variable static.

How to abstract class names or types in Objective C to use as variables?

I've realized that several of my list view controllers behave in the same way, and now I want to abstract them into a generic one called ListVC. The plan is to create an instance of ListVC when its needed, and pass into it all of the specific things needed for that instance of it.
For example, if I need a list of customers I would do something like:
ListVC *customersVC = [[ListVC alloc] init];
customersVC.tableArray = self.customersList
In ListVC, there are times when I need to know certain object's class type. In my example, the objects in the array self.customersList are from the class CustomerClass. I know I can convert strings to class names but that doesn't help when I have a method inside ListVC that needs to return the object's type. For example, in ListVC, this old non-abstracted method:
- (CustomerClass *)customerAfterFilter
should be something like:
- (self.objectClass *)objectAfterFilter
And I'd set objectClass after it was instantiated like so.
ListVC *customersVC = [[ListVC alloc] init];
customersVC.tableArray = self.customersList
customersVC.objectClass = [CustomerClass class];
How do I do something like this? Or if my approach is just wrong, please suggest the correct approach. I'm a beginner in programming.
You can't change the return type like that. What you can do is return an id, which is a pointer to an object of any class.
- (id)objectAfterFilter
EDIT:
Customer *c = [customersVC objectsAfterFilter];
[c duCustomerStuff];
Alternatively:
[((Customer *)[customersVC objectsAfterFilter]) doCustomerStuff];

Another oddness with ObjC inheritance

I have another odd problem with Objective C inheritance. I have a protocol called IGameObject, a class GameObject that inherits from NSObject and IGameObject and finally a Player that inherits from GameObject..
The problem is that when I assign a Player* to a IGameObject*, this produces an error, but it works ok when I assign a Player* to a GameObject*. I haven't seen that this is not possible in all I have read. Here the code:
-(IGameObject*) clone
{
Player* p=(Player*) 0xFFfFFFFF;
//Throws an error saying that Cannont initialise a variable of type IGameObject with an value of Player*
IGameObject* go=p;
//This works perfectly
GameObject* go2=p;
return [[Player alloc] initWithGameObject:self];
}
Could anybody guess what is happening?
Thanks in advance.
When returning (or declaring) a type that is only known by its interface, don't treat it as an object pointer. Instead, use:
-(id<IGameObject>) clone {
And:
id<IGameObject> go=p;
This should clear up that warning.
Sidenote: Why in the world are you assigning p to a memory address?!
You cannot create a pointer to a protocol
IGameObject* go=p;
is meaningless.

Need help understanding Generics, How To Abstract Types Question

I could use some really good links that explain Generics and how to use them. But I also have a very specific question, relater to working on a current project.
Given this class constructor:
public class SecuredDomainViewModel<TDomainContext, TEntity> : DomainViewModel<TDomainContext, TEntity>
where TDomainContext : DomainContext, new()
where TEntity : Entity, new()
public SecuredDomainViewModel(TDomainContext domainContext, ProtectedItem protectedItem)
: base(domainContext)
{
this.protectedItem = protectedItem;
}
And its creation this way:
DomainViewModel d;
d = new SecuredDomainViewModel<MyContext, MyEntityType>(this.context, selectedProtectedItem);
Assuming I have 20 different EntityTypes within MyContext, is there any easier way to call the constructor without a large switch statement?
Also, since d is DomainViewModel and I later need to access methods from SecuredDomainViewModel, it seems I need to do this:
if (((SecuredDomainViewModel<MyContext, MyEntityType>)d).CanEditEntity)
But again "MyEntityType" could actually be one of 20 diffent types. Is there anyway to write these types of statements where MyEntityType is returned from some sort of Reflection?
Additional Info for Clarification:
I will investigate ConstructorInfo, but I think I may have incorrectly described what I'm looking to do.
Assume I have the DomainViewModel, d in my original posting.
This may have been constructed via three possible ways:
d = new SecuredDomainViewModel<MyContext, Order>(this.context, selectedProtectedItem);
d = new SecuredDomainViewModel<MyContext, Invoice>(this.context, selectedProtectedItem);
d = new SecuredDomainViewModel<MyContext, Consumer>(this.context, selectedProtectedItem);
Later, I need to access methods on the SecuredDomainViewModel, which currently must be called this way:
ex: if (((SecuredDomainViewModel<MyContext, Order)d).CanEditEntity)
ex: if (((SecuredDomainViewModel<MyContext, Invoice)d).CanEditEntity)
ex: if (((SecuredDomainViewModel<MyContext, Consumer)d).CanEditEntity)
Assuming I have N+ entity types in this context, what I was hoping to be able to do is
something like this with one call:
ex: if (((SecuredDomainViewModel<MyContext, CurrentEntityType)d).CanEditEntity)
Where CurrentEntityType was some sort of function or other type of call that returned Order, Invoice or Consumer based on the current item entity type.
Is that possible?
You can create a non-generic interface that has the CanEditEntity property on it, make SecuredDomainViewModel inherit off that, then call the property through the interface...
Also, the new() constructor allows you to call a constructor on a generic type that has no arguments (so you can just write new TEntity()), but if you want to call a constructor that has parameters one handy trick I use is to pass it in as a delegate:
public void Method<T>(Func<string, bool, T> ctor) {
// ...
T newobj = ctor("foo", true);
// ...
}
//called later...
Method((s, b) => new MyClass(s, b));
I can't help on the links, and likely not on the type either.
Constructor
If you have the Type, you can get the constructor:
ConstructorInfo construtor = typeof(MyEntityType).GetConstructor(new object[]{TDomainContext, ProtectedItem});
Type
I'm not really sure what you're looking for, but I can only see something like
if (((SecuredDomainViewModel<MyContext, entityType>)d).CanEditEntity)
{
entityType=typeof(Orders)
}
being what you want.