Thanks Kieth, But in my code examples you'll see the ref keyword does act like a pointer to an object. Where as the assignment operator just assigns a reference. In the former, there are two variables with the same address. The latter, two variables that only point to the same reference. Use of the new keyword in each scenario will confirm the difference. I just find it odd that a function parameter can make use of pointers to objects but there's no way to duplicate that behaviour inside of a function. I was able to use unsafe code and pointers to primitive types to share addresses across multple variables. But was unable to do the same with a class/object instance.
thenutz72
Posts
-
Assign By Reference -
Assign By ReferenceIs there any way to assign an object by address in C#? (I want to use the word reference but it complicates the question). It's like you can have reference behaviour through functions that you cannot have WITHIN the function. Example: private MyClass m_MyClass; public void MyFunction(ref MyClass objIn) { objIn = new MyClass() //Creates a new object in the function that called this procedure } But if did private MyClass m_MyClass; public void MyFunction(ref MyClass objIn) { m_MyClass = objIn; } public void SomeOtherFunction() { m_MyClass = new MyClass() //Now the calling function's object is forever unlinked } My question is: //Is there some way to do something more like m_Myclass = ref objIn; //???? I thank everyone who wants to be helpful but I am not looking for a workaround, I am trying to advance my understanding of .NET. Thanks again, All.
-
Public vs Private Memory QuestionHi All, My teacher stated - "Using public variables in a class will allocate memory for the lifetime of an application. So we should use private variables and use Get/Set." I believe that this is false and the get/set actually add some memory. Forgetting what is better programming practice, is he right about the memory? Signed, Puzzled in C#
-
Function Overload QuestionAlright, thanks brother. Just changed it and it looks good.
-
Function Overload QuestionHello again, all. I need help with function overloads. I have overloaded a class constructor but its not working. I added the second constructor and thought I could call the original and save a lot of typing. ref MyClass{ int ProductValue; MyClass(array^ arrIn) { ///Do lots of stuff ProductValue = arrIn[0] * arrIn[1]; } MyClass(int i, int j) { array^ newArr = gcnew array(2); newArr[0] = i; newArr[1] = j; MyClass::MyClass(newArr); }}; myCobj = gcnew MyClass(4,6); ///myCobj->ProductValue = 0 and not 24 When I call the 1st constructor everything works fine. But calling the second constructor does not return an initialized object. i.e. ProductValue is 0 (zero) instead of 24 (for example).
-
Intellisense helpThanks, But I've been trying that in C++ with no luck. According to some posts it looks like this only 'works' in C#. Anyone know if this is still true? I'm using VS 2008, hard to believe the ability is not there. Even as is, the xml tags show up in intellisense and nothing has changed in the tooltip.
-
Intellisense helpDon't know if this is intellisense per se, but when calling a function from within the same class you can type "(" or "," and that tooltip box pops up with the parameter list. Is there anyway to get summary info to display in there? Example: The tooltip might show " int MyFunction(int,int) ". I guess I'm looking to see something like " int MyFunction(int MaxValue,int MinValue) ". Thanks.
-
My own class & IntelliSenseDoes anyone know if you can do something similar within the same class? i.e. When calling a function from within the same class you can type ( or , and that tooltip pops up with the parameter list. Is there anyway to get that summary info to display in there? The tooltip might show " int MyFunction(int,int) ". I guess I'm looking to see something like " int MyFunction(int MaxValue,int MinValue) ". Thanks.
-
Reference User32.libAwesome answer, thanks!
-
Reference User32.libI'm trying to learn c++ and I just spent some time trying to use GetWindowText(). I was getting a linker error for unresolved token LNK2028, LNK2019. I finally added "C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Lib\User32.lib" path to Project->Properties->Linker->Input and it worked. But I have two questions for anyone who may know so that I may understand a little better. And maybe someone else can use this later, too. .NET 2008 C++, WinXP Home 1) Why did I have to that? I kept telling myself the dll was already referenced and I had the "windows.h" #include going on. Everything looked correct (as far as my understanding goes (not far)). The code is in a normal CLR project, .NET Form button_click (so it must be using User32.dll already, right?). If you check the Linker->Input Additional Dependencies it shows User32.lib in "Inherited Values" along with all of the other Windows libraries. So I assumed I didn't have to add it. 2) It may have worked but did I do it 'right'? I ran a search on my drive for user32.lib (I expected it in C:\WINDOWS somewhere but it was in the path listed above. Should I move it? There are others in "C:\Program Files\Microsoft SDKs\Windows\v6.0A\Lib" and the like. Thanks.
-
Circular Form ReferenceShut up, jackass.
-
Cross reference between classesNavaneeth (or anyone :), I posted this under "Circular Form Reference" subject but it's a very similar question. I had tried to add 'ref class Form2;' but now get a 'no appropriate default constructor available'. I was able to recreate my issue by simply creating two forms and adding a button to each that call the other form. So most of the code is the automaticly generated code from MS. I'm trying to keep the example simple by only incl. some code. /*Form1.h*/ #pragma once #ifndef First #define First #include "Form2.h" ref class Form2; private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form2 ^ frm = gcnew Form2; frm->Show(); this->Close(); } #endif /*Form2.h*/ #pragma once #ifndef Second #define Second #include "Form1.h" private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form1 ^ frm = gcnew Form1; frm->Show(); this->Close(); } #endif
modified on Tuesday, March 24, 2009 1:30 PM
-
Circular Form ReferenceWow, talk about confused. For someone who doesn't know the answer you sure have a lot to say. Clearly, you misunderstand an attempt to simplify an example. As such you should consider not "answering" (loosely used, in your case) to people's posts.
-
Circular Form ReferenceHey, I am trying to open form2 from form1. Then open a new form1 when form2 is closed. But adding the #include "Form1.h" header to form2.h is causing errors (undeclared identifier). I added these inclusion guards and tried using forward declaration. EIther that didn't work or I used it wrong, not sure I followed the correct syntax since all of the code is in the header files - the automatically generated code for Forms and controls. /*Form1.h*/ #pragma once #ifndef First #define First #include "Form2.h" private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form2 ^ frm = gcnew Form2; frm->Show(); this->Close(); } #endif /*Form2.h*/ #pragma once #ifndef Second #define Second #include "Form1.h" private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form1 ^ frm = gcnew Form1; frm->Show(); this->Close(); } #endif
-
Form Circular Reference?Hey, I am trying to open form2 from form1. Then open a new form1 when form 2 is closed. But adding the #include "Form1.h" header to form2.h is causing errors (undeclared identifier). I added these inclusion guards and tried using forward declaration. EIther that didn't work or I used it wrong because these are the automatically generated code for Forms in the .h file. /*Form1.h*/ #pragma once #ifndef First #define First #include "Form2.h" private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form2 ^ frm = gcnew Form2; frm->Show(); this->Close(); } #endif /*Form2.h*/ #pragma once #ifndef Second #define Second #include "Form1.h" private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form1 ^ frm = gcnew Form1; frm->Show(); this->Close(); } #endif
-
Out Of Scope, Debugging ProblemHi All, I'm a pretty experienced programmer but can't figure how to see the value of some variables when in debugger - either Watch Window or the Immediate Window (.NET 2008, C++). The Watch Window lists them as :Out of Scope. At the least any variable passed into a function is not visible. Example: void MyFunction(int x, Int32 y) { int i; i = x * y; } i would be available but x & y are not. I've tried some project settings, optimization is off. Any help?
-
Multiple EventsHello, I am writing a BlackJack type program to learn C++/.NET. I'm, trying to raise an event from Shoe Class (DealtCutCard Event) which is used by Dealer Class. Dealer sets a flag and at the apprpriate time raises a DealerShuffles Event which the Shoe Class uses. This code won't compile, the error is in the Shoe.h: "improper syntax for specifying event in __hook/__unhook" line 25 "improper syntax for specifying event in __hook/__unhook" line 25 "improper syntax for specifying event in __hook/__unhook" line 25 If I comment out #include "Dealer.h" and the __hook init function this will compile, but now I can only raise events from Shoe it won't recieve events from Dealer. How can I both call and handle events from between the same two classes? BTW - note i NEED "Shoe.h" in Dealer because there are functions in Dealer that reference Shoes. Using .NET 2003 Also, I tried substituting #include "Dealer.h" with class Dealer; as a predeclaration but that didn't work. Dealer.h
#pragma once
#include "Bettor.h"
#include "Hand.h"
#include "Shoe.h"#ifndef DEALER_H
define DEALER_H
[event_source(native)]
[event_receiver(native)]class Dealer
{
public:
__event void DealerShuffles(void);
Dealer(Shoe*);
void InitShoeEvents(Shoe* pSource){
__hook(&Shoe::DealtCutCard, pSource, &Dealer::ReadyToDeal);};
};
#endifShoe.h
#pragma once
#ifndef SHOE_H
define SHOE_H
#include "Dealer.h"
[event_receiver(native)]
[event_source(native)]
class Shoe
{
public:
void Shuffle(void);
//Initialize events from Dealer
void InitDealEvents(Dealer* pSource){
__hook(&Dealer::DealerShuffles, pSource, &Shoe::Shuffle);};
//Events
__event void DealtCutCard(void);
//Variables
};
#endif