|
Can anyone tell me if there is something like pointer in C#. If I want to interchange two rows in a mtrix which is the most efficient way? In C, probabaly we can use, if we wan to elliminate the extensive use of the stoarage space.
Regards,
Amit
|
|
|
|
|
You can use regular old pointers in C#. You just have to do it in an unsafe context:
unsafe static void WriteLocations()
{
int *p;
int i;
i = 10;
p = &i;
string addr;
addr = int.Format((int) p, "X");
Console.WriteLine(addr);
Console.WriteLine(*p);
*p = 333;
Console.WriteLine(i);
i = *(&i) + 10;
Console.WriteLine(i);
}
Also, instead of marking a class or function as static, you can mark portions of code as unsafe:
void DoSomething()
{
unsafe
{
}
}
Also note that the C# compiler needs the /unsafe switch turned on in order to use unsafe code.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Homosexuality in Christianity
Judah Himango
|
|
|
|
|
Thanks Judah!
How can one function be unsafe? what is a unsafe code.
regards,
Amit
|
|
|
|
|
Unsafe code is
- code that cannot be verified as type-safe by the .NET Common Language Runtime (CLR).
- is not Common Language Specification (CLS)-compliant, meaning that other .NET languages like VB.NET, J#, and so on are not required to implement nor are they guaranteed to consume such code, if it were declared as public and made available to other .NET dlls & executables.
- requires a full trust security scenario in order to run, for instance, it must be installed/run from the target machine; if you try to use pointers in code run in a partial-trust scenario (e.g. a C# applet over the internet), the .NET CLR will throw a SecurityException because pointers require full access to the machine's memory, thus are potentially dangerous to the end user.
For a function, class, or code block to be unsafe, just use the unsafe keyword:
unsafe class MyClass
{
}
unsafe void MyFunction()
{
}
void MyFunctionWithUnsafeBlock
{
unsafe
{
}
}
Despite all these cautions, you're free to use pointers as much as you like if your application is a standalone app or dll, indeed, the .NET framework uses pointers internally quite often for performance gains and interop scenarios.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Homosexuality in Christianity
Judah Himango
|
|
|
|
|
There is seldom any real need for pointers. Can't you use a reference instead?
---
b { font-weight: normal; }
|
|
|
|
|
I really wanted to use something which can allow me to interchange position of two rows in a 2D array. say i want to make 3rd row 7th row and place 7th row as the third row. I don't want to use a temporary 1Darray for that. If each row has say 1000 elements, then i need 1000 temporary varibale to do the calculation. I don't want to do that.
|
|
|
|
|
What does the array contain?
If it contains objects, it's really an array of references. Then you can easily exchange different elements in the array.
---
b { font-weight: normal; }
|
|
|
|
|
Ironically, it contains double precision numerical value. Is there a way? How can I change the reference if it consists of objects.
Regards,
Amit
|
|
|
|
|
A double is a value type, not an object, so the array does not contain references. You have to exchange the values.
---
b { font-weight: normal; }
|
|
|
|
|
Well, arrays in C# can be seen as pointers. If you copy one via assigning another one (e.g. a1 = a2) then you don't get a real copy a2, you just get a new reference.
Greetings
|
|
|
|
|
I need to generate Excel Reports using Pre-Created Excel Template.
Data is fetched from the databse. Does any one know any managed open source tool
to achieve this? All help is appreciated.
Thanks,
Alomgir
|
|
|
|
|
How to write common event handler which can accept range through EventArgs.Is it possible to write my own Eventargs derive from System.EventArgs, so that i
can pass arguments when event handlers get called. Please tell me how to acheive this.
I have two textboxes called textbox1 and textbox2 each accept integer values with different range
textbox1 accept values between 1800-2200
textbox2 accept values between 2400-2800
i have onenter event handler for both the textboxes like this
//delegate and event handler for first text box
this.YearBuiltTextBox.Enter += new EventHandler(Parse_TextBox1);
public void Parse_TextBox1(System.Object sender, System.EventArgs e)
{
try
{
if (this.YearBuiltTextBox.Enabled == false) return;
object parseValue = DataFormatter.ParseInteger(this.YearBuiltTextBox.Text,1800, 2200);
}
}
//delegate and event handler for second text box
this.YearRenovateTextBox.Enter += new EventHandler(Parse_TextBox2);
public void Parse_TextBox2(System.Object sender, System.EventArgs e)
{
try
{
if (this.YearRenovateTextBox.Enabled == false) return;
object parseValue = DataFormatter.ParseInteger(this.YearBuiltTextBox.Text,2400, 2800);
}
}
How to write common event handler for this...
|
|
|
|
|
Yes, you can derive your own class from EventArgs and add custom fields to it.
I'm wondering how it would help you solve your problem, in your case, you know beforehand the range of valid values that YearBuiltTextBox and YearRenovateTextBox can accept. Why not put the range information in a hashtable or similar datastructure and do the ParseInteger stuff after retrieving the range?
I'm suggesting something like this
Hashtable rangeMap = new Hashtable();
rangeMap[YearBuiltTextBox] = new int[] {1800, 2200};
rangeMap[YearRenovateTextBox] = new int[] {2400, 2800};
this.YearRenovateTextBox.Enter += new EventHandler(Parse_TextBox);
this.YearBuiltTextBox.Enter += new EventHandler(Parse_TextBox);
and withing ParseTextBox
public void Parse_TextBox(System.Object sender, System.EventArgs e)
{
int[] range = (int[])rangeMap[sender];
object parseValue = DataFormatter.ParseInteger(this.YearBuiltTextBox.Text,range[0], range[1]);
}
Hope this helps.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
you could use one event hander for both textBoxes i.e.
<br />
textbox1.enter += new EventHandler(myEventHandler);<br />
textbox2.enter += new EventHandler(myEventHandler);<br />
public void myEventHandler(object sender,EventArg e)<br />
{<br />
<br />
TextBox tmpBox = (TextBox)sender;<br />
if(tmpBox.Name == "textbox1")<br />
{<br />
}<br />
else<br />
{<br />
}<br />
}<br />
|
|
|
|
|
If you need to, you can write your own event arguments simply by writing a class that derives from System.EventArgs.
However, you wouldn't be able to raise the event with your new arguments unless you inherit from the TextBox class and raise it yourself with your own arguments.
Here is a common handler for both:
this.yearBuiltTextBox.Enter += new EventHandler(Parse_TextBox);
this.yearRenovateTextBox.Enter += new EventHandler(Parse_TextBox);
void ParseTextBox(object sender, EventArgs e)
{
TextBox tbSender = (TextBox)sender;
if(sender.Enabled == false)
{
return;
}
int min, max;
if(tbSender == this.yearBuiltTextBox)
{
min = 1800;
max = 2200;
}
else
{
min = 2400;
max = 2800;
}
object parseValue = DataFormatter.ParseInteger(this.yearBuiltTextBox.Text, min, max);
}
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Homosexuality in Christianity
Judah Himango
|
|
|
|
|
Judah Himango wrote:
if(sender.Enabled == false)
object doesnt have an enabled property, so the above quoted line should have read
if(tbSender.Enabled == false)
or
if(!tbSender.Enabled)
|
|
|
|
|
hello
I have a datagrid which consists of two columns, one templatecolumn and one boundcolumn. the templatecolumn's got two checkboxes, one in the header (when pressed it checks all checkboxes) and one for every row.
the boundcolumn shows data from a datatable. when I press a button I want to check which of the checkboxes are checked, and for everone that is, get the datakey for that row and put it in an array.
to accomplish this I do
<br />
CheckBox cb;<br />
int i = 0;<br />
foreach( DataGridItem dgi in this.MyDataGrid.Items ) <br />
{<br />
cb = (CheckBox) dgi.Cells[0].Controls[1];<br />
if ( cb != null && cb.Checked ) <br />
{<br />
id = (int) MyDataGrid.DataKeys[i];<br />
alSelectedValues.Add(id.ToString());<br />
} <br />
i++;<br />
}<br />
but when I run program I get "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index "
can anyone see whats wrong?
/Regards Magnus
|
|
|
|
|
Hi All,
I want to know how to use Pessimistic locking inside code in the multiuser invironment urgant
|
|
|
|
|
You've just asked a very vague question. You want to use pessimistic locking of WHAT? What database, table, block, record, ... ? What DBMS are you using?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Hello all ,
Im new to dot net programming and want to develop a Client-Server app over a wireless medium preferably 802.11g . I need to know what libraries to use . Is it Winsock , and if so wat port does it use to listen connections.
I'll appreciate a prompt reply..
|
|
|
|
|
It's no different than any other wired network application. Your application shouldn't care what the network medium is anyway.
There is no specific port to use on wireless compared to anything else. TCP ports work not matter what the network medium is. The port you use in your app is entirely up to you, though, I would suggest not using a port number below 1024.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Thanx dave , i'll post if i hav more questions
|
|
|
|
|
Hello
I am learning crystal reports. I dont know how to use functions listed in formula editor. If any body knows plz help me.
Regards
Asmyan
|
|
|
|
|
I am tasked with creating a Windows service that acts as a process manager for several applications. the service must start, monitor and restart these applications. i've just created a basic service using C# and it installs and starts fine. i have a small piece of code to spawn a thread and start a process. the service executes this fine and the process starts, but there's no GUI. for instance, i tested it with notepad.exe. when i start my service, i see notepad.exe in Task Manager, but there's no notepad window on the screen. are Windows services limited in this regard? can i not start a program that has a GUI from a windows service?
thanks,
b
|
|
|
|
|
Your service is running under the Local System account (by default) and is not tagged as "Allow interaction with desktop". Go into the Service control panel, get properties on your service, click on the LogOn tab and you'll find what you're looking for.
By default, services run on their own desktop, not the visible one the user sees.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|