473,761 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting Objects Within Class?

I'm not sure how to ask for what I want, so I can't find it on Google.

I'll explain.....

I have a class 'hanging' with a constructor:

class hanging{
function hanging(){
}

}

//and a class curtain
class curtain extends hanging{
function curtain(){
}

}

//and a blind
class blind extends hanging{
function blind(){
}

}

In the hanging constructor the object gets database info part of which
has an ID number for its type, so I want the constructor to have
something like....

function hanging(){
//blah blah
if ( type_ID is curtain ){
$this-is a curtain object
}
//etc etc

}

But I don't know how. Any help would be appreciated.

Thanks

Pete

Oct 3 '07 #1
5 2661
pe**********@go oglemail.com wrote:
I'm not sure how to ask for what I want, so I can't find it on Google.

I'll explain.....

I have a class 'hanging' with a constructor:

class hanging{
function hanging(){
}

}

//and a class curtain
class curtain extends hanging{
function curtain(){
}

}

//and a blind
class blind extends hanging{
function blind(){
}

}

In the hanging constructor the object gets database info part of which
has an ID number for its type, so I want the constructor to have
something like....

function hanging(){
//blah blah
if ( type_ID is curtain ){
$this-is a curtain object
}
//etc etc

}

But I don't know how. Any help would be appreciated.

Thanks

Pete
Pete,

You can't that way. When you create the class, you need to know what
type it is then - 'curtain' or 'blind', for instance. But once you're
in the constructor, you already have the object created.

You could create a static method in the hanging class which will
retrieve the information from the database, then depending on the type,
create the correct object and return its address. This isn't an ideal
solution because if you add 'tapestry' as a derived class of hanging,
you need to go back and modify the static function. But it works.

But I also have to ask - do you really need all three classes? What are
the differences between 'blind' and 'curtain'? Could they actually be
handled by a single class?

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Oct 3 '07 #2
On 3 Oct, 12:55, Jerry Stuckle <jstuck...@attg lobal.netwrote:
petethebl...@go oglemail.com wrote:
I'm not sure how to ask for what I want, so I can't find it on Google.
I'll explain.....
I have a class 'hanging' with a constructor:
class hanging{
function hanging(){
}
}
//and a class curtain
class curtain extends hanging{
function curtain(){
}
}
//and a blind
class blind extends hanging{
function blind(){
}
}
In the hanging constructor the object gets database info part of which
has an ID number for its type, so I want the constructor to have
something like....
function hanging(){
//blah blah
if ( type_ID is curtain ){
$this-is a curtain object
}
//etc etc
}
But I don't know how. Any help would be appreciated.
Thanks
Pete

Pete,

You can't that way. When you create the class, you need to know what
type it is then - 'curtain' or 'blind', for instance. But once you're
in the constructor, you already have the object created.

You could create a static method in the hanging class which will
retrieve the information from the database, then depending on the type,
create the correct object and return its address. This isn't an ideal
solution because if you add 'tapestry' as a derived class of hanging,
you need to go back and modify the static function. But it works.

But I also have to ask - do you really need all three classes? What are
the differences between 'blind' and 'curtain'? Could they actually be
handled by a single class?

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attgl obal.net
=============== ===
Thanks for that Jerry.
I was trying to keep things simple (!).
At the moment I have 'design' and it does everything, but it has 30
odd methods
and I wanted to see if I could make things easier to manage. The idea
was to have
inheriting classes 'cushion', 'blind', 'pelmet', 'curtain' and so on
(it's a soft
furnishings site) where functions relevant only to cushions (or
whatever) would be
in the child class.

The reason I need to call design without knowing its type is that it
has to speak up
for itself in the shopping basket page.

Mind you.... it works at the moment, so maybe I'm only making grief
for myself.

Thanks again.

Pete

Oct 3 '07 #3
pe**********@go oglemail.com wrote:
On 3 Oct, 12:55, Jerry Stuckle <jstuck...@attg lobal.netwrote:
>petethebl...@g ooglemail.com wrote:
>>I'm not sure how to ask for what I want, so I can't find it on Google.
I'll explain.....
I have a class 'hanging' with a constructor:
class hanging{
function hanging(){
}
}
//and a class curtain
class curtain extends hanging{
function curtain(){
}
}
//and a blind
class blind extends hanging{
function blind(){
}
}
In the hanging constructor the object gets database info part of which
has an ID number for its type, so I want the constructor to have
something like....
function hanging(){
//blah blah
if ( type_ID is curtain ){
$this-is a curtain object
}
//etc etc
}
But I don't know how. Any help would be appreciated.
Thanks
Pete
Pete,

You can't that way. When you create the class, you need to know what
type it is then - 'curtain' or 'blind', for instance. But once you're
in the constructor, you already have the object created.

You could create a static method in the hanging class which will
retrieve the information from the database, then depending on the type,
create the correct object and return its address. This isn't an ideal
solution because if you add 'tapestry' as a derived class of hanging,
you need to go back and modify the static function. But it works.

But I also have to ask - do you really need all three classes? What are
the differences between 'blind' and 'curtain'? Could they actually be
handled by a single class?

--
============== ====
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attg lobal.net
============== ====

Thanks for that Jerry.
I was trying to keep things simple (!).
Yea, but it's not always that way, unfortunately.
At the moment I have 'design' and it does everything, but it has 30
odd methods
and I wanted to see if I could make things easier to manage. The idea
was to have
inheriting classes 'cushion', 'blind', 'pelmet', 'curtain' and so on
(it's a soft
furnishings site) where functions relevant only to cushions (or
whatever) would be
in the child class.
OK, that's reasonable enough.
The reason I need to call design without knowing its type is that it
has to speak up
for itself in the shopping basket page.

Mind you.... it works at the moment, so maybe I'm only making grief
for myself.

Thanks again.

Pete
I suspect everything's in a single class right now, and some methods are
applicable to one object while not another. This would be a great use
of inheritance.

What I might recommend is to use the static function idea in the
'hanging' class. Maybe pass it a database row id (or the contents of
the row, if you wish) and have it build the appropriate class from the data.

As I said - it's not perfectly clean, but if you want to use
inheritance, someone needs to know what the classes you're using are.
And keeping everything in this one function means you can easily add
more classes to the inheritance hierarchy later.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Oct 3 '07 #4
Thanks for that Jerry.
I was trying to keep things simple (!).

Yea, but it's not always that way, unfortunately.
At the moment I have 'design' and it does everything, but it has 30
odd methods
and I wanted to see if I could make things easier to manage. The idea
was to have
inheriting classes 'cushion', 'blind', 'pelmet', 'curtain' and so on
(it's a soft
furnishings site) where functions relevant only to cushions (or
whatever) would be
in the child class.

OK, that's reasonable enough.
The reason I need to call design without knowing its type is that it
has to speak up
for itself in the shopping basket page.
Mind you.... it works at the moment, so maybe I'm only making grief
for myself.
Thanks again.
Pete

I suspect everything's in a single class right now, and some methods are
applicable to one object while not another. This would be a great use
of inheritance.

What I might recommend is to use the static function idea in the
'hanging' class. Maybe pass it a database row id (or the contents of
the row, if you wish) and have it build the appropriate class from the data.

As I said - it's not perfectly clean, but if you want to use
inheritance, someone needs to know what the classes you're using are.
And keeping everything in this one function means you can easily add
more classes to the inheritance hierarchy later.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attgl obal.net
=============== ===
Pete,

I was wrestling with much the same issue recently with a form library
I'm developing. I wanted there to be a Form class which would be
responsible for managing all the elements in a webpage form and a
FormElement class which the add_element method of the Form class would
use to construct child object elements of specific form field types
(e.g. ElementTextInpu t, an extension of FormElement).

It doesn't involve a database, but I did want to validate element
types against a central array. For that I ended up using the idea
Jerry suggested here: a static method in the base element class (I may
move it to the form class before I'm finished) that can be called
statically to retrieve a valid list of element types:

function get_valid_types ()
{
$V = array(); // return

// element families
$V['INPUT_TEXT'] = array('input_te xt', 'input_email', 'input_url',
'input_password ');
$V['TEXTAREA'] = array('textarea ');

// all elements
$V['TYPE'] = array_merge($V['INPUT_TEXT'],$V['TEXTAREA']);

return $V;
}

So if I add a new subclass (say, a radio-button menu class), I can
just update this method.

Like you, I originally was dreaming of something simpler and more
elegant for the library as a whole. The resulting code ended up being
more complicated and required an additional Factory class to make
everything work as transparently on the API end as I wished. But once
I got that sorted out, implementation is pretty simple:

$TestForm = new CeoForm($debug) ;
$TestForm->add_element($n ame='text_input ', $type='input_te xt',
$label='input some text', $is_required=1, $maxlength=255,
$minlength=4);
$TestForm->add_element($n ame='email_inpu t', $type='input_em ail',
$label='email address', $is_required=1) ;
$TestForm->add_element($n ame='url_input' , $type='input_ur l',
$label='web address', $is_required=0) ;

if ( $TestForm->is_valid() )
{
// insert results in db or whatever...
}
else
{
$show_form = 1;
}

Anyway, your problem strikes me as the same basic problem. If
interested, you can view the source for this library here:

http://klenwell.googlecode.com/svn/t...ceo/ext/input/

Tom

Oct 3 '07 #5
On 3 Oct, 17:49, klenwell <klenw...@gmail .comwrote:
Thanks for that Jerry.
I was trying to keep things simple (!).
Yea, but it's not always that way, unfortunately.
At the moment I have 'design' and it does everything, but it has 30
odd methods
and I wanted to see if I could make things easier to manage. The idea
was to have
inheriting classes 'cushion', 'blind', 'pelmet', 'curtain' and so on
(it's a soft
furnishings site) where functions relevant only to cushions (or
whatever) would be
in the child class.
OK, that's reasonable enough.
The reason I need to call design without knowing its type is that it
has to speak up
for itself in the shopping basket page.
Mind you.... it works at the moment, so maybe I'm only making grief
for myself.
Thanks again.
Pete
I suspect everything's in a single class right now, and some methods are
applicable to one object while not another. This would be a great use
of inheritance.
What I might recommend is to use the static function idea in the
'hanging' class. Maybe pass it a database row id (or the contents of
the row, if you wish) and have it build the appropriate class from the data.
As I said - it's not perfectly clean, but if you want to use
inheritance, someone needs to know what the classes you're using are.
And keeping everything in this one function means you can easily add
more classes to the inheritance hierarchy later.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attgl obal.net
=============== ===

Pete,

I was wrestling with much the same issue recently with a form library
I'm developing. I wanted there to be a Form class which would be
responsible for managing all the elements in a webpage form and a
FormElement class which the add_element method of the Form class would
use to construct child object elements of specific form field types
(e.g. ElementTextInpu t, an extension of FormElement).

It doesn't involve a database, but I did want to validate element
types against a central array. For that I ended up using the idea
Jerry suggested here: a static method in the base element class (I may
move it to the form class before I'm finished) that can be called
statically to retrieve a valid list of element types:

function get_valid_types ()
{
$V = array(); // return

// element families
$V['INPUT_TEXT'] = array('input_te xt', 'input_email', 'input_url',
'input_password ');
$V['TEXTAREA'] = array('textarea ');

// all elements
$V['TYPE'] = array_merge($V['INPUT_TEXT'],$V['TEXTAREA']);

return $V;

}

So if I add a new subclass (say, a radio-button menu class), I can
just update this method.

Like you, I originally was dreaming of something simpler and more
elegant for the library as a whole. The resulting code ended up being
more complicated and required an additional Factory class to make
everything work as transparently on the API end as I wished. But once
I got that sorted out, implementation is pretty simple:

$TestForm = new CeoForm($debug) ;
$TestForm->add_element($n ame='text_input ', $type='input_te xt',
$label='input some text', $is_required=1, $maxlength=255,
$minlength=4);
$TestForm->add_element($n ame='email_inpu t', $type='input_em ail',
$label='email address', $is_required=1) ;
$TestForm->add_element($n ame='url_input' , $type='input_ur l',
$label='web address', $is_required=0) ;

if ( $TestForm->is_valid() )
{
// insert results in db or whatever...}

else
{
$show_form = 1;

}

Anyway, your problem strikes me as the same basic problem. If
interested, you can view the source for this library here:

http://klenwell.googlecode.com/svn/t...ceo/ext/input/

Tom
Thanks Jerry. Thanks Tom.
I'm diving off somewhere else at the moment but I'll take a look at
that code when I get back.
I appreciate you both having a look at this for me.

Pete

Oct 3 '07 #6

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

Similar topics

4
3478
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A good way to do this is by example. So I will give an example and please tell me what you think: I have a base class A with a virtual destructor, and a class B that is it inherits publicly from A and defines som extra stuff.
3
2564
by: DJ.precario | last post by:
Hi, I have a vector (myVector) of BaseClass objects. I store in it a DerivedClass object (where DerivedClass inherits from BaseClass). Later I want to get the last element in the vector and store it in a DerivedClass variable, doing: DerivedClass d = (DerivedClass)myVector;
4
16317
by: Fabrizio | last post by:
Hi I cannot figure why it isn't possible to cast a struct array to an object array. I written a structure like this: public struct Test { private int TestA; private int TestB;
8
9469
by: Razak | last post by:
Hi, I have a class which basically do Impersonation in my web application. From MS KB sample:- ++++++++++++++++++++code starts Dim impersonationContext As System.Security.Principal.WindowsImpersonationContext Dim currentWindowsIdentity As System.Security.Principal.WindowsIdentity
61
4601
by: Ken Allen | last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years. One of the drawbacks with VB6 and earlier was the difficulty in casting a 'record' to a different 'shape' so one could perform different manipulations on it. For example, I have a complex data structure, which I can represent in a VB6 TYPE declaration, but I cannot easily convert that to a fixed length array of unsigned bytes so that I could perform a checksum...
3
2772
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some dead ends. I'm posting this to get some feedback on wether I'm going in the right direction, and at the same time hopefully save others from going through the process.
5
5929
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style casting. from what I am reading, I should use reinterpret cast in this situation, is that correct? Why does static and dynamic casting fail me? // please excuse the windows types, it is necessary in this code, // but the question remains C++ related
11
32464
by: Frederic Rentsch | last post by:
Hi all, If I derive a class from another one because I need a few extra features, is there a way to promote the base class to the derived one without having to make copies of all attributes? class Derived (Base): def __init__ (self, base_object): # ( copy all attributes ) ...
14
6024
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared static inside functions (i.e. local static objects) 5. objects declared at file scope.
9
2459
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I have a derived class object and then upcast it to its base class, which cast operator should I use? Is it static_cast, or, can I simply cast it implicitly without any operator? Does this upcasting remove the derived class portion of the object?...
0
9377
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.