Sign in to follow this  
Steve Relf

A simple problem...

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.

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

GenerateSector.cs
[CODE]
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;

}
}
}
[/CODE]

When i run the above code, and step through
[CODE]
string SectorName = _SectorName;
int MaxAlliedFleets = _MaxAlliedFleets;
int MaxPirateFleets = _MaxPirateFleets;
int MaxCivFleets = _MaxCivFleets;
int SectorSizeWidth = _SectorSizeWidth;
int SectorSizeHeight = _SectorSizeHeight;
[/CODE]
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 this post


Link to post
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]

[i]Above is incorrect, see laztrezort's answer.[/i] Edited by Bacterius

Share this post


Link to post
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:

[CODE]
this.SectorName = _SectorName;
this.MaxAlliedFleets = _MaxAlliedFleets;
...
[/CODE]

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 this post


Link to post
Share on other sites
[quote name='Bacterius' timestamp='1335753979' post='4935973']
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.
[/quote]

This ain't right, the other answer is.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this