I feel like I am going about this wrong, but here is sort of a rundown of what I am trying to do (and failing)
Make a structure struc_dog Has various declarations such as age as integer , [b]breed as string100[/b] , color as string*25 , etc
*Open Event Handler for main window kicks off Method MakeDog
*Method MakeDog creates a dog with the structure :
Dim gdane as struc_dog
gdane.age = 2
gdane.breed = "Great Dane"
gdane.color = "Gray"
Ok, so far so good.
Now, on my Window let’s say there’s some sort of text entry to change the age, or to display even a messagebox with this info
MsgBox(gdane.color)
error :
[quote]This item does not exist
MsgBox(gdane.color)[/quote]
Well of course: the form itself can’t understand the code that hasn’t been run yet to actually declare the Great Dane. However, I am trying to make a program that can create lots of objects with properties (“Dogs” in this example, with various attributes such as age, breed, etc) , and then manipulate them from within the program (Example: press a button on the form to add a year to a dog’s age)
So yes, I am showing my n00bness here… what is a better way to go about this? I am sure making 1,000 global variables is probably not the right way to do this either
Class Dog
name as string
breed as string
color as string
end class
for 99.9% of your usage they are absolutely interchangeable - and the tiny bit that isn’t is, I’m guessing, irrelevant
if you want to store a lot of objects in your app that are all dogs you COULD
create a global array of dogs - literally dim allDogs() as Dog
create a dictionary of dogs dim allDogs as new Dictionary and then you add and remove dogs using keys (maybe names but I’m sure many dogs have the same name)
OK … maybe a database
Trust me I have a LOT of ideas about how to organize things as far as “dogs” go - I own a dog daycare & grooming business so I’ve been down this road with dogs, owners etc etc etc
Thanks for the reply. I have the same issue when using a class, but I think it’s because I am doing something wrong at an even more basic level.
Basically I am creating a variable in one method, and then trying to reference it with the user interface… it fails because, well, that variable doesn’t exist at compile.
To illustrate my conundrum, paste this raw text into a new .xojo_xml_project file and open it in Xojo:
Once you open this little test application, go to the “Open” Event Handler for the window… I Dim strTestString and set a value…
…then go to the button’s push action. See how it is trying to reference the string (that doesn’t exist)? I think this is my issue.
I can create structures - or like you said, classes - but I can’t reference them from other parts of my program because they don’t actually exist until compile.
So basically I am probably doing something incredibly basic the entirely wrong way!
You should post projects using the name Xojo used, in particular the .xojo_xml_project extension, and not a txt file.
If you want strTestString to be visible throughout the window, make it a string property of that window. If you dim it in Open, it exists only in that event.
May I suggest you read UserGuide-Fundamentals.pdf that is in the Documentation folder, next to the Xojo executable, in particular about scope. Grasping that notion will greatly help your Xojo use.
Ah sorry. For the future, is there a preferred method of posting sample projects to the forum? I didn’t see an attachment option so I resorted to pasting the raw XML to pastebin.
[quote]If you want strTestString to be visible throughout the window, make it a string property of that window. If you dim it in Open, it exists only in that event.
May I suggest you read UserGuide-Fundamentals.pdf that is in the Documentation folder, next to the Xojo executable, in particular about scope. Grasping that notion will greatly help your Xojo use.[/quote]
I see that I can make even structures as properties. This solves the issue; thanks!
Furthermore, I see that using structures is somewhat frowned upon in favor of using full-blown classes.
Is there a reason for this? Are classes a cleaner, faster way of going about this? I am a novice programmer, and I remembered having success using structures in VB.NET over a decade ago, so I sort of just kept using them!
I can definitely switch over to using classes instead if you think it’s best / better form.
[quote=242098:@Norman Palardy]1) use a class - not a structure
2) use a class (see #1)
So lets march on
Class Dog
name as string
breed as string
color as string
end class
for 99.9% of your usage they are absolutely interchangeable - and the tiny bit that isn’t is, I’m guessing, irrelevant
[/quote]
Hi Norman,
I also use too many times structures instead of classes
the reason is simple : I can store structures in a module, but not a class
and when you program big apps, you have to segment into modules, reusable modules
and if the class is outside the module, then you can forget to install it in another app that would use the module
so a module is a black box you insert in an app to have functionnalities
is there is a class, it must be outside, and is a pain not to forget
so my big concern would be to be able to add classes into modules
then I probably would never use a structure again !
thanks.
I also use too many times structures instead of classes
the reason is simple : I can store structures in a module, but not a class
and when you program big apps, you have to segment into modules, reusable modules
and if the class is outside the module, then you can forget to install it in another app that would use the module
so a module is a black box you insert in an app to have functionnalities
is there is a class, it must be outside, and is a pain not to forget
so my big concern would be to be able to add classes into modules
then I probably would never use a structure again !
thanks.[/quote]
Create the class and move it into the module
Modules that manipulate structures is “procedural”
An OO way would be to have those methods that you have in a module IN the class
Suppose you have a structure that represents an image
Then you module might have methods for
apply mask to image like applyMaskToImage( image, mask)
rotate image like rotateImage( image , rotationAngle )
In an OO setup you’d have a class, Image, that has methods
apply mask to image like applyMaskToImage( mask) <<< you dont need to pass the image as its “self”
rotate image like rotateImage( rotationAngle ) <<< you dont need to pass the image as its “self”
You dont pass the image because the class IS the image to be manipulated
You dont have to remember to include the module & class because the class HAS the code in it
That is good info Norman. So in your opinion, Structures are sort of defunct now and really should be superseded by using Classes? I will try to start using Classes instead moving forward.
[quote=242199:@Norman Palardy]Create the class and move it into the module
Modules that manipulate structures is “procedural”
An OO way would be to have those methods that you have in a module IN the class
Suppose you have a structure that represents an image
Then you module might have methods for
apply mask to image like applyMaskToImage( image, mask)
rotate image like rotateImage( image , rotationAngle )
In an OO setup you’d have a class, Image, that has methods
apply mask to image like applyMaskToImage( mask) <<< you dont need to pass the image as its “self”
rotate image like rotateImage( rotationAngle ) <<< you dont need to pass the image as its “self”
You dont pass the image because the class IS the image to be manipulated
You dont have to remember to include the module & class because the class HAS the code in it
Just my 2 cents[/quote]
I have multiples structures in a module
I can have one class with methods
but if I have multiple class with methods that involve the classes it becomes difficult to handle in OO
I would like to have a module with multiple classes in it, but you can’t have classes in modules
you can only have structures in a module
that’s why I use it that way
but yes it would be better to have classes in modules
[quote=242250:@Jean-Yves Pochez]sig !
old realstudio or even realbasic reflex ? remember I couldnt put a class in a module !
since when is it possible ?[/quote]
7 or 8 years at least
what you CANT do is have an EXTERNAL module with classes in it
ahhh so my memory is not that bad… yes i use intensively external modules so I can reuse code between all my projects
so I cannot put classes into modules I was right, but I use external modules that’s why it does not work for me…
sharing code doesn’t require externals
esp if you have a compliant version control system (you DO use one right ???)
svn makes this decently easy with subprojects
vault made it ridiculously easy (its literally drag & drop in their windows UI)
git may (but I’m not a git guru)
and there are pros & cons to using externals
pro
its easy to share code
EVERY project always has “the latest”
con
EVERY project always has “the latest” so IF you need to work on one project in 2013r3, one in 2014, one in 2015 then they may rely on things that dont exist in other versions. It can/could force you to use the same version across all projects which may or may not be feasible depending on your circumstances
that said I’d still use modules with classes & not structures
structures force you into a design pattern that is not very OO and that can have bigger implications than all the rest of this little missive
However, it immediately bugs out at compile with NilObjectException, as if the property “dog” does not exist
I think something is wrong with my scope, as if I make class_dog’s properties all Shared Properties instead it works - but that defeats the purpose of my using classes in this manner
So where’s my scope breakdown? Shouldn’t the window property of “Dog” with the type of “class_dog” be available to edit from within a Method?
No scope problem
A property represents a thing that CAN refer to an instance
BUT you have to create that instance somewhere otherwise it refers to nothing (nil)