473,770 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Structure as object property

Are there any reasons why I shouldn't or couldn't use a structure as a
property in a class? Specifically since structures are value types and
objects are reference types?

I have a Shipping object that has 4 different types of addresses.
Initially, I created 4 instances of an Address object, but seem
inefficient to me since the Address itslef doesn't really have (or need)
and methods... I was thinking a structure would be more efficient, but
thought that the different types (reference v.s. value) might cause a
problem when passing the object around...

I could also use some help in acessibility or scope. I don't want the
user of my object to create an instance of the Address class (structure?)
(as well as a few other objects), but they do need to access pieces of a
created Address.

Hmmm, I'm not sure I explained that well...

I do want the user of the class to be able to do this:

oShipment.ShipT o.Address1 = "blah blah blah"

But I don't want them to be able to do this:

oAddress = new Address();
oAddress.Addres s1 = "blah blah blah"

What's the best way to accomplish this?

Thanx!
Joe

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Dec 14 '05 #1
6 1402
As long as the class initializes the private Address object exposed by the
ShipTo property, the user will still be able to say oShipment.ShipT o.Address
= "whatever".

"JSheble" <js***********@ logicor.com> wrote in message
news:op******** *******@joelapt op.logicorphx2. com...
Are there any reasons why I shouldn't or couldn't use a structure as a
property in a class? Specifically since structures are value types and
objects are reference types?

I have a Shipping object that has 4 different types of addresses.
Initially, I created 4 instances of an Address object, but seem
inefficient to me since the Address itslef doesn't really have (or need)
and methods... I was thinking a structure would be more efficient, but
thought that the different types (reference v.s. value) might cause a
problem when passing the object around...

I could also use some help in acessibility or scope. I don't want the
user of my object to create an instance of the Address class (structure?)
(as well as a few other objects), but they do need to access pieces of a
created Address.

Hmmm, I'm not sure I explained that well...

I do want the user of the class to be able to do this:

oShipment.ShipT o.Address1 = "blah blah blah"

But I don't want them to be able to do this:

oAddress = new Address();
oAddress.Addres s1 = "blah blah blah"

What's the best way to accomplish this?

Thanx!
Joe

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

Dec 14 '05 #2
So I could define my Address structure as Private with private fields? Or
Internal?

On Wed, 14 Dec 2005 11:03:00 -0700, Marina <so*****@nospam .com> wrote:
As long as the class initializes the private Address object exposed by
the
ShipTo property, the user will still be able to say
oShipment.ShipT o.Address
= "whatever".

"JSheble" <js***********@ logicor.com> wrote in message
news:op******** *******@joelapt op.logicorphx2. com...
Are there any reasons why I shouldn't or couldn't use a structure as a
property in a class? Specifically since structures are value types and
objects are reference types?

I have a Shipping object that has 4 different types of addresses.
Initially, I created 4 instances of an Address object, but seem
inefficient to me since the Address itslef doesn't really have (or need)
and methods... I was thinking a structure would be more efficient, but
thought that the different types (reference v.s. value) might cause a
problem when passing the object around...

I could also use some help in acessibility or scope. I don't want the
user of my object to create an instance of the Address class
(structure?)
(as well as a few other objects), but they do need to access pieces of a
created Address.

Hmmm, I'm not sure I explained that well...

I do want the user of the class to be able to do this:

oShipment.ShipT o.Address1 = "blah blah blah"

But I don't want them to be able to do this:

oAddress = new Address();
oAddress.Addres s1 = "blah blah blah"

What's the best way to accomplish this?

Thanx!
Joe

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/



--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Dec 14 '05 #3
Huh?

I was talking about making it a class, and having the class that contains
properties of that type be responsible for making sure there is always a
valid instance being returned by the property.

"JSheble" <js***********@ logicor.com> wrote in message
news:op******** *******@joelapt op.logicorphx2. com...
So I could define my Address structure as Private with private fields? Or
Internal?

On Wed, 14 Dec 2005 11:03:00 -0700, Marina <so*****@nospam .com> wrote:
As long as the class initializes the private Address object exposed by
the
ShipTo property, the user will still be able to say
oShipment.ShipT o.Address
= "whatever".

"JSheble" <js***********@ logicor.com> wrote in message
news:op******** *******@joelapt op.logicorphx2. com...
Are there any reasons why I shouldn't or couldn't use a structure as a
property in a class? Specifically since structures are value types and
objects are reference types?

I have a Shipping object that has 4 different types of addresses.
Initially, I created 4 instances of an Address object, but seem
inefficient to me since the Address itslef doesn't really have (or need)
and methods... I was thinking a structure would be more efficient, but
thought that the different types (reference v.s. value) might cause a
problem when passing the object around...

I could also use some help in acessibility or scope. I don't want the
user of my object to create an instance of the Address class
(structure?)
(as well as a few other objects), but they do need to access pieces of a
created Address.

Hmmm, I'm not sure I explained that well...

I do want the user of the class to be able to do this:

oShipment.ShipT o.Address1 = "blah blah blah"

But I don't want them to be able to do this:

oAddress = new Address();
oAddress.Addres s1 = "blah blah blah"

What's the best way to accomplish this?

Thanx!
Joe

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/



--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

Dec 14 '05 #4
Ahhh... I was talking about making it a structure first, then asked how to
keep users of my object from instantiating an instance of it, but still be
able to access it via a public property of the containing object...

On Wed, 14 Dec 2005 11:22:01 -0700, Marina <so*****@nospam .com> wrote:
Huh?

I was talking about making it a class, and having the class that contains
properties of that type be responsible for making sure there is always a
valid instance being returned by the property.

"JSheble" <js***********@ logicor.com> wrote in message
news:op******** *******@joelapt op.logicorphx2. com...
So I could define my Address structure as Private with private fields?
Or
Internal?

On Wed, 14 Dec 2005 11:03:00 -0700, Marina <so*****@nospam .com> wrote:
As long as the class initializes the private Address object exposed by
the
ShipTo property, the user will still be able to say
oShipment.ShipT o.Address
= "whatever".

"JSheble" <js***********@ logicor.com> wrote in message
news:op******** *******@joelapt op.logicorphx2. com...
Are there any reasons why I shouldn't or couldn't use a structure as a
property in a class? Specifically since structures are value types
and
objects are reference types?

I have a Shipping object that has 4 different types of addresses.
Initially, I created 4 instances of an Address object, but seem
inefficient to me since the Address itslef doesn't really have (or
need)
and methods... I was thinking a structure would be more efficient,
but
thought that the different types (reference v.s. value) might cause a
problem when passing the object around...

I could also use some help in acessibility or scope. I don't want the
user of my object to create an instance of the Address class
(structure?)
(as well as a few other objects), but they do need to access pieces
of a
created Address.

Hmmm, I'm not sure I explained that well...

I do want the user of the class to be able to do this:

oShipment.ShipT o.Address1 = "blah blah blah"

But I don't want them to be able to do this:

oAddress = new Address();
oAddress.Addres s1 = "blah blah blah"

What's the best way to accomplish this?

Thanx!
Joe

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/



--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Dec 14 '05 #5
Your struct must be declared at the same accessibility level as the
property that returns it. Otherwise the compiler will complain that you
are exposing a (for example) public property whose return type is (for
example) internal, and so your clients could never use the property.

However, you _could_ make all of the constructors for the struct
internal, meaning that no client code could ever instantiate a (valid)
Address. (Note, however, that client code _could declare_ an Address,
which would create the Address with all fields null / zero. They just
couldn't initialize those fields to anything meaningful.) This works
better with classes, by the way, for which making all of the
constructors internal / protected / private effectively means that the
outside world can _use_ objects of that class but could never _create_
one.

However, aside from all of this I think that you want Address to be a
class, not a struct.
I created 4 instances of an Address object, but seem
inefficient to me since the Address itslef doesn't really have (or need)
any methods...
That's not really a good reason to make something a struct. Make it a
struct if it is logically a value, like an int, a double, or a
DateTime, or if you're doing massive numbers of calculations with it
and allocating / garbage collecting transient instances of it would
create performance problems (e.g. Point, Rectangle). Four addresses
doesn't really fit into that category, IMHO.
I was thinking a structure would be more efficient, but
thought that the different types (reference v.s. value) might cause a
problem when passing the object around...


Yes, it will cause problems, particularly if your Address object
exposes mutable properties (with "set" methods). It's not the end of
the world, but I think that the extra thinking you'll have to do in
order to use an Address struct successfully will more than outweigh any
minor efficiency improvements that might result. Just my opinion.

Dec 14 '05 #6
Right, but I think your concerns with passing around structures are valid.
As soon as you have a second variable assigned to a structure instance, it
actually gets a copy of it, and from then on there are 2 different instances
of the structure. What you change in one does not effect the other - this is
not desired behavior for most cases.

So you should stick with the class, and you can still access everything in
it via a property and not have to worry about the end consumer instantiating
the property, because the class defining the property should be the one to
do it.

"JSheble" <js***********@ logicor.com> wrote in message
news:op******** *******@joelapt op.logicorphx2. com...
Ahhh... I was talking about making it a structure first, then asked how to
keep users of my object from instantiating an instance of it, but still be
able to access it via a public property of the containing object...

On Wed, 14 Dec 2005 11:22:01 -0700, Marina <so*****@nospam .com> wrote:
Huh?

I was talking about making it a class, and having the class that contains
properties of that type be responsible for making sure there is always a
valid instance being returned by the property.

"JSheble" <js***********@ logicor.com> wrote in message
news:op******** *******@joelapt op.logicorphx2. com...
So I could define my Address structure as Private with private fields?
Or
Internal?

On Wed, 14 Dec 2005 11:03:00 -0700, Marina <so*****@nospam .com> wrote:

As long as the class initializes the private Address object exposed by
the
ShipTo property, the user will still be able to say
oShipment.ShipT o.Address
= "whatever".

"JSheble" <js***********@ logicor.com> wrote in message
news:op******** *******@joelapt op.logicorphx2. com...
> Are there any reasons why I shouldn't or couldn't use a structure as a
> property in a class? Specifically since structures are value types
> and
> objects are reference types?
>
> I have a Shipping object that has 4 different types of addresses.
> Initially, I created 4 instances of an Address object, but seem
> inefficient to me since the Address itslef doesn't really have (or
> need)
> and methods... I was thinking a structure would be more efficient,
> but
> thought that the different types (reference v.s. value) might cause a
> problem when passing the object around...
>
> I could also use some help in acessibility or scope. I don't want the
> user of my object to create an instance of the Address class
> (structure?)
> (as well as a few other objects), but they do need to access pieces
> of a
> created Address.
>
> Hmmm, I'm not sure I explained that well...
>
> I do want the user of the class to be able to do this:
>
> oShipment.ShipT o.Address1 = "blah blah blah"
>
> But I don't want them to be able to do this:
>
> oAddress = new Address();
> oAddress.Addres s1 = "blah blah blah"
>
> What's the best way to accomplish this?
>
> Thanx!
> Joe
>
> --
> Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/



--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

Dec 14 '05 #7

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

Similar topics

5
451
by: Andre | last post by:
I have two questions; 1) When executing the sub routine "TestStructure", I'm trying to update the member "intTyres" in the structure "structureCar" dynamically using System.Reflection. The code executes, but the value does not change, nor is there an exception thrown. 2) Read comments in sub routine "TestIntegerProperty"
1
3267
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data structures (ints), and more complex data structures (strings and vectors) in it, and would like to write the entire class to a data file that could then be read back and loaded. However I'm having difficulty with this -- I found out (due to an...
3
1984
by: RBCC | last post by:
I have a form with a listbox and two textboxes. In the listbox I have the make and models of automobiles. and as the user clicks on the make of the car in the listbox I would like to output the make and model in the textboxes, how is this done with an arraylist John --- Posted using Wimdows.net NntpNews Component - Posted from .NET's Largest Community Website: http://www.dotnetjunkies.com/newsgroups/
3
1286
by: dan heskett | last post by:
Hello group, I am trying to get used to vb.net coming from a far far far away set of tools. What i want to do is setup an object in my application that contains other object types, that act like collections. For example: objQuotes.Quotes("2004-10-08").Item("MSFT").StockQuote = "45.45"
15
8241
by: Charles Law | last post by:
I have adapted the following code from the MSDN help for PropertyInfo SetValue. In the original code, the structure MyStructure is defined as a class MyProperty, and it works as expected. There is also a minor change in class Mypropertyinfo, which I have commented out. When using a structure, however, the second call to GetValue returns "Default caption". Can anyone tell me why, and how I can make this work? <code> Imports System
2
1372
by: eBob.com | last post by:
I know that this must be a really dumb question but I just can't find an answer. I want to associate some information with a RichTextBox. The Tag property seems to be the intended way to "hang" some additional information on a Control. I've created a structure, rtbAuxInfo, for the information. I think that I have found the right syntax for assigning a pointer to the structure to the Tag property of the RichTextBox, but I can't figure...
4
1422
by: John | last post by:
I am having a problem accessing the members in a structure via an interface and Class. ***Development Code*** Structure Person Public Name as String End Structure Interface IData Public Property Employee As Person
12
2431
by: Sam Kong | last post by:
Hi, JavaScript hides its memory structure. I know that numbers, booleans, null and undefined are value types (value is directed saved in a variable). I want to know: - How JavaScript distinguishes value types from reference types by seeing the variable content?
4
1854
by: Alexander Adam | last post by:
Hello folks, I got a few question on some basic data structure stuff. Sorry if those questions might sound too easy or such but after googling a lot I didn't find a real answer to all those questions so appreciate any help. I need to keep a very efficient data-tree structure (parent, previous, next, first child, last child) within the memory which can be filled up with billions of records. A struct place into the tree (I am using
0
9617
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
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
10254
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...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
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...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
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.