Constructor using out parameters

  • Thread starter Thread starter Vanessa
  • Start date Start date
V

Vanessa

How could I change the code below for the constructor to make use out int X
and out int Y parameters?
Could someone please show me?

using System;
namespace PRL001
{
public class Locate
{
public int loc,location,x,y;
private int [, ] mat= new int [6,20];

public Locate(int Loc)
{
loc=Loc;
createMat();
}

private void createMat()
{
for(int row = 0;row<6;row++)
{
for (int col = 0; col<20;col++)
{
mat[row,col]=location++;
}
}
}

public int getX( )
{
for(int row = 0;row<6;row++)
{
for (int col = 0; col<20;col++)
{
if(mat[row,col]==loc)
{
x=row;
}
}
}
return x;
}

public int getY( )
{
for(int row = 0;row<6;row++)
{
for (int col = 0; col<20;col++)
{
if(mat[row,col]==loc)
{
y=col;
}
}
}
return y;
}
}
}
 
Vanessa,

You could just declare your constructor like so:

public Locate(int Loc, out int x, out int y)

Then, in the code, you would just set x = getX and y = getY. Remember
to use the "out" keyword when making the call as well.

Hope this helps.
 
Vanessa said:
How could I change the code below for the constructor to make use out int X
and out int Y parameters?
Could someone please show me?

If you mean so that you could pass in variables and the variables in
Locate would *stay* aliased with variables you passed in, you can't.
The aliasing finishes at the end of the method - it's only the formal
parameter which is aliased, not anything which is assigned its value.
 
I tried to do exactly this, but I couldn't get it to work.
Could you please show a little more code?
Or a simple example?
 
This is what I want to do:

CLASS(int LOCATION, out int X, out int Y)

CLASS has a MATRIX in which LOCATIONs are the sequence numbers
of the CELLs[X,Y]

CLASS has GETX and GETY, returning X and Y values respectively.

I want to pass LOCATION to a CLASS and receive values for X and Y back
in the calling function of the user class.

Could you please tell me what it is, that I am trying to do, that is
incorrect?
And would you please tell me how to do this? I can't get it to work.
(I can if I don't use out parameters.)
 
Vanessa said:
This is what I want to do:

CLASS(int LOCATION, out int X, out int Y)

CLASS has a MATRIX in which LOCATIONs are the sequence numbers
of the CELLs[X,Y]

CLASS has GETX and GETY, returning X and Y values respectively.

I want to pass LOCATION to a CLASS and receive values for X and Y back
in the calling function of the user class.

Yes, you can do that.
Could you please tell me what it is, that I am trying to do, that is
incorrect?

Nope, that's fine. I thought you were trying to make the relationship
between the variables you pass in and X/Y after the constructor had
finished.
And would you please tell me how to do this? I can't get it to work.
(I can if I don't use out parameters.)

Well, broadly speaking it's:

public Foo (int location, out int x, out int y)
{
// Do stuff with location
// ...

// Then at the end of the method, set x and y
x = GetX(); // Or something similar
y = GetY();
}
 
John, it doesn't work :(
It passes -1 back to the calling class.


using System;

namespace PRL001
{
public class Locate2
{
public int loc;
private int [, ] mat= new int [6,20];

public Locate2(int Loc, out int x, out int y)
{
loc=Loc;
x=getX();
y=getY();
createMat();
}

private void createMat()
{
int location = 0;
for(int nam = 0;nam<6;nam++)
{
for (int dim = 0; dim<20;dim++)
{
mat[nam,dim]=location++;
}
}
}

public int getX()
{
for(int nam = 0;nam<6;nam++)
{
for (int dim = 0; dim<20;dim++)
{
if(mat[nam,dim]==loc) return nam;
}
}
return -1;
}

public int getY()
{
for(int nam = 0;nam<6;nam++)
{
for (int dim = 0; dim<20;dim++)
{
if(mat[nam,dim]==loc) return dim;
}
}
return -1;
}
}
}
 
Vanessa said:
John, it doesn't work :(
It passes -1 back to the calling class.

That's because you're calling getX() and getY() before you're calling
createMat().
 
Back
Top