Public Group

A simple problem...

This topic is 2300 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi all,

Im fairly new to C#, having spent most of my time working with Perl.

Im having an issue and i hope somone can shed some light.

I have the below block of code which should make CurrentSector be filled with the variables i Pass in. But, for some reason, this is not happening, and i cant work out why.

 CurrentSector = new GenerateSector(startsector.SectorName, startsector.MaxAlliedFleets, startsector.MaxPirateFleets, startsector.MaxCivFleets, startsector.SectorSizeWidth, startsector.SectorSizeHeight); 

GenerateSector.cs
 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FleetCommand { public class GenerateSector { public string SectorName = null; public int MaxAlliedFleets = 0; public int MaxPirateFleets = 0; public int MaxCivFleets = 0; public int SectorSizeWidth = 0; public int SectorSizeHeight = 0; public GenerateSector(string _SectorName, int _MaxAlliedFleets, int _MaxPirateFleets, int _MaxCivFleets, int _SectorSizeWidth, int _SectorSizeHeight) { string SectorName = _SectorName; int MaxAlliedFleets = _MaxAlliedFleets; int MaxPirateFleets = _MaxPirateFleets; int MaxCivFleets = _MaxCivFleets; int SectorSizeWidth = _SectorSizeWidth; int SectorSizeHeight = _SectorSizeHeight; } } } 

When i run the above code, and step through
 string SectorName = _SectorName; int MaxAlliedFleets = _MaxAlliedFleets; int MaxPirateFleets = _MaxPirateFleets; int MaxCivFleets = _MaxCivFleets; int SectorSizeWidth = _SectorSizeWidth; int SectorSizeHeight = _SectorSizeHeight; 
The above veriables are set, but when i get out of the routine, the veriable CurrentSector, shows null, and zero's.

Any ideas, I bet its something stupid, but i have now ben staring at it for so long, i cant think about it any more...

Do i need a get and set...?

Share on other sites
[s]You are passing the parameters by value and not by reference, which means that a deep copy of the variables is done each time you call the function (and they are discarded when the function returns). What you want is to pass your parameters by reference, I'm not sure what the keyword is but it should be something along the lines of adding "ref", "var", or "out" (not sure which one) before each parameter.[/s]

Above is incorrect, see laztrezort's answer. Edited by Bacterius

Share on other sites
In your constructor, you are defining new variables for SectorName, MaxAlliedFleets, etc. They happen to have the same name as your class fields - but since they are explicitly defined in the current scope, they hide your fields. They are local variables, different from your fields, and once the constructor is finished, they are destroyed. You are never setting your class fields as you intended.

You should do something like this instead:

 this.SectorName = _SectorName; this.MaxAlliedFleets = _MaxAlliedFleets; ... 

Note that "this." is optional in this case, but it clarifies what your intents.

I would think you would have gotten some sort of compiler warning about the fields not being used, but I'm not sure about that.

Share on other sites

You are passing the parameters by value and not by reference, which means that a deep copy of the variables is done each time you call the function (and they are discarded when the function returns). What you want is to pass your parameters by reference, I'm not sure what the keyword is but it should be something along the lines of adding "ref", "var", or "out" (not sure which one) before each parameter.

This ain't right, the other answer is.

Share on other sites
Thanks for the imput guys, laztrezort you are correct

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 18
• Forum Statistics

• Total Topics
632160
• Total Posts
3004497

×