Is this a bug or bad coding?

I have setup a class named “user” as follows with several property:

id as integer
name as string
email as string

In the “App” a property is set up “x” Type as user.

In the App’s open event have the following code:

x.name=“Jones”

Which crashes with a NilException.

If I change code as follows:

dim u as new user
x=u
x.name=“jones”

and I get no error. Why?

You still need to create a new instance of x. In the open event:

x = New User

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.

Edit: Exactly what Greg said a few seconds ago :wink:

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.

In particular I highly recommend Foundations of Programming: Object-Oriented Design by Simon Allardice. This course is joy for learning OOP concepts that applies to Xojo.

Also Xojo webinars are fantastic and Paul is great introducing weekly new topics. I know there are some OOP webinars planned.

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=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]

Much better explained! Thanks Norman.

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 :slight_smile: :

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

Thanks all.

And thanks for not flaming me for asking such a BASIC question.