passing more than 1 parameter in metro style app c# - microsoft-metro

I have a class student with 2 variables one string name and other integer roll.
using
this.frame.Navigate(typeof(Page1),s1); //s1 is an object of class student
I am trying to pass an object to page1
But I am not able to access any of the variables of s1 on page1.
in the NavigatedTo method of Page1 I have written:
student x= e.Parameter as student;
student n= new student();
n.name=x.name;
in whichever I try to access object x variables it throws a "NullReferenceException unhandled by the user code"
"object reference not set to an instance of an object"
not able to find a solution for this.

Have you tried using student x = e.ExtraData as student? Here is some relevant Msdn doco.

Related

Gorm one to one relation ship 2 foreignkeys refering one go model?

Receiver uint gorm:"not null",json:"receiver"
Sender uint gorm:"not null",json:"sender"
User User `gorm:"foreignKey:Receiver,Sender"
my model loos like this and sender and reciver refers to user model when i migrate it throughs an error how do i solve that
As far as I understand your question, you have an object that has a User reference. Then there is a Receiver and a Sender which refer to this User. Since both seem to point to the same user one is not necessary. I guess you are trying to create an object like this:
type Obj struct {
Receiver uint
ReceiverUser User `gorm:"foreignKey:Receiver"`
Sender uint
SenderUser User `gorm:"foreignKey:Sender"`
}
create different relation ships the relations should be unique the above answer should work and creates fk_reciver ans fk_sender belongs_to relation ships using id as a reference use it like this
type Model struct {
Receiver uint
ReceiverUser User gorm:"foreignKey:Receiver"
Sender uint
SenderUser User gorm:"foreignKey:Sender"
}

Apex DateTime to String then pass the argument

I have two Visualforce pages, and for several reasons I've decided it is best to pass a datetime value between them as a string. However, after my implementation my date always appear to be null even though my code seems to compile.
I have researched quite a few formatting topics but unfortunately each variation of the format() on date time seems to not produce different results.
The controller on page 1 declares two public variables
public datetime qcdate;
public String fdt;
qcdate is generated from a SOQL query.
wo = [SELECT id, WorkOrderNumber, Quality_Control_Timestamp__c FROM WorkOrder WHERE id=:woid];
qcdate = wo.Quality_Control_Timestamp__c;
fdt is then generated from a method
fdt = getMyFormattedDate(qcdate);
which looks like this
public String getMyFormattedDate(datetime dt){
return dt.format(); }
fdt is then passed in the URL to my next VF page.
String url = '/apex/workordermaintenanceinvoice?tenlan='+tenlan +'&woid=' + woid + '&invnum=' + invnum + '&addr1=' + addr1 + 'fdt=' + fdt;
PageReference pr = new PageReference(url);
I expected when calling {!fdt} on my next page to get a proper string. But I do not.
UPDATE:
Sorry I guess I should not have assumed that it was taken for granted that the passed variable was called correctly. Once the variable is passed the following happens:
The new page controller creates the variable:
public String fdt
The variable is captured with a getparameters().
fdt = apexpages.currentPage().getparameters().get('fdt');
The getfdt() method is created
public String getfdt(){
return fdt;
}
Which is then called on the VF page
{!fdt}
This all of course still yields a 'blank' date which is the mystery I'm still trying to solve.
You passed it via URL, cool. But... so what? Page params don't get magically parsed into class variables on the target page. Like if you have a record-specific page and you know in the url there's /apex/SomePage?id=001.... - that doesn't automatically mean your VF page will have anything in {!id} or that your apex controller (if there's any) will have id class variable. The only "magic" thing you get in apex is the standard controller and hopefully it'll have something in sc.getId() but that's it.
In fact it'd be very stupid and dangerous. Imagine code like Integer integer = 5, that'd be fun to debug, in next line you type "integer" and what would that be, the type or variable? Having a variable named "id" would be bad idea too.
If you want to access the fdt URL param in target page in pure Visualforce, something like {!$CurrentPage.parameters.fdt} should work OK. If you need it in Apex - you'll have to parse it out of the URL. Similar thing, ApexPages.currentPage() and then call getParameters() on it.
(Similarly it's cleaner to set parameters that way too, not hand-crafting the URL and "&" signs manually. If you do it manual you theoretically should escape special characters... Let apex do it for you.

Grails Grom + mongoDb get during save OptimisticLockingException

I try in Grails service save an object to mongodb:
Cover saveCover = new Cover()
saveCover.id = url
saveCover.url = url
saveCover.name = name
saveCover.sku = sku
saveCover.price = price
saveCover.save()
Cover domain looks like this:
class Cover {
String id
String name
String url
String sku
String price
}
So I want to have custom id based on url, but during save process I get error:
Could not commit Datastore transaction; nested exception is
org.grails.datastore.mapping.core.OptimisticLockingException: The
instance was updated by another user while you were editing
But if I didn`t use setters and just pass all values in constructor, the exception is gone. Why?
As reported in the documentation here:
Note that if you manually assign an identifier, then you will need to use the insert method instead of the save method, otherwise GORM can't work out whether you are trying to achieve an insert or an update
so you need to use insert method instead of save when id generator is assigned
cover.insert(failOnError: true)
if you do not define the mapping like this:
static mapping = {
id generator: 'assigned'
}
and will use insert method you'll get an auto-generated objectId:
"_id" : "5496e904e4b03b155725ebdb"
This exception occurs when you assign an id to a new model and try to save it because GORM thinks it should be doing an update.
Why this exception occurs
When I ran into this issue I was using 1.3.0 of the grails-mongo plugin. That uses 1.1.9 of the grails datastore core code. I noticed that the exception gets generated on line 847(ish) of NativeEntryEntityPersister. This code updates an existing domain object in the db.
Above that on line 790 is where isUpdate is created which is used to see if it's an update or not. isInsert is false as it is only true when an insert is forced and readObjectIdentifier will return the id that has been assigned to the object so isUpdate will end up evaluating as true.
Fixing the exception
Thanks to && !isInsert on line 791 if you force an insert the insert code will get called and sure enough the exception will go away. However when I did this the assigned id wasn't saved and instead a generated object id was used. I saw that the fix for this was on line 803 where it checks to see if the generator is set to "assigned".
To fix that you can add the following mapping.
class Cover {
String id
String name
String url
String sku
String price
static mapping = {
id generator: 'assigned'
}
}
A side effect of this is that you will always need to assign an id for new Cover domain objects.

How to get available classes at runtime

I would like to poll a class and have it return all available subclasses in a way I can then address them. It can return an array, a dictionary, or something else. As long as I can then loop through the set of them and read properties or call functions from each.
Scenario:
I want to create a form where the user inputs the details of a series of events. Then I read the form and output a report. Each kind of event has a ".Name", but a different set of inputs (FormOptions) and methods (FormatOutput) to create an output. Right now this is implemented with a complex form and a script that runs after the user submits the form.
The trouble is that every time I add an option to the form, I have to change code in several different places. In the interest of making my code easier to maintain, I would like to contain all the code for each event type in a Class, then build the form based on the available Classes.
So as an example, I'm building an itinerary from a collection of Meeting and Travel objects:
Class Itinerary
Class Event
Public Property Get Name()
Name = "Meeting"
End Property
Public Function FormOptions(id)
Form Options = "<div id="& id &">form code for Meeting options</div>"
End Function
Public Sub FormatOutput(Time, Title, Location)
'Make a fancy meeting entry
End Sub
End Class
Class Travel
Public Property Get Name()
Name = "Travel"
End Property
Public Function FormOptions(id)
Form Options = "<div id="& id &">form code for Travel options</div>"
End Function
Public Sub FormatOutput(StartTime, EndTime, Origin, Destination)
'Make a fancy travel entry
End Sub
End Class
End Class
When the script runs it creates a form where the user can add a series of events. Each time the user chooses between "Meeting" and "Travel" then fills out the options for that event type. At the end, they push a button and the script makes a pretty document listing all the user's inputs.
At some point in the future, I will want to add a new kind of event: lodging.
Class Lodging
Public Property Get Name()
Name = "Lodging"
End Property
Public Function FormOptions(id)
Form Options = "<div id="& id &">form code for Lodging options</div>"
End Function
Public Sub FormatOutput(Date, Location)
'Make a fancy Lodging entry
End Sub
End Class
How do I setup my Itinerary class so that it automatically recognizes the new class and can return it as an available event type? I can think of several ways of doing this, but they all involve keeping an index of the available classes separate from the actual classes, and I'm trying to minimize the number of places I have to change code when I add new event types.
I am very purposely not including any details on how I build the form since at this point I'm open to anything. Also, please be gentle with references to "inheritance", "extensibility", or "polymorphism". I'm a just a scripter and these OOP concepts are still a bit foreign to me.
I don't think this is possible, but one way to come close to this would be to have a unique list of class names -- the simplest would be to have a global dictionary.
You can get a list of classes by reading the Keys collection of the dictionary.
I don't think VBScript supports references to classes, so when the user chooses one of the types use Eval to create an instance of the appropriate class.
Dim ClassList
Set ClassList = CreateObject("Scripting.Dictionary")
'on type selection:
Function GetItineraryInstance(className)
Set GetItineraryInstance = Eval("New " & className)
End Function
ClassList("Travel") = 1
Class Travel
'methods
End Class
At least the class registration can be kept together with the class definition.

Inheritance with coffeescript

Why this...
class Person
name: "initial name"
constructor: (#name) ->
class User extends Person
password: "initial password"
f = new User "Felds"
console.log f
console.log "my name is '#{f.name}' and my password is '#{f.password}'"
b = new User "Bob"
b.password = "bob's password"
console.log b
... when run through coffee -p test.coffee | node outputs this?
{ name: 'Felds' }
my name is 'Felds' and my password is 'initial password'
{ name: 'Bob', password: 'bob\'s password' }
Why doesn't the password property show on console.log f ? Where is it stored and how is it retrieved?
The 'initial password' value is stored in Person.prototype.password. Person.prototype is an object with all the common attributes shared between Person instances. When you access aPersonInstance.password, the password property is first looked up in the aPersonInstance object; if it's not found there it will be looked up in its prototype, and then in its prototype's prototype and so on.
console.log f will not show f's prototype properties, only the properties stored in f themselves (also known as f's own properties). When you assign Bob's password with b.password = 'bob\'s password' you're creating a new password property in b itself, which will now be the value of accessing b.password, even though b's prototype still has the 'initial password' value. I hope that made some sense =P
By storing 'initial password' in the prototype you're sharing that value between all Person instances as a kind of default value. This is perfectly fine to do with strings or other primitive (immutable) types; but you have to take special care if you're going to do it with arrays or other mutable objects, because the same object will be shared between all the class' instances, and if one of them modifies it, e.g. #someArray.push 5, all the other ones are going to see it. In those cases, it's usually preferable to initialize the attribute in the class constructor, like #someArray = [], to guarantee that each instance will have a different array/object attribute.
I want to give u a simple&quick answer but not exactly right in Javascript:
The variable password which you defined in class User is User.prototype.password, the variable just like class variable in OO language.
It is not f's instance variable, so when you console.log f, u cant see password.
but if you retrieve f.password, then u will still get the value 'initial password'. when a instance cant find a variable in itself, it will continue finding it in its class, that is User.
b.password is a instance variable in b, so console can log it out.