473,401 Members | 2,139 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,401 software developers and data experts.

Is it possible to hide class methods when the class has not been properly instantiated?

Hi all,

Do you know if it is possible to hide class methods when the class has
not been "properly" instantiated?

Say in the following example, I'd like to hide or disable Drive() when
the numWheels == 0.

class Car
{

int _numWheels;

Car()
{
_numWheels = 0;
}

Car(int numWheels)
{
_numWheels = numWheels;
}

void Drive() {...}
}

I don't suppose there's a way to do it, other than checking numWheels >
0 in each class method.

What's the proper way to do this? How do you guys handle this
situation?

Thanks,
Andre

Nov 17 '05 #1
5 1338
Hi,

No, there is no way of doing that, you will have to check your
preconditions in each method.

Now in your case, you could check in the constructor if numWheels is > 0 ,
if not throw an exception

you could do the same if you have a property allowing to change the member
variable.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<ah****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi all,

Do you know if it is possible to hide class methods when the class has
not been "properly" instantiated?

Say in the following example, I'd like to hide or disable Drive() when
the numWheels == 0.

class Car
{

int _numWheels;

Car()
{
_numWheels = 0;
}

Car(int numWheels)
{
_numWheels = numWheels;
}

void Drive() {...}
}

I don't suppose there's a way to do it, other than checking numWheels >
0 in each class method.

What's the proper way to do this? How do you guys handle this
situation?

Thanks,
Andre

Nov 17 '05 #2
Hi Ignacio,

Great suggestion!

That's just what I'm after.

Thanks for that,
Andre

Nov 17 '05 #3
<ah****@gmail.com> a écrit dans le message de news:
11**********************@g43g2000cwa.googlegroups. com...
Hi all,

Do you know if it is possible to hide class methods when the class has
not been "properly" instantiated?

Say in the following example, I'd like to hide or disable Drive() when
the numWheels == 0.
Simply hide the default constructor :
class Car
{

int _numWheels;

private Car()
{
_numWheels = 0;
}

public Car(int numWheels)
{
_numWheels = numWheels;
}

public void Drive() {...}
}


Then it is impossible to construct a valid instance without passing a number
to the parameterised constructor.

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #4
Hi,

Or just remove it, most probably it has no use at all
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Joanna Carter (TeamB)" <jo*****@nospamforme.com> wrote in message
news:e3**************@TK2MSFTNGP09.phx.gbl...
<ah****@gmail.com> a écrit dans le message de news:
11**********************@g43g2000cwa.googlegroups. com...
Hi all,

Do you know if it is possible to hide class methods when the class has
not been "properly" instantiated?

Say in the following example, I'd like to hide or disable Drive() when
the numWheels == 0.


Simply hide the default constructor :
class Car
{

int _numWheels;

private Car()
{
_numWheels = 0;
}

public Car(int numWheels)
{
_numWheels = numWheels;
}

public void Drive() {...}
}


Then it is impossible to construct a valid instance without passing a
number
to the parameterised constructor.

Joanna

--
Joanna Carter
Consultant Software Engineer

Nov 17 '05 #5
As suggested, you can make it impossible to construct an invalid object
or you
can provide an isValid() method or flag.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #6

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

Similar topics

8
by: Dev | last post by:
Hello, Why an Abstract Base Class cannot be instantiated ? Does anybody know of the object construction internals ? What is the missing information that prevents the construction ? TIA....
5
by: Felix I. Wyss | last post by:
Good Afternoon, I recently noticed that some very simple methods of a template declared and used in a DLL library get inlined when used by the DLL itself, but not by other DLLs and EXEs. After...
4
by: drb | last post by:
Hi, I'm hoping this is a stupid question, so I apologize in advance. After creating WSDL files from scratch for a new web service, I used the WSDL.exe tool to generate C# stubs for both the...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.