PDA

View Full Version : c# get and set & viewstate issue



Drenriza
December 4th, 2011, 01:02 PM
Hi all.

I am working on a ASP.NET site, where i have some trouble with some of my get and set methodes.

I currently have 3 classes, to seperate the different methodes and to give a good overview.

In my WebForm1.aspx.cs class i add my values to my get and set


foreach (String[] line in tableListTwo)
{
getAndSet.CellOne = line[0];
getAndSet.CellTwo = line[1];
getAndSet.CellThree = line[2];
getAndSet.CellFour = line[3];
getAndSet.CellFive = line[4];
getAndSet.CellSix = line[5];
getAndSet.CellSeven = line[6];
getAndSet.CellEight = line[7];
getAndSet.CellNine = line[8];
getAndSet.CellTeen = line[9];


And the values get added to the get and set, no trouble so far.


publicString _CellOne = "Removed";
publicString _CellTwo = "Removed";
publicString _CellThree = "Removed";
publicString _CellFour = "Removed";
publicString _CellFive = "Removed";
publicString _CellSix = "Removed";
publicString _CellSeven = "Removed";
publicString _CellEight = "Removed";
publicString _CellNine = "Removed";
publicString _CellTeen = "Removed";


The problem is when i call for the values. Then i don't get the value that i've added to the get and set. I get the static value "Removed"

For example i add the value 2, to _CellOne, but i retrive the value Removed.



cell1.Text = getAndSet.CellOne;
cell2.Text = getAndSet.CellTwo;
cell3.Text = getAndSet.CellThree;
cell4.Text = getAndSet.CellFour;
cell5.Text = getAndSet.CellFive;
cell6.Text = getAndSet.CellSix;
cell7.Text = getAndSet.CellSeven;
cell8.Text = getAndSet.CellEight;
cell9.Text = getAndSet.CellNine;
cell10.Text = getAndSet.CellTeen;


Is this because i need to use a viewstate to save the values in? For some reason they get lost when retrived them into a different class then the WebForm1.aspx.cs class.

Hope you understand what i mean. I am not a C# shark ,) im quite new at it.

If their is something that sounds fuzzy, please dont hesitate to ask and i will try to elaborate.

Thanks all on advance.
Kind regards.

Edit.
is their someone who can explain to me when to use a viewstate and when to use a get and set methode?

Edit 2.
Well i have read a little up on what a viewstate is. So my question is more focused on. When i from my page.aspx.cs file, add a value to a get&set methode in a C# class. And then call that same value to another C# class, why do i then "loose" the value. Resulting in getting the static value, when i defined the get&set methode. And how can i prevent "loosing" the value?

Drenriza
December 4th, 2011, 10:17 PM
No one? :( :)

Petrolea
December 4th, 2011, 10:32 PM
The problem might occur in two places:

1. When you put strings into cells with your foreach loop
2. When you try to retrieve them with get statement. You might return a wrong value and that's why you get that weird string over and over again.

Please tell me if the values of cells are correct right after the foreach part.

Drenriza
December 4th, 2011, 10:42 PM
Please tell me if the values of cells are correct right after the foreach part.

Right after the foreach loop (to where i place the value inside the get&set methode) the values in the get&set are correct. But when i leave the

Form1.aspx.cs class and try to retrive the values in another xxxx.cs i get the wrong value. Meaning i get the static value i give the String when i create it. And somehow the value inside the get&set methode vanishes.

Petrolea
December 4th, 2011, 11:10 PM
Right after the foreach loop (to where i place the value inside the get&set methode) the values in the get&set are correct. But when i leave the

Form1.aspx.cs class and try to retrive the values in another xxxx.cs i get the wrong value. Meaning i get the static value i give the String when i create it. And somehow the value inside the get&set methode vanishes.

Please show the code that you use within your get/set function.

Edit: And briefly describe what the values are that you get and return.

Drenriza
December 5th, 2011, 12:01 AM
example of my get and set methode


public String _CellOne = "Removed";

public String CellOne
{
get { return _CellOne; }
set { _CellOne = value; }
}


The values i add to for example CellOne is a series of numbers. Like 1, 2, 3, 451, 622, 1901 and so on and so forth. Where the values (in this case numbers) are read from a text file. But when i try and retrive them i get the static value of in this case "Removed". And that gets placed into the table.


public void TableCreationTwo()//creates user defined tabels
{
//Create the table
Table tb2 = new Table();
//Set table width
tb2.BorderWidth = 1;
//create tabel cells
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
TableCell cell3 = new TableCell();
TableCell cell4 = new TableCell();
TableCell cell5 = new TableCell();
TableCell cell6 = new TableCell();
TableCell cell7 = new TableCell();
TableCell cell8 = new TableCell();
TableCell cell9 = new TableCell();
TableCell cell10 = new TableCell();
//Add cells to the row
row.Cells.Add(cell1);
row.Cells.Add(cell2);
row.Cells.Add(cell3);
row.Cells.Add(cell4);
row.Cells.Add(cell5);
row.Cells.Add(cell6);
row.Cells.Add(cell7);
row.Cells.Add(cell8);
row.Cells.Add(cell9);
row.Cells.Add(cell10);
tb2.Rows.Add(row);
//Set cells border width
cell1.BorderWidth = 1;
cell1.Style["Text-Align"] = "center";
cell1.Width = Unit.Pixel(70);
cell1.BackColor = Color.PaleGoldenrod;
cell2.BorderWidth = 1;
cell2.Style["Text-Align"] = "center";
cell2.Width = Unit.Pixel(250);
cell2.BackColor = Color.PaleGoldenrod;
cell3.BorderWidth = 1;
cell3.Style["Text-Align"] = "center";
cell3.Width = Unit.Pixel(100);
cell3.BackColor = Color.PaleGoldenrod;
cell4.BorderWidth = 1;
cell4.Width = Unit.Pixel(80);
cell4.Style["Text-Align"] = "center";
cell4.BackColor = Color.PaleGoldenrod;
cell5.BorderWidth = 1;
cell5.Width = Unit.Pixel(90);
cell5.Style["Text-Align"] = "center";
cell5.BackColor = Color.PaleGoldenrod;
cell6.BorderWidth = 1;
cell6.Style["Text-Align"] = "center";
cell6.Width = Unit.Pixel(80);
cell6.BackColor = Color.PaleGoldenrod;
cell7.BorderWidth = 1;
cell7.Style["Text-Align"] = "center";
cell7.Width = Unit.Pixel(100);
cell7.BackColor = Color.PaleGoldenrod;
cell8.BorderWidth = 1;
cell8.Style["Text-Align"] = "center";
cell8.Width = Unit.Pixel(200);
cell8.BackColor = Color.PaleGoldenrod;
cell9.BorderWidth = 1;
cell9.Style["Text-Align"] = "center";
cell9.Width = Unit.Pixel(100);
cell9.BackColor = Color.PaleGoldenrod;
cell10.BorderWidth = 1;
cell10.Style["Text-Align"] = "center";
cell10.Width = Unit.Pixel(100);
cell10.BackColor = Color.PaleGoldenrod;
//Add static content to the cells
cell1.Text = getAndSet.CellOne;
cell2.Text = getAndSet.CellTwo;
cell3.Text = getAndSet.CellThree;
cell4.Text = getAndSet.CellFour;
cell5.Text = getAndSet.CellFive;
cell6.Text = getAndSet.CellSix;
cell7.Text = getAndSet.CellSeven;
cell8.Text = getAndSet.CellEight;
cell9.Text = getAndSet.CellNine;
cell10.Text = getAndSet.CellTeen;
//Add table to arraylist
lmc.addToTableList(tb2);
}


Where the Table then is added to the form.


public void TableCreationAction()
{
if (!File.Exists(getAndSet.PathOne))
{
using (FileStream fs = File.Create(getAndSet.PathOne)) { }
}
int Num = 1;
using (StreamReader sr = new StreamReader(getAndSet.PathOne))
{
while (sr.Peek() >= 0)
{
getAndSet.Lala = sr.ReadLine();
String[] lines = getAndSet.Lala.Split('#');
tableListTwo.Add(lines);
}
}
foreach (String[] line in tableListTwo)
{
getAndSet.CellOne = line[0];
getAndSet.CellTwo = line[1];
getAndSet.CellThree = line[2];
getAndSet.CellFour = line[3];
getAndSet.CellFive = line[4];
getAndSet.CellSix = line[5];
getAndSet.CellSeven = line[6];
getAndSet.CellEight = line[7];
getAndSet.CellNine = line[8];
getAndSet.CellTeen = line[9];

if (Num % 2 == 0)//modulus forklaring http://en.wikipedia.org/wiki/Modulo_operation
{
//TableCreationTwo();
tbu.TableCreationTwo();
}
else
{
TableCreationThree();
}
Num++;
}
Num = 0;
tableListTwo.Clear();
System.Collections.ArrayList something = lmc.returnTablelist();
foreach (System.Web.UI.WebControls.Table p in something)
{
form1.Controls.Add(p);
}
lmc.clearTableList();
}//tableCreationAction Ends


The get and set methode is placed in class GetAndSetMethodes.cs.
TableCreationTwo() is placed in class TableBuildUp.cs
TableCreationAction() is placed in WebForm1.aspx.cs

If i place the TableCreationTwo() methode in the WebForm1.aspx.cs class, i have no trouble getting the correct value from the get and set methode. But when i place TableCreationTwo() in another class, in this case TableBuildUp.cs i get the wrong value.

Hope i explained it well enough.

Petrolea
December 5th, 2011, 12:47 AM
You might also want to post your getset class and I will analyse it tomorrow.

Can't find any errors in these two classes.

Drenriza
December 5th, 2011, 11:05 AM
You might also want to post your getset class and I will analyse it tomorrow.

Can't find any errors in these two classes.

You want me to post my entire get/set class?

Petrolea
December 5th, 2011, 02:23 PM
You want me to post my entire get/set class?

Actually, you don't have to. I can't seem to think of why this error might occur. Try asking on some Windows forum, more people will probably be able to help you there since this is Windows related.