|
Even if you wrap something in an unsafe scope, or mark a method as unsafe, .NET will still perform bounds checks on pointers. If you need to read an arbitrary location in memory, you would likely need to use an unchecked {} scope as well.
|
|
|
|
|
.NET never performs bound checks on pointers. That's why they're unsafe .
unchecked {} only affects integer overflow checking, it is unrelated to bounds checking.
|
|
|
|
|
Thats not entirely true. If you create a pointer to an object, and try to advance the pointer beyond the range of memory that object consumes on the heap, C# throws an exception. Your free to move the pointer to any location that on the heap that is part of the object you originally created the pointer from, but nothing ouside of it. Only when you wrap your ponter logic in an unchecked {} block are you able to access memory beyond the bounds of whatever it was you originally set the pointer to.
|
|
|
|
|
Do you have any references for that?
In my understanding, "int* IncrementPointer(int *p) { return p++; }" is a simple increment of the pointer, no runtime checks. It doesn't matter whether p points to unmanaged memory or a pinned object on the GC heap.
.NET doesn't even try to remember the 'valid range' - it's just a simple pointer!
Looking for 'unchecked' in the C# specification, it only talks about integer overflows. No mention of pointers there.
However, when you dereference an invalid pointer, you might get a nice managed exception. But that's just because .NET converts Win32 access violations to exceptions (to NullReferenceException if the pointer was near null, AccessViolationException otherweise). It is still possible to read invalid data/crash the process/whatever - they're unsafe pointers!
|
|
|
|
|
I ran into the problem a while back on a project that was using pointers to manipulate a Bitmap object. I had a bug in my math tha was causing me to read past the end of the bitmap, and I was getting an AccessViolationException. I'll see if I can dig the code up. Interestingly enough, I just tried to proove the theory by writing a simple loop that passed over an array with a pointer, and when reading past the end, I recieved neither a NullReferenceException nor an AccessViolationException. Given that, perhapse the exception was something specific to the Bitmap class in .NET...
|
|
|
|
|
To use pointers in C# you must use unsafe code either through the compiler option /unsafe or through an unsafe block. After that the code is the same in C++ and C#. My article on Grayscale conversion uses pointers.
http://www.codeproject.com/KB/graphics/quickgrayscale.aspx[^]
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|
Ennis Ray Lynch, Jr. wrote: either through the compiler option /unsafe or through an unsafe block
Don't you need both?
|
|
|
|
|
Yes.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Anybody help me
i am new c# developing. i want dynamic drag window component for example the label or button will move after execute the project .
And one more thing on that time want call back method, if the label drag on the another label of same form
Thankyou
|
|
|
|
|
Replace Panel with Label1 in the following code.
private bool _mouseDown;
private Point _mousePos;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_mouseDown = true;
_mousePos = new Point(e.X, e.Y);
}
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (_mouseDown)
{
panel1.Location = PointToClient(this._panel1.PointToScreen(new Point(e.X - _mousePos.X, e.Y - _mousePos.Y)));
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (_mouseDown)
{
_mouseDown = false;
}
}
|
|
|
|
|
Hi
i have created dynamically 2 text boxes in a row using a loop based on the user selection from dropdownlist box .i.e if user selects 2 in dropdownlistbox then there will be 2 rows of that text boxes will be generated dynamically...
My problem is i want to read the 2 text box values in a row and insert the values in to the database (sqlserver 2005) table,in the same way for all the geneated rows ??? can some body help me in solving my problem..
i dont knw how to read the values and inset into the table..pls pls some body give some idea or steps to follow...waiting for the answer eagerly.. i'm attaching the code fro reference:
protected void Page_Load(object sender, EventArgs e)
{
int i=0;
if (!Page.IsPostBack)
for (i = 0; i < 1000; i++)
{
DropDownList1.Items.Add(i.ToString());
}
if (Page.IsPostBack)
{
this.Rows = DropDownList1.SelectedIndex;
}
CreateDynamicTable();
}
protected int Rows
{
get
{
return ViewState["Rows"] != null ? (int)ViewState["Rows"] : 0;
}
set
{
ViewState["Rows"] = value;
}
}
protected int Columns
{
get
{
return ViewState["Columns"] != null ? (int)ViewState["Columns"] : 0;
}
set
{
ViewState["Columns"] = value;
}
}
private void CreateDynamicTable()
{
PlaceHolder1.Controls.Clear();
int tblRows = Rows;
int tblCols = Columns;
Table tbl = new Table();
PlaceHolder1.Controls.Add(tbl);
for (int i = 0; i < tblRows; i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < 4; j++)
{
if (j % 2 == 0)
{
TableCell tc = new TableCell();
Label label1 = new Label();
label1.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j;
tc.Controls.Add(label1);
tr.Cells.Add(tc);
}
else
{
TableCell tc = new TableCell();
TextBox txtBox = new TextBox();
tc.Controls.Add(txtBox);
tr.Cells.Add(tc);
}
}
tbl.Rows.Add(tr);
}
ViewState["dynamictable"] = true;
}
protected override void LoadViewState(object earlierState)
{
base.LoadViewState(earlierState);
if (ViewState["dynamictable"] == null)
CreateDynamicTable();
}
Ramya
|
|
|
|
|
To store the values to the database you would for example:
- create a SqlConnection[^]
- create a SqlCommand[^] and set INSERT INTO... statement to it
- add SqlParameters[^] to the SqlCommand.Parameters collection for your values
- open the connection
- create a loop where you set the parameter values and then execute the command
- close the connection
|
|
|
|
|
Hello
I need some help writing a couple of classes in C#.
One of them should encapsulate a point in a grid and the other one has a method to get 2 points with different X values and calculate the slope.
I'm quite lost since I haven't done any programming for over 2 years and my apologies if this doesn't make any sense.
here's what I got so far with the first class, it looks sketchy to me.
class Pointless
{
private int X, Y;
public Pointless(int nullx, int nully)
{
X = nullx;
Y = nully;
}
public void setX(int i)
{
i = 2;
X = i;
}
public void setY(int b)
{
b = -4;
Y = b;
}
public int getX()
{
return X;
}
public int getY()
{
return Y;
}
}
|
|
|
|
|
Can't you use two System.Drawing.PointF s and calculate the slope from that?
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
|
What's the purpose of the i = 2; and b = -4; ?
You might want to use properties rather than methods (automatic properties even).
You may even just want the members (X and Y) to be public, and perhaps readonly.
And have you considered using a struct instead?
|
|
|
|
|
Well the i and b were supposed to set the values of X and Y. Like I said I'm very rusty and have been scratching my head over this all day and feel like I'm moving backwards by now.
Yes properties seem much cleaner
Is this right?
public int pointX
{
get
{
return X;
}
set
{
X = 2;
}
}
|
|
|
|
|
X = value; would likely work better.
|
|
|
|
|
Two points that differ only on the X axis have a slope of 0.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|
Good, we can check the result of the calculation against a known value.
|
|
|
|
|
what do you think of this?
class Pointless
{
private int X, Y;
public Pointless()
{
X = 0;
Y = 0;
}
public int pointX
{
get
{
return X;
}
set
{
X = value;
}
}
public int pointY
{
get
{
return Y;
}
set
{
Y = value;
}
}
}
class Line
{
Pointless p1;
Pointless p2;
double slope;
public Line(Pointless p1_inn, Pointless p2_inn)
{
p1.pointX = 2;
p1.pointY = -4;
p2.pointX = 5;
p2.pointY = 3;
reiknaslope();
}
private double reiknaslope()
{
slope = (p1.pointX - p2.pointX) / (p1.pointY - p2.pointY);
return slope;
}
}
|
|
|
|
|
System.Drawing.Point already exists in the framework and the formula for slope of two points is:http://cs.selu.edu/~rbyrd/math/slope/[^] Your version is not correct?
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|
You probably shouldn't be setting the Xs and Ys in the Line constructor.
|
|
|
|
|
fixed it
now how the hell do I get it to write the number in console?
class Line
{
Pointless p1;
Pointless p2;
double slope;
public Line()
{
reiknaslope();
}
private double reiknaslope()
{
p1.pointX = 2;
p1.pointY = -4;
p2.pointX = -5;
p2.pointY = 3;
slope = (p2.pointY - p1.pointY) / (p2.pointX - p1.pointX);
return slope;
}
}
|
|
|
|
|
Pass the X and Y values to the constructors of Pointless.
Pass the instances of Pointless to the constructors of Line.
See the System.Console.WriteLine method for how to output.
|
|
|
|