Find largest area using - c#

I need to find largest area with same numbers in 2D array. Example: {{2 2 3}{1 2 3}{1 2 1}}
And mark it in new 2D array with same number for same area.
I will need to color the table with 3 colors, so i use between 1-3 in table[,]
I did test, but it doesn't work properly. ID just stacks too much on the same area.
Second edit: updated the code
public class Matrix
{
public int Height { get; set; }
public int Witdh { get; set; }
public int[,] Table { get; set; }
public int[,] Area { get; set; }
public int ID { get; set; }
public int tempBiggestArea = 0;
public int biggestArea { get; set; }
public Matrix(int height, int witdh)
{
Table = GenerateTable(height, witdh);
Height = height;
Witdh = witdh;
ID = 2;
Area = new int[height,witdh];
}
public int[,] GenerateTable(int height, int witdh)
{
int[,] table = new int[height, witdh];
Random rnd = new Random();
for (int i = 0; i < table.GetLength(0); i++)
{
for (int j = 0; j < table.GetLength(1); j++)
{
table[i, j] = rnd.Next(3) + 1;
}
}
return table;
}
public void Find(int x, int y, int color)
{
if (y < 0)
y = Height - 1;
if (x < 0)
x = Witdh - 1;
if (IsChanged(x, y))
return;
if (Area[x, y] == 0)
{
if (color > 3)
color = 1;
if (Table[x, y] == color)
{
Area[x, y] = ID;
tempBiggestArea++;
Find(x, y - 1, color);
Find(x - 1, y, color);
ID++;
}
}
if (tempBiggestArea > biggestArea)
biggestArea = tempBiggestArea;
tempBiggestArea = 0;
Find(x, y, color + 1);
}
public bool IsChanged(int i, int j)
{
return Area[i, j] != 0;
}

Related

JsonConvert.DeserializeObject loading data types but not data values

My program gives the user the option to serialize a certain list List<Position>ListPosition that stores the data from the class Position. It does serialize pretty well and it sorta looks like the following
{
"PosZ": 0,
"PosX": 749,
"PosY": 208,
"Agent": {
"IsEdgeTree": false,
"ThisTreesCreator": null,
"Type": 0,
"Colour": "Green",
"Dimension": 25
}
},
{
"PosZ": 0,
"PosX": 724,
"PosY": 183,
"Agent": {
"IsEdgeTree": false,
"ThisTreesCreator": {
"IsEdgeTree": false,
"Position": {
"PosZ": 0,
"PosX": 749,
"PosY": 208
},
"ThisTreesCreator": null,
"Type": 0,
"Colour": "Green",
"Dimension": 25
},
"Type": 0,
"Colour": "Green",
"Dimension": 25
}
},
These are just a few examples.
The bit that is giving me problems is when I deserialize it.
When I inspect the bit of code that converts it JsonConvert.DeserializeObject<List<Position>>(JsonString);, everything reads fine but, when I equal it to the List I am trying to dump the data to AKA the same list the serialized data comes from but empty, the data doesn't load and I'm left with the correct structure but no values.
Values not showing up
To Deserialize, I call the following method from another Windows Forms:
public static void Deserialize()
{
if (Directory.Exists("Serialized"))
{
var FileName = "Serialized/PositionsSerialized_4.json";
var JsonString = File.ReadAllText(FileName);
ListPositions = JsonConvert.DeserializeObject<List<Position>>(JsonString);
}
else
MessageBox.Show("There's no directory");
}
I would really appreciate some help. If more information is needed, be sure to request it in the comments.
Thank you for your time.
public class Position
{
public static int MaxX { get; private set; }
public static int MaxY { get; private set; }
public static List<Position> ListPositions { get; private set; }
public static List<Position> ListTreePositions {
get
{
return ListPositions.Where(w => w.Agent?.Type == AgentType.Tree).ToList();
}
}
public static List<Position> ListDronePositions
{
get
{
return ListPositions.Where(w => w.Agent?.Type == AgentType.Drone).ToList();
}
}
public int PosX { get; private set; }
public int PosY { get; private set; }
public int PosZ;
private static object services;
public Agent Agent { get; private set; }
public Position()
{
ListPositions = new List<Position>();
}
public static void SetMaxValues(int maxX, int maxY)
{
MaxX = maxX;
MaxY = maxY;
}
public void FillNeighbours(int posX, int posY, int dim)
{
for (int i = 0; i < dim; i++)
{
for (int y = 0; y < dim; y++)
{
new Position(posX - (dim - y), posY - (dim - i), Agent);
}
}
}
public static Position CreateNewRandomPosition()
{
Random rnd = new Random();
int rndX;
int rndY;
Position position = null;
while (position == null)
{
rndX = rnd.Next(MaxX - 100);
rndY = rnd.Next(MaxY - 100);
position = CreatePosition(rndX, rndY);
}
return position;
}
public static Bitmap ExportBitmap()
{
return ExportBitmap(0, 0, MaxX, MaxY, ListPositions);
}
public static Bitmap ExportBitmap(int posX, int posY, int maxX, int maxY)
{
var listPositions = ListPositions.Where(w => (w.Agent != null) && (w.PosX >= posX && w.PosX <= maxX) && (w.PosY >= posY && w.PosY <= maxY)).ToList();
return ExportBitmap(posX, posY, maxX, maxY, listPositions);
}
public static Bitmap ExportBitmap(int posX, int posY, int maxX, int maxY, AgentType agentType)
{
var listPositions = ListPositions.Where(w => (w.Agent.Type == agentType) && (w.PosX >= posX && w.PosX <= maxX) && (w.PosY >= posY && w.PosY <= maxY)).ToList();
return ExportBitmap(posX, posY, maxX, maxY, listPositions);
}
public static Bitmap ExportBitmap(int posX, int posY, int maxX, int maxY, AgentType[] agentType)
{
var listPositions = ListPositions.Where(w => (agentType.Contains(w.Agent.Type)) && (w.PosX >= posX && w.PosX <= maxX) && (w.PosY >= posY && w.PosY <= maxY)).ToList();
return ExportBitmap(posX, posY, maxX, maxY, listPositions);
}
private static Bitmap ExportBitmap(int posX, int posY, int maxX, int maxY, List<Position> positions)
{
Bitmap bmp = new Bitmap(maxX - posX, maxY - posY);
if (Agent.Sprites.Count() == 0)
{
Agent.LoadSprites();
}
Graphics g = Graphics.FromImage(bmp);
foreach (var item in positions)
{
var SpriteWidth = Agent.Sprites[item.Agent.Type].Width / 2;
var SpriteHeight = Agent.Sprites[item.Agent.Type].Height / 2;
g.DrawImage(Agent.Sprites[item.Agent.Type], new Point(item.PosX - SpriteWidth - posX, item.PosY - SpriteHeight - posY));
}
if (!Directory.Exists("Photos"))
{
Directory.CreateDirectory("Photos");
}
int count = Directory.GetFiles("Photos", "*").Length;
bmp.Save("Photos\\Img" + count + 1 + ".png", System.Drawing.Imaging.ImageFormat.Png);
return bmp;
}
public Position(int poxX, int poxY)
{
PosX = poxX;
PosY = poxY;
if (Agent != null)
ListPositions.Add(this);
}
public Position(int posX, int posY, Agent agent)
{
PosX = posX;
PosY = posY;
Agent = agent;
if (Agent != null)
ListPositions.Add(this);
}
public void AssociateAgent(Agent agent)
{
Agent = agent;
if (Agent != null)
ListPositions.Add(this);
}
public static bool CheckPositionIsValid(int poxX, int poxY)
{
int? NullableMaxX = MaxX;
int? NullableMaxY = MaxY;
var posXValid = poxX > 0 && (poxX <= MaxX || NullableMaxX == null); //verifies if posX is within the boundries of the premises
var posYValid = poxY > 0 && (poxY <= MaxY || NullableMaxY == null); //verifies if posY is within the boundries of the premises
return posXValid && posYValid; //returns if they are both valid
}
public static Position Find(int poxX, int poxY)
{
return ListPositions.Find(f => f.PosX == poxX && f.PosY == poxY) ?? null;
}
public static Position CreatePosition(int poxX, int poxY)
{
if (Find(poxX, poxY) == null)
if (CheckPositionIsValid(poxX, poxY))
return new Position(poxX, poxY);
else
return null;
else
return null;
}
public Dictionary<int, Position> ListNeighbourPositions(int dim = 1)
{
Dictionary<int, Position> NeighbourList = new Dictionary<int, Position>();
for (int i = 1; i < 9; i++)
{
if (i != 5)
{
var position = FindNeighbour(i, dim);
if (position != null)
NeighbourList.Add(i, position);
}
}
return NeighbourList;
}
public Position FindNeighbour(int neighbourPos, int dim)
{
var position = FindNeighbourbyPosition(neighbourPos, dim);
return Find(position.Item1, position.Item2);
}
public Tuple<int, int> FindNeighbourbyPosition(int neighbourPos, int dim = 1)
{
int neighbourPosX;
int neighbourPosY;
switch (neighbourPos)
{
case 1:
neighbourPosX = PosX - dim;
neighbourPosY = PosY - dim;
break;
case 2:
neighbourPosX = PosX;
neighbourPosY = PosY - dim;
break;
case 3:
neighbourPosX = PosX + dim;
neighbourPosY = PosY - dim;
break;
case 4:
neighbourPosX = PosX - dim;
neighbourPosY = PosY;
break;
case 6:
neighbourPosX = PosX + dim;
neighbourPosY = PosY;
break;
case 7:
neighbourPosX = PosX - dim;
neighbourPosY = PosY + dim;
break;
case 8:
neighbourPosX = PosX;
neighbourPosY = PosY + dim;
break;
case 9:
neighbourPosX = PosX + dim;
neighbourPosY = PosY + dim;
break;
default:
return null;
}
return new Tuple<int, int>(neighbourPosX, neighbourPosY);
}
public void UpdatePosition(int newX, int newY)
{
PosX = newX;
PosY = newY;
}
public static void Serialize()
{
if (!Directory.Exists("Serialized"))
{
Directory.CreateDirectory("Serialized");
}
int count = Directory.GetFiles("Serialized", "*").Length;
string fileName = "Serialized/PositionsSerialized_" + count + ".json";
string jsonString = JsonConvert.SerializeObject(ListPositions, Formatting.Indented, new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
File.WriteAllText(fileName, jsonString);
}
public static void Deserialize()
{
if (Directory.Exists("Serialized"))
{
var FileName = "Serialized/PositionsSerialized_4.json";
var JsonString = File.ReadAllText(FileName);
ListPositions = JsonConvert.DeserializeObject<List<Position>>(JsonString);
}
else
MessageBox.Show("There's no directory");
}
}
The reason of your problem is because you defined setters as private...
public static int MaxX { get; private set; }
public static int MaxY { get; private set; }
public int PosX { get; private set; }
public int PosY { get; private set; }
it should be...
public static int MaxX { get; set; }
public static int MaxY { get; set; }
public int PosX { get; set; }
public int PosY { get; set; }

Reading an array from an XML file

At the moment iI got this:
class robot
{
Configuratie config = new Configuratie();
short[,] AlleCoordinaten = new short[3, 6]
{
{1,2,3,4,5,6},
{6,5,4,3,2,1},
{2,3,4,5,6,7}
};
}
But I want to put that array in a XML-file, so this is what I tried:
class robot
{
private static XDocument xdoc = XDocument.Load("configuratie.xml");
public Robot()
{
short[,] AlleCoordinaten = new short[3, 6];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 6; j++)
{
AlleCoordinaten[i, j] = GetPositionValue("position" + (i + 1), j);
}
}
}
public static short GetPositionValue(string position,int index)
{
return (short)xdoc.Descendants(position).Skip(index).First();
}
private void methode2()
{
GoTo[0] = new Position();
for (short a=0 ; a<10 ; a++)
{
GoTo[0].degrees[0] = AlleCoordinaten[a,0];
GoTo[0].degrees[1] = AlleCoordinaten[a,1];
GoTo[0].degrees[2] = AlleCoordinaten[a,2];
GoTo[0].degrees[3] = AlleCoordinaten[a,3];
GoTo[0].degrees[4] = AlleCoordinaten[a,4];
GoTo[0].degrees[5] = AlleCoordinaten[a,5];
//here it tells me The name 'AlleCoordinaten' does not exist in the currect context
}
}
}
configuration file:
class Configuratie
{
private XDocument xdoc;
public Configuratie()
{
xdoc = XDocument.Load("configuratie.xml");
}
public int GetIntConfig(string desc1, string desc2)
{
int value = 0;
if (string.IsNullOrEmpty(desc1))
{
value = 0;
}
if (!string.IsNullOrEmpty(desc1) && !string.IsNullOrEmpty(desc2))
{
foreach (XElement node in xdoc.Descendants(desc1).Descendants(desc2))
{
value = Convert.ToInt16(node.Value);
}
}
if (!string.IsNullOrEmpty(desc1) && string.IsNullOrEmpty(desc2))
{
foreach (XElement node in xdoc.Descendants(desc1))
{
value = Convert.ToInt16(node.Value);
}
}
return value;
}
}
XML file:
<robot>
<position1>1</position1>
<position1>2</position1>
<position1>3</position1>
<position1>4</position1>
<position1>5</position1>
<position1>6</position1>
etc...
<position3>7</position3>
</robot>
It still isnt working, could you guys help me with what I did wrong and maybe give an example.
Using XmlSerializer would simplify things by a lot:
size array automatically;
serialization/deserialization is just few rows of code.
Like this
public class Coordinate
{
public int X1 {get; set;}
public int Y1 {get; set;}
public int Z1 {get; set;}
public int X2 {get; set;}
public int Y2 {get; set;}
public int Z2 {get; set;}
public Coordinate(int x1, int y1, int z1, int x2, int y2, int z2)
{
X1 = x1; Y1 = y1; Z1 = z1; X2 = x2; Y2 = y2; Z2 = z2;
}
}
[XmlArray("Robot")]
public Coordinate[] points = new Coordinate[] {
new Coordinate(1, 2, 3, 4, 5, 6),
new Coordinate(6, 5, 4, 3, 2, 1),
new Coordinate(2, 3, 4 ,5, 6, 7),
}
// serialize (remove long namespace)
var space = new XmlSerializerNamespaces();
space.Add("", "");
var serializer = new XmlSerializer(points.GetType()); // typeof(Coordinate[])
using (var stream = new FileStream("robot.xml", FileMode.Create))
serializer.Serialize(stream, points, space);
// deserialize
using (var stream = new FileStream("robot.xml", FileMode.Open, FileAccess.Read))
points = (Coordinate[])serializer.Deserialize(stream);
Try this method:
public static short GetPositionValue(string position,int index)
{
return (short)xdoc.Descendants(position).Skip(index).First();
}
And populate your array with for loop:
Here is the full code:
class robot
{
private static XDocument xdoc = XDocument.Load("configuratie.xml");
short[,] AlleCoordinaten = new short[3, 6];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 6; j++)
{
AlleCoordinaten[i, j] = GetPositionValue("position" + (i+1), j);
}
}
public static short GetPositionValue(string position,int index)
{
return (short)xdoc.Descendants(position).Skip(index).First();
}
}
Note: CHANGE your xdoc definition:
private XDocument xdoc;
To:
private static XDocument xdoc;

Problems with a tile worldscreen system?

Im trying to make a tile based world screen for my game , here are the three classes i have
namespace WindowsGame1
{
public enum Tiletype
{Grass,
Water,
Foothill,
Mountain
}
public struct Tile
{
public Tiletype TerrainType { get; set; }
public Texture2D TileGFX { get; set; }
public Rectangle SrcRect { get; set; }
public Vector2 Location { get; set; }
public int AniFrame { get; set; }
// TILE ACTIONS
public bool IsActivated { get; set; }
public bool IsBlocked { get; set; }
public bool IsTouchTrigger { get; set; }
public bool IsStepTrigger { get; set; }
}
}
MapBase class
public class MapBase
{
public Tile[,] TileList = new Tile[1, 1];
public MapBase(int width, int height, Vector2 start)
{
TileList = new Tile[width + 1, height + 1];
// TEMPORARY MAP
for (int X = 0; X <= width; X++)
{
for (int Y = 0; Y <= height; Y++)
{
TileList[X, Y] = new Tile();
var _with1 = TileList[X, Y];
_with1.TerrainType = Tiletype.Water;
_with1.TileGFX = Textures.World;
_with1.AniFrame = 0;
_with1.IsBlocked = true;
}
}
// SIMPLE ISLAND
for (int z = 22; z <= 33; z++)
{
for (int c = 22; c <= 31; c++)
{
TileList[z, c].TerrainType = Tiletype.Grass;
}
}
TileList[27, 25].TerrainType = Tiletype.Foothill;
TileList[28, 25].TerrainType = Tiletype.Foothill;
TileList[27, 24].TerrainType = Tiletype.Foothill;
TileList[28, 26].TerrainType = Tiletype.Mountain;
for (int x = 0; x <= width; x++)
{
for (int y = 0; y <= height; y++)
{
TileList[x, y].SrcRect = GetTileSource(TileList[x, y].TerrainType);
}
}
}
GetTitleSource function
private Rectangle GetTileSource(Tiletype TType)
{
Rectangle sRect = default(Rectangle);
switch (TType)
{
case Tiletype.Grass:
sRect = new Rectangle(0, 0, 16, 16);
break;
case Tiletype.Water:
sRect = new Rectangle(0, 80, 16, 16);
break;
case Tiletype.Foothill:
sRect = new Rectangle(48, 0, 16, 16);
break;
case Tiletype.Mountain:
sRect = new Rectangle(32, 0, 16, 16);
break;
}
return sRect;
}
}
Class WorldScreen
class Worldscreen : BaseScreen
{
public int MapWidth = 100;
public int MapHeight = 100;
public int TileSize = 32;
public int MapX = 20;
public int MapY = 19;
public MapBase Map = new MapBase(100, 100, new Vector2(0, 0));
public Worldscreen(GraphicsDevice device)
: base(device, "WorldScreen")
{
Map = new MapBase(MapWidth, MapHeight, new Vector2(0, 0));
}
Draw function
public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
Globals.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone);
//DRAW TILE LAYER
for (int DrawX = -1; DrawX <= 16; DrawX++)
{
for (int DrawY = -1; DrawY <= 15; DrawY++)
{
int x = DrawX + MapX;
int y = DrawY + MapY;
if (x > 0 & x <= MapWidth & y >= 0 & y <= MapHeight)
{
Globals.spriteBatch.Draw(Map.TileList[x,y].TileGFX, new Rectangle(DrawX * TileSize, DrawY * TileSize, TileSize, TileSize), Map.TileList[x, y].SrcRect, Color.White);
// VIEW COORDINATES ON TILE
//Globals.SpriteBatch.DrawString(Fonts.Arial_8, "X:" & x & vbCrLf & "Y:" & y, New Vector2(DrawX * TileSize, DrawY * TileSize), Color.Black)
}
}
}
Globals.spriteBatch.End();
}
}
When I try to run the draw void it comes up with this error on the world screen class
This method does not accept null for this parameter.
Parameter name: texture
is there any way to solve this or do i have to rework these classes.
you got all the info you need to debug this yourself :
theres a method on the world screen class who have a texture = null.
Go step-by-step debug and find out which one, unless its a simple error we can find looking at your code, but its often not that easy. ( ex : do you have a list which isnt instanciate ? )
but since its a method paramaters ... the only method that accept a texture as a parameter is GetTileSource.
So quickly what could explain it ... I'd check 2 things :
A- are you sure your loop have correct bound ? You might simply be asking for a tile in that array that doesn't exist since its out of bound. (array are 0 base, and you loop up to <= height instead of < height)
B- 2nd thing i'd check would be (since this is a texture ) that you didn't load one of those texture you use ...

Need help in data Binding in datagridview

I am new to c# and absolutely new to windows form application. I am Implementing a Sudoku
Solver I coded in C++ since i need a GUI. Using a DataGridView and need help with data binding without databases. Posting my code, please help. Need idea about
1. How to enable user enter data in datagridview and "inital" array.
2. How to read that data and copy in "copied" array.
3. How to Bind data to the DataGridView.
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
bindGrid();
}
private void bindGrid()
{
List<TestCode> list = new List<TestCode>();
TestCode tt = new TestCode();
list.Add(tt);
list.Add(tt);
list.Add(tt);
list.Add(tt);
list.Add(tt);
list.Add(tt);
list.Add(tt);
list.Add(tt);
list.Add(tt);
this.dataGridView1.DataSource = list;
}
public int i;
public int[,] initial;
public int[,] copied;
int inputvalue(int x, int y, int value)
{
initial = new int[9, 9];
copied = new int[9, 9];
for (int i = 0; i < 9; i++)
{
if (value == copied[x, i] || value == copied[i, y])
return 0;
}
for (i = (x / 3) * 3; i <= ((x / 3) * 3) + 2; i++)
for (int j = (y / 3) * 3; j <= ((y / 3) * 3) + 2; j++)
if (copied[i, j] == value)
return 0;
return value;
}
bool solve(int x, int y)
{
int i;
int temp;
if (copied[x, y] == 0)
{
for (i = 1; i < 10; i++)
{
temp = inputvalue(x, y, i);
if (temp > 0)
{
copied[x, y] = temp;
if (x == 8 && y == 8)
return true;
else if (x == 8)
{
if (solve(0, y + 1))
return true;
}
else
{
if (solve(x + 1, y))
return true;
}
}
}
if (i == 10)
{
if (copied[x, y] != initial[x, y])
copied[x, y] = 0;
return false;
}
}
if (x == 8 && y == 8)
return true;
else if (x == 8)
{
if (solve(0, y + 1))
return true;
}
else
{
if (solve(x + 1, y))
return true;
}
return true;
}
private void fillDatagrid()
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//this.dataGridView1.Rows.Add(new object[] { "Column1", "Column2", "Column3", "Column4","Column5","Column5","Column6","Column7","Column8","Column9", true });
//this.dataGridView1.DataSource = copied;
}
private void button1_Click(object sender, EventArgs e)
{
int i, j;
Form1 P = new Form1();
for (i = 0; i < 9; i++)
for (j = 0; j < 9; j++)
{
//dataGridView1[i, j];
// Console.SetCursorPosition(i + 1, j + 1);
//P.initial[i,j] = Console.ReadLine();
}
for (i = 0; i < 9; i++)
for (j = 0; j < 9; j++)
P.copied = P.initial;
if (P.solve(0, 0))
{
for (i = 0; i < 9; i++)
for (j = 0; j < 9; j++)
{
Console.SetCursorPosition(i + 1, j + 1);
Console.Write(" ", +P.copied[i, j]);
}
}
else
Console.Write(" NO Solution");
}
}
public class TestCode
{
public int Value1 { get; set; }
public int Value2 { get; set; }
public int Value3 { get; set; }
public int Value4 { get; set; }
public int Value5 { get; set; }
public int Value6 { get; set; }
public int Value7 { get; set; }
public int Value8 { get; set; }
public int Value9 { get; set; }
}
}
private void bindGrid()
{
List <TestCode> list = new List<TestCode>();
TestCode tt = new TestCode();
list.Add(tt);
list.Add(tt);
list.Add(tt);
list.Add(tt);
list.Add(tt);
list.Add(tt);
list.Add(tt);
list.Add(tt);
list.Add(tt);
this.dataGridView1.DataSource = list;
this.dataGridView1.DataBind();
}

how to use variables of a class from other classes in c#

i need to call variables that are defined in a class
class testclasss
{
public testclass(double[,] values)
{
double[][] rawData = new double[10][];
for (int i = 0; i < 10; i++)
{
rawData[i] = new double[] { values[i, 2], stockvalues[i, 0] }; // if data won't fit into memory, stream through external storage
}
int num = 3;
int max = 30;
}
public int[] checkvalue(double[][] rawData, int num, int maxCount)
{
............
}
}
I have called constructor using
testclass newk = new testclass(values);
now how can i call the function checkvalues(). i tried using newk.num,newk.maxcount but those variables were not recognized.
Just as your class constructor and checkvalues function are public, so too must the properties you wish to access from outside the class. Put these at the top of your class declaration:
public int num {get; set;}
public int max {get; set;}
Then you can access them via newk.num and newk.max.
EDIT: In response to your second comment, I think you might be a bit confused about how functions and properties interact within the same class. This might help:
class TestClass {
private int _num;
private int _max;
private double[][] _rawData;
public TestClass(double[,] values, int num, int max)
{
_num = num;
_max = max;
_rawData = new double[10][];
for (int i = 0; i < 10; i++)
{
_rawData[i] = new double[] { values[i, 2], stockvalues[i, 0] };
}
}
public int[] CheckValues()
{
//Because _num _max and _rawData exist in TestClass, CheckValues() can access them.
//There's no need to pass them into the function - it already knows about them.
//Do some calculations on _num, _max, _rawData.
return Something;
}
}
Then, do this (for example, I don't know what numbers you're actually using):
double[,] values = new double[10,10];
int num = 3;
int max = 30;
Testclass foo = new Testclass(values, num, max);
int[] results = foo.CheckValues();
I believe this is what you're looking for:
class TestClass {
public int num { get; set; }
public int max { get; set; }
public double[][] rawData;
public TestClass(double[,] values)
{
rawData = new double[10][];
for (int i = 0; i < 10; i++)
{
rawData[i] = new double[] { values[i, 2], stockvalues[i, 0] }; // if data won't fit into memory, stream through external storage
}
this.num = 3;
this.max = 30;
}
public int[] CheckValue()
{............}
}
You'll need a property with a get accessor.
Here is a simplified example based on your code:
class testclass
{
private int _num = 0;
private int _max = 0;
public int Num
{
set { _num = value; }
get { return _num; }
}
public int Max
{
set { _max = value; }
get { return _max; }
}
public testclass()
{
Num = 3;
Max = 30;
}
}
//To access Num or Max from a different class:
testclass test = new testclass(null);
Console.WriteLine(test.Num);
Hope that helps.
Using your exact same code, except for public testclass(double[,] values), which I changed to public testfunction(double[,] values)
class testclass
{
public testfunction(double[,] values)
{
double[][] rawData = new double[10][];
for (int i = 0; i < 10; i++)
{
rawData[i] = new double[] { values[i, 2], stockvalues[i, 0] }; // if data won't fit into memory, stream through external storage
}
int num = 3;
int max = 30;
}
public int[] checkvalue(double[][] rawData, int num, int maxCount)
{
............
}
}
Have you tried calling checkvalues() function like this?
testclass.checkvalue();

Categories