In the app open event before you try to use x, you need to add
x = new User
The second code works because you are assigning an initiated instance to the variable so it is not nil. The first code does not work because x has not be initiated and is still nil.
Just to add a bit more info, in the second code x and u use the SAME user data. So:
Dim x as User // x is type User (nil for now)
Dim u as new User // u is type User, create an instance of this class and assign to it
x = u // make x point to the same data u points to
x.name = "Jones"
u.name = "James"
MsgBox x.name // James
[quote=114020:@Jim Smith]dim u as new user
x=u
x.name=“jones”
and I get no error. Why?[/quote]
Dim varname as ClassName // Defines a type of ClassName
Dim varname as new ClassName // Creates an “Instance” of ClassName (alloc) and Inits it (calls the constructor of the class if is available).
Dim varname as ClassName = ClassName.MyType() // Gets an Instance created from a shared method or other instance of same type (or a subclass)
I suggest to have a look at some basic OOP tutorials, lynda.com has really good tutorials for OOP that are perfect for Xojo.
[quote=114173:@Norman Palardy]Not really - it defines a variable that can hold a reference to an instance of ClassName
It doesn’t define the type - the class definition does that[/quote]
Ok, as we are starting to be a bit academic here, consider my previous 101 text quoting the proper text to be a bit emphatic on the “not exactly” thing :
Dim x as User // x is "type User" (nil for now)
Dim u as new User // u is "type User", create an instance of this class and assign to it
x = u // make x point to the same data u points to
x.name = "Jones"
u.name = "James"
MsgBox x.name // James
Dim x as User // x can hold a reference to an instance of "type User" (nil for now)
Dim u as new User // u can hold reference to an instance of "type User", create an instance of this class
// and make u refer to this new instance
x = u // make x refer to the same object u refers to
x.name = "Jones"
u.name = "James"
MsgBox x.name // James
the fact that some variables are references to objects (reference types) rather than holding the value (value types) is important ESP in the case of something like your code shows