|
Not everyone uses VS. Snippets of code posted here don't have intellisense either. Nor do printouts
Don't rely on the behaviour of any one tool.
modified on Friday, January 23, 2009 1:26 AM
|
|
|
|
|
The semantics of an extension method are correct, and they increase code readability. They are certainly preferable than buggering up the type heirarchy with things like "MyListWithExtraStuff : List". They also allow you to extend any type which supports a particular interface, which is a powerful feature - almost like half-baked multiple inheritance.
myFoo.Baz(bar);
Is Baz an extension method? Does it matter? As long as it "bazzes" as documented, then it is no different from a normal method.
|
|
|
|
|
Mark Churchill wrote: The semantics of an extension method are correct
Because they added those semantics.
Mark Churchill wrote: they increase code readability
My opinion is that they do just the opposite.
Mark Churchill wrote: any type which supports a particular interface
Just like regular static methods.
Mark Churchill wrote: almost like half-baked multiple inheritance
You may be onto something there. Or maybe just on something.
Mark Churchill wrote: Does it matter?
Of course it makes a difference. Not to the original developer, not to the compiler, but when communicated between developers, particularly inexperienced ones.
I'm well aware that I take the minority view on this and many other things, but I'm not the only one. Others [weasel words] have the same and other concerns. I do write an occasional extension method (see my recent "Untabify and Tabify"[^] article), but I try to make it clear that extension methods are in use.
|
|
|
|
|
I'm trying to use a bluetooth device as though it were a socket. I am aware that this can be done using WINSOCK and P/Invoke, but to be honest I really don't want to put myself through that just yet . With WMI I can get the MAC address that the BT Dongle is using, but can I use the MAC Address to create a socket with .NET?
Scott P
"Simplicity carried to the extreme becomes elegance."
-Jon Franklin
|
|
|
|
|
Nope. The MAC address sits in the DataLink layer of the network stack, whereas sockets exist in the Network layer. You can't combine the two with any meaning. You'd have to resolve the MAC address to a Network layer address in order to create a socket.
|
|
|
|
|
Microsoft has encapsulated bluetooth communciation in their socket layer as an extension to Winsock.
There are a few APIs also provided that have the name BluetoothXXX.
Winsock would be your best bet on this.
Or you would have to go very low level as Dave mentioned.
«_Superman_»
|
|
|
|
|
Hi all!
(Sorry for my bad English...)
I need to read a value in a memory address. I don't need to change that value, just need to read it. Reading from a pointer would be better, but not that important if it would be much more hard than just reading a value in a memory address. I tried googling to find something, but can only find tutorials about making game trainers, but I don't need all that stuff, and actually I don't understand them.
Can anyone help me?
Edit:
I mean... I need to read the the value of a given memory address (or the pointer to that address) of a given running application. But don't know how to actually do that! I found this on the net "To pull this off you'll need to PInvoke OpenProcess and ReadProcessMemory." but I can't find a guide nor an example code about how using that to read memory. That's what I'm looking for...
modified on Thursday, January 22, 2009 6:58 PM
|
|
|
|
|
AFAIK C# isn't your best choice for access memory outside of the scope of the application. I'm pretty sure C# cannot use pointers as C++ can.
What is the goal of your project, surely there is a workaround.
|
|
|
|
|
EliottA wrote: I'm pretty sure C# cannot use pointers as C++ can.
It can - all you need to do is wrap it in an unsafe region.
|
|
|
|
|
Ahh I wasn't sure, sorry for the misinformation
|
|
|
|
|
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)
|
|
|
|
|