Click here to Skip to main content
15,890,717 members
Home / Discussions / C#
   

C#

 
Questionhelp Print Version Professional C# 2005 Pin
rjorget7-Nov-05 3:37
rjorget7-Nov-05 3:37 
AnswerRe: help Print Version Professional C# 2005 Pin
Gulfraz Khan7-Nov-05 5:37
Gulfraz Khan7-Nov-05 5:37 
QuestionHelpProvider question Pin
Dan Neely7-Nov-05 3:34
Dan Neely7-Nov-05 3:34 
QuestionCopy Object - Problem within ArrayList Pin
Seraphin7-Nov-05 2:58
Seraphin7-Nov-05 2:58 
AnswerRe: Copy Object - Problem within ArrayList Pin
S. Senthil Kumar7-Nov-05 5:00
S. Senthil Kumar7-Nov-05 5:00 
Questionhow make a reference in c# Pin
rjorget7-Nov-05 2:36
rjorget7-Nov-05 2:36 
AnswerRe: how make a reference in c# Pin
S. Senthil Kumar7-Nov-05 5:05
S. Senthil Kumar7-Nov-05 5:05 
AnswerRe: how make a reference in c# Pin
Jon Rista8-Nov-05 7:51
Jon Rista8-Nov-05 7:51 
Here is some information about pointers in C#. A class is a reference type, and as such, it is always allocated on the heap, with a pointer to it on the stack. A struct us a value type, and as such, it is always allocated directly on the stack. You can force something to be passed by reference using the 'ref' modifier in a parameter. For classes, this will pass the original pointer. This contrasts with the normal method, which passes a copy of the original pointer. For structs, using ref will create a pointer to the struct data on the stack. This contrasts with the normal method, which passes a copy of the whole struct.

class MyClass
{
  public MyClass(int number)
  {
    Number = number;
  }

  public int Number; 

  public override string ToString()
  {
    return Number.ToString();
  }
}

struct MyStruct
{
  public MyStruct(int number)
  {
    Number = number;
  }

  public int Number;

  public override string ToString()
  {
    return Number.ToString();
  }
}

public class Program
{
  public static void Main()
  {
    MyClass myClass = new MyClass(10);
    MyStruct myStruct = new MyStruct(5);

    // Print the original values
    Console.WriteLine("Original Values:");
    Console.WriteLine(myClass);
    Console.WriteLine(myStruct);

    // Test the default passing mechanisms
    TestNorm(myClass, myStruct);

    Console.WriteLine("After first test:");
    Console.WriteLine(myClass);
    Console.WriteLine(myStruct);

    // Test 'ref' passing mechanism
    TestRef(myClass, myStruct);

    Console.WriteLine("After second test:");
    if (myClass == null)
      Console.WriteLine("myClass is null);
    else
      Console.WriteLine(myClass);
    Console.WriteLine(myStruct);
  }

  // This function gets a copy of the pointer to myClass
  // And gets a copy of the whole myStruct struct
  // Modifying myClass modifies the same actual data in memory
  // as the Main() function modifies.
  // Modifying myStruct modifies only the local copy.
  public static void TestNorm(MyClass myClass, MyStruct myStruct)
  {
    myClass.Number = 20;
    myStruct.Number = 10;

    Console.WriteLine("First Test:");
    Console.WriteLine(myClass);
    Console.WriteLine(myStruct);

    myClass = null;
  }

  // This function gets the original pointer to myClass
  // And gets a pointer to the original myStruct data on the stack
  // Modifying myClass modifies the same actual data in memory
  // as the Main() function modifies.
  // Modifying myStruct modifies the original data on the stack.
  public static void TestRef(ref MyClass myClass, ref MyStruct myStruct)
  {
    myClass.Number = 35;
    myStruct.Number = 15;

    Console.WriteLine("Second Test:");
    Console.WriteLine(myClass);
    Console.WriteLine(myStruct);

    myClass = null;
  }
}


The output of this program will be:

Original Values:
10
5
First Test:
20
10
After first test:
20
5
Second Test:
35
15
After second test:
myClass is null
15

-- modified at 13:54 Tuesday 8th November, 2005
Questionproblem with visual studio .net 2003 Pin
Gulfraz Khan7-Nov-05 1:48
Gulfraz Khan7-Nov-05 1:48 
AnswerRe: problem with visual studio .net 2003 Pin
Dan Neely7-Nov-05 5:05
Dan Neely7-Nov-05 5:05 
GeneralRe: problem with visual studio .net 2003 Pin
Gulfraz Khan7-Nov-05 5:24
Gulfraz Khan7-Nov-05 5:24 
GeneralRe: problem with visual studio .net 2003 Pin
Dan Neely7-Nov-05 7:16
Dan Neely7-Nov-05 7:16 
GeneralRe: problem with visual studio .net 2003 Pin
Gulfraz Khan8-Nov-05 0:09
Gulfraz Khan8-Nov-05 0:09 
Questionhttp://www.codeproject.com/csharp/minihttpd.asp Pin
sayangoin7-Nov-05 1:15
sayangoin7-Nov-05 1:15 
AnswerRe: http://www.codeproject.com/csharp/minihttpd.asp Pin
mav.northwind7-Nov-05 20:26
mav.northwind7-Nov-05 20:26 
Questiondownload image over http Pin
g00fyman7-Nov-05 0:54
g00fyman7-Nov-05 0:54 
AnswerRe: download image over http Pin
David Stone7-Nov-05 9:34
sitebuilderDavid Stone7-Nov-05 9:34 
GeneralRe: download image over http Pin
g00fyman7-Nov-05 10:26
g00fyman7-Nov-05 10:26 
GeneralRe: download image over http Pin
Jon Rista8-Nov-05 7:35
Jon Rista8-Nov-05 7:35 
GeneralRe: download image over http Pin
g00fyman8-Nov-05 13:07
g00fyman8-Nov-05 13:07 
QuestionCombining Cells Probs Pin
tadhg887-Nov-05 0:44
tadhg887-Nov-05 0:44 
GeneralRe: Combining Cells Probs Pin
Vikram A Punathambekar7-Nov-05 1:58
Vikram A Punathambekar7-Nov-05 1:58 
AnswerRe: Combining Cells Probs Pin
J4amieC7-Nov-05 2:35
J4amieC7-Nov-05 2:35 
QuestionSet File Access (Read/ReadWrite/Write) Pin
M-20006-Nov-05 23:59
M-20006-Nov-05 23:59 
AnswerRe: Set File Access (Read/ReadWrite/Write) Pin
Tom Larsen7-Nov-05 5:52
Tom Larsen7-Nov-05 5:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.