473,289 Members | 1,723 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Adding a custom object to an array

Hey all,

Anyone have any idea what im doing wrong here:

$image = new Image();
$image->SetImageContent($imageRow['imageContent']);
$image->SetFileSize($imageRow['fileSize']);
$image->SetFileName($imageRow['fileName']);
$image->SetImageId($imageRow['imageId']);
$image->SetParentImageId($imageRow['parentId']);
$image->SetFileType($imageRow['fileType']);
$image->SetImageHeight($imageRow['height']);
$image->SetImageWidth($imageRow['width']);
$image->SetIsThumbnail($imageRow['isThumb']);
$this->mImages[$this->mImageCount] = $image;

where Image is a custom class and mImages is an array declared as
follows:
$this->mImages = array();

Whats happening is im executing this function
function GetImage($imageIndex)
{
return $this->mImages[$imageIndex];

}

and then trying to call a member function on the returned value
i.e.
$image = $album->GetImage(1);
$imageId = $image->GetImageId();

and im getting this error" Fatal error: Call to a member function on a
non-object in
/home/flyerweb/public_html/ImageManipulator/POCPages/DisplayAllImagesInAlbum.php
on line 30"

Any idea why i cant add a custom object to an array then pull it back
out and call a method?

May 7 '06 #1
3 3760
Rik
Areric wrote:
Any idea why i cant add a custom object to an array then pull it back
out and call a method?


<?php

class A{
var $elements = array();
var $count;
function A(){
$this->count = 0;
}
function add_element($text){
$object = new B($text);
$this->elements[$this->count] = $object;
$this->count++;
}
function return_element($int){
return $this->elements[$int];
}
}
class B{
var $text;
function B($var){
$this->text = $var;
}
function display(){
echo $this->text;
}
}

$test = new A();
$test->add_element("This works");
$test->add_element("Several times even");
$object = $test->return_element(0);
$object2 = $test->return_element(1);
$object->display();
$object2->display();
?>
Outputs: "This worksSeveral times even"

So, I think the problem would be elsewhere.
What do you see if you var_dump() your $album, or print_r($album->mImages),
before getting the image out? Does it really contain the object?

Maybe the Image class is only declared after the album has already tried to
add the images?

What happens if in the method you add the images, instead of
$this->mImages[$this->mImageCount] = $image;
return is_object($image):

Does it return false or true?
Grtz,
--
Rik Wasmus
May 7 '06 #2
Rik

THanks for the help you were right the problem was elsewhere. For some
reason my mImageCount wasnt getting initalized properly. So when i
added my first image it was adding it to a index with no value, where
when i tried to reference it i was referencing index 0. The weird thing
was i had the mImageCount = 0 in the constructor.

Oh well i fixed it by initalizing the variable where it was declared
instead of the constructor.

May 7 '06 #3
Rik
Areric wrote:
Rik

THanks for the help you were right the problem was elsewhere. For some
reason my mImageCount wasnt getting initalized properly. So when i
added my first image it was adding it to a index with no value, where
when i tried to reference it i was referencing index 0. The weird
thing was i had the mImageCount = 0 in the constructor.


Euhm, you've lost me.
If you initialize mImageCount = 0;
Add an object by $this->mImages[$this->mImageCount] = $image;
Surely it would be at index 0....
Or were you incrementing mImageCount beforehand?

Grtz,
--
Rik Wasmus
May 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex Stevens | last post by:
I'd already posted this in microsoft.public.dotnet.framework.windowsforms and microsoft.public.dotnet.framework.windowsforms.controls to no avail so apologies for the cross-posting. Hi, I'm...
7
by: John Grandy | last post by:
My ASP.NET Web Service project has a Web Method that returns an array filled with instances of a custom class. The custom class is defined in a Class Library that is included in the web-service...
5
by: Water Cooler v2 | last post by:
I know that we can add a single name value entry in app.config or web.config in the configuration/configSettings/appSettings section like so: <add key="key" value="value" /> Question: I want...
9
by: Greger | last post by:
Hi, I am building an architecture that passes my custom objects to and from webservices. (Our internal architecture requires me to use webservices to any suggestion to use other remoting...
3
by: Kannan | last post by:
Hi, I am trying to created Outloook Add-in Com in outlook using C#. I have seen this URL for developing this sample http://support.microsoft.com/?kbid=302901 When I executed this program it...
11
by: Pete Kane | last post by:
Hi All, does anyone know how to add TabPages of ones own classes at design time ? ideally when adding a new TabControl it would contain tab pages of my own classes, I know you can achieve this with...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
12
by: Bob Jones | last post by:
I have an odd business requirement and I think that the implementation is not correct in the terms of OOP development. Any help on the concepts would be very appreciated! We currently have a...
0
by: nelis | last post by:
Anyone ever ran into the issue of adding custom properties to a user? Here's a code snippet i wrote: DirectoryEntry newUser = deContainer.Children.Add("CN=" + username, "User");...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.