473,387 Members | 1,476 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,387 software developers and data experts.

reference types in instantiated objects

DS
I've got a question about how I can go about nesting a reference type in
an object of an instantiated class alongside value types. This may seem
a bit vague so I'll give an example:

public class Cow
{
private string farm;
private int brandNumber;
public Cow()
{
farm = "Old MacDonald";
brandNumber = 0;
}
public string Farm
{
get { return farm; }
set { farm = value; }
}
public int BrandNumber
{
get { return brandNumber; }
set { brandNumber = value; }
}
}

then elsewhere...
Cow milk = new Cow();
milk.BrandNumber = 57;
Cow beef = milk;
//beef.BrandNumber = 95;
now here's what I want: I need them to both share the same Farm name so
that when the farm is changed for either it changes for all. This will
happen as this stands because "Cow beef = milk;" point to the same
place; unfortunately the brand number is not independent like this...

The trick is I need them to have independent BrandNumbers which means I
really need to declare it as "Cow Beef = new Cow();", but then I don't
have them sharing a reference to the same Farm. I cannot use a static
for the Farm because elsewhere I may have other Cows in another farm.

How can I do this in C#? Is there a way to change the farm value type to
a reference type?

Thanks a bunch in advance
-Dan
*apologies to the vegetarians/vegans, no cows were harmed in the
creation of this ;-)
Nov 17 '05 #1
4 1126
I suggest you create a class, e.g. CowFarm

public class CowFarm
{
private string name;
// omit constructors and properties
}

public class Cow
{
private CowFarm farm;

// omit constructors and properties
}

To make them share a farm:
cow1.Farm = cow2.Farm;

To change name that affect both cows:
cow1.Farm.Name = "Mac2";

Nov 17 '05 #2
you can use
public static string Farm;

Nov 17 '05 #3
<ch****@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
you can use
public static string Farm;


However, this would mean that *all* cows belong to the same farm rather than
being able to have different groups of cows at different farms

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Nov 17 '05 #4
conceptually, i prefer to do it in the following flavour IMO

public class Farm {
List<Cow> cows;
//...
}

public class Cow {
Farm farmOfTheCow;
public Cow(Farm farm) {
}

}

regards
Nov 17 '05 #5

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

Similar topics

3
by: klaas | last post by:
the following code gives rise to the beneath error message, only when a matrix object is instantiated as matrix<bool>, not with matrix<float>: /*returns a reference to the object at position...
4
by: grahamo | last post by:
Hi, I am trying to use STL copy routine to stream the my user defined type to std out. I am using a standard ostream_iterator object in conjunction with my type (called employee) which has ...
4
by: Keith Chadwick | last post by:
I am having some trouble referencing an Application("myVar") variable from within a module.vb file on my ASP.NET site. According to the documentation I should be able to reference...
3
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
8
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
0
by: David | last post by:
Hi, I'm working on a project where we're juggling with two potential implementations. In the two scenarios, we create objects in the base namespace. These objects are interdependent, in the sense...
22
by: Ruben | last post by:
Why would a method that is defined to return a reference such as with the operator overload of , operator, href& operator(int index){ return _array; } not cause a type mismatch compiler...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.