I
am learning about the programming language C #. to the use of unsafe
code and pointers in C #.
and this is all my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
struct Location
{
public int X;
public int Y;
}
unsafe static void Main(string[] args)
{
Location loc;
loc.X = 100;
loc.Y = 100;
Location* loc_ptr;
loc_ptr = &loc;
Console.WriteLine("X = {0}", loc_ptr->X);
loc_ptr->X = 200;
Console.WriteLine("X = {0}", loc_ptr->X);
}
}
}
the code is in error as follows:
unsafe code may only appear if compiling with/unsafe
// I tried and then move consoleapplication but it failed. and error is so
//Hope the experts help answer helped me
and this is all my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
struct Location
{
public int X;
public int Y;
}
unsafe static void Main(string[] args)
{
Location loc;
loc.X = 100;
loc.Y = 100;
Location* loc_ptr;
loc_ptr = &loc;
Console.WriteLine("X = {0}", loc_ptr->X);
loc_ptr->X = 200;
Console.WriteLine("X = {0}", loc_ptr->X);
}
}
}
the code is in error as follows:
unsafe code may only appear if compiling with/unsafe
// I tried and then move consoleapplication but it failed. and error is so
//Hope the experts help answer helped me
Chung Le KhaPosted Nov 2, 2009, 3:40 PM
:D
Kirtan PatelPosted Nov 2, 2009, 3:27 PM
if my answer Helped you to resolve problem then check "Do you like this answer" @ my post :)
Chung Le KhaPosted Nov 2, 2009, 3:17 PM
I solved my problem!
I was a more obstacles:
if in the code above we use the pointer that will modify the code like?
Sam HobbsPosted Nov 2, 2009, 2:14 PM
Roei BarPosted Nov 2, 2009, 2:06 PM
marking the project as Unsafe will make you incharge of everything regarding memory management and memory allocation.
about using the project as console and actually being in WinForm this is not an issue, you can use Winform with Console, its even easier to debug this way
Sam HobbsPosted Nov 2, 2009, 1:49 PM
Sam HobbsPosted Nov 2, 2009, 1:45 PM
Kirtan PatelPosted Nov 2, 2009, 1:40 PM
Here is Solution of Your Problem :)
dont forget to mark "Do you like this answer" it my answer Help you :)
Here are your mistake ..i found in programs I Corrected The Code :)
1. you have used Main() Method two times in Program Here is corrected Code
namespace WindowsFormsApplication2
{
static class Program
{
///
/// The main entry point for the application.
///
struct Location
{
public int X;
public int Y;
}
[STAThread]
unsafe static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Location loc;
loc.X = 100;
loc.Y = 100;
Location* loc_ptr;
loc_ptr = &loc;
Console.WriteLine("X = {0}", loc_ptr->X);
loc_ptr->X = 200;
Console.WriteLine("X = {0}", loc_ptr->X);
}
}
}
2. Second Error(Unsafe code may only appear if compiling with /unsafe) Can Be resolved By
Following Way :)
Go To Project Menu >> Properties>>Build >> Check Allow Unsafe Code Check Box To Solve it :)