Click here to Skip to main content
15,908,013 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: Help reading vector name from file Pin
Christian Graus4-Jul-07 17:16
protectorChristian Graus4-Jul-07 17:16 
GeneralRe: Help reading vector name from file Pin
empaleador5-Jul-07 1:41
empaleador5-Jul-07 1:41 
GeneralRe: Help reading vector name from file Pin
Christian Graus5-Jul-07 2:34
protectorChristian Graus5-Jul-07 2:34 
GeneralRe: Help reading vector name from file Pin
led mike5-Jul-07 6:36
led mike5-Jul-07 6:36 
GeneralRe: Help reading vector name from file Pin
Christian Graus5-Jul-07 7:24
protectorChristian Graus5-Jul-07 7:24 
QuestionRead lines in a file Pin
Perspx4-Jul-07 10:46
Perspx4-Jul-07 10:46 
AnswerRe: Read lines in a file Pin
Christian Graus4-Jul-07 11:01
protectorChristian Graus4-Jul-07 11:01 
QuestionTrivial Property not working Pin
iddqd5153-Jul-07 11:13
iddqd5153-Jul-07 11:13 
I'm getting really confused by trivial properties. Can someone explain this to me:

#include "stdafx.h"

using namespace System;

ref class Foo
{
public:
   // default constructor, initializes data members to non null values
   Foo() 
   {
      data = 0;
      s = "default";
   }
   // constructor with 2 parameters, initializes data members to non null values
   Foo(int d, String^ in)
   {
      data = d;
      s = in;
   }
   virtual String^ ToString() override
   {
      return "Data: " + this->Data + " S: " + this->S;
   }

   // I'm writing the properties explicitly
   // In theory, there is no reason I shouldn't be able to use trivial property
   // declarations in place of these two
   property int Data
   {
      int get()
      {
         return data;
      }
      void set(int d )
      {
         data = d;
      }
   
   }
   property String^ S
   {
      String^ get()
      {
         return s;
      }
      void set(String^ in)
      {
         s = in;
      }
   }
   // The trivial property declarations which are giving me trouble when I try to use them
   // property int Data;
   // property String^ S;

   // This is commented out just for demonstration purposes
   // With the current code these could be made private and things would still work
   // private: 
   int data;
   String^ s;
};

int main(array<System::String ^> ^args)
{
    // create Foo objects in a bunch of ways
    // none of them should have any null members
    Foo f;
    Foo b(1, "hello");
    Foo ^c = gcnew Foo(5, "world");

    // this will obviously fail if you make the data members private
    // In the current form, this prints out the correct values
    Console::WriteLine(f.data + " " + f.s->ToString());
    Console::WriteLine(b.data + " " + b.s->ToString());
    Console::WriteLine(c->data + " " + c->s->ToString());

    // This block prints correctly when using the explicit property declarations currently in use
    // whether the data members are private or public.
    // If I use the trivial property declarations (currently commented out) instead
    // then this block causes NullReferenceExceptions when trying to access S.
    // If I debug I notice that the <backing_store> for each member made by the trivial property
    // is not being set but there is a properly set Data and S field.
    Console::WriteLine(f.Data + " " + f.S->ToString());
    Console::WriteLine(b.Data + " " + b.S->ToString());
    Console::WriteLine(c->Data + " " + c->S->ToString());

    // With the explicitly written properties this prints the correct values.
    // With the trivial properties this prints 0 and "" for each Foo object but
    // doesn't cause a NullReferenceException like the above block.
    Console::WriteLine(f.ToString());
    Console::WriteLine(b.ToString());
    Console::WriteLine(c->ToString());
    return 0;
}


I'm so confused.

On a similar note, I have another code sample where I'm writing a very basic copy constructor for a ref class (with a single int data member) and if I use a trivial or explicit property the copy constructor works. If I remove the property declaration entirely and just access the data member (when its public), it works. But if I have the property declared (explicitly or trivially) then I can't set the data member in the copy constructor by accessing the member itself. I have to use the property name otherwise it sets it to 0.
QuestionRe: Trivial Property not working Pin
Mark Salsbery3-Jul-07 11:57
Mark Salsbery3-Jul-07 11:57 
AnswerRe: Trivial Property not working [modified] Pin
iddqd5153-Jul-07 12:13
iddqd5153-Jul-07 12:13 
GeneralRe: Trivial Property not working Pin
Mark Salsbery3-Jul-07 12:36
Mark Salsbery3-Jul-07 12:36 
GeneralRe: Trivial Property not working Pin
iddqd5155-Jul-07 5:08
iddqd5155-Jul-07 5:08 
GeneralRe: Trivial Property not working Pin
Mark Salsbery5-Jul-07 6:07
Mark Salsbery5-Jul-07 6:07 
AnswerRe: Trivial Property not working Pin
iddqd5153-Jul-07 12:16
iddqd5153-Jul-07 12:16 
GeneralRe: Trivial Property not working Pin
Daniel Grunwald10-Jul-07 8:42
Daniel Grunwald10-Jul-07 8:42 
GeneralRe: Trivial Property not working Pin
iddqd51510-Jul-07 11:11
iddqd51510-Jul-07 11:11 
QuestionTo refresh Data in my DataGridView Pin
mikobi3-Jul-07 5:11
mikobi3-Jul-07 5:11 
AnswerRe: To refresh Data in my DataGridView Pin
Paul Conrad14-Jul-07 13:39
professionalPaul Conrad14-Jul-07 13:39 
Questiondynamic cast question Pin
Programm3r3-Jul-07 4:27
Programm3r3-Jul-07 4:27 
AnswerRe: dynamic cast question Pin
George L. Jackson3-Jul-07 10:53
George L. Jackson3-Jul-07 10:53 
GeneralRe: dynamic cast question Pin
Programm3r3-Jul-07 19:54
Programm3r3-Jul-07 19:54 
QuestionWinPcap C++ CLI Pin
abbd3-Jul-07 4:01
abbd3-Jul-07 4:01 
QuestionHelp Needed Fast Pin
A. K. S3-Jul-07 1:43
A. K. S3-Jul-07 1:43 
AnswerRe: Help Needed Fast Pin
Pete O'Hanlon3-Jul-07 1:53
mvePete O'Hanlon3-Jul-07 1:53 
Questionsrand in c or c++ doesn't really Generate Random Numbers ? Pin
snailflying2-Jul-07 21:45
snailflying2-Jul-07 21:45 

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.