473,804 Members | 3,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

gcnew does not generate an object instance?

Hi everyone,

I am writing a small low level embedded USB application using C++/CLI
windows Forms.
I am sort of new to the C++/CLI and having trouble understanding what
happens in this very simple line of code:

I have a reference class pic_usbapi, declared in "pic_usbapi .h".
I want to create an object of the class and have a tracking handle for
it.
It should be simple to do, see CODE#1 and CODE #2

Basically, the problem is this:
--------------------------------------------
A) If I declare p_usbapi as an object without the hat, see CODE#1,
then no problem, but then p_usbapi does not seem to behave like a
tracking handle.
In particular, I can't access member functions using -such as
p_usbapi->led1();
I thought addressing reference classes was always done through a
handle?

B) Okay, so what if I declare p_usbapi to be a handle, with the hat,
CODE#2
Then the code compiles, but i get an exception when trying to address/
assign anything that has to do with p_usbapi. The reason is that an
object apparently was not created and handle is still null. I thought
that "gcnew pic_usbapi" was supposed to create the object no matter
what.

What I really would like to understand is why in CODE#2, an object of
the type pic_usbapi is not created and the p_usbapi handle is NULL.

Thanks!
// *************** ******* CODE # 1 *************** *************** ***
// This code works:
//
---------------------------------------------------------------------------------
#include "pic_usbapi c.h"
public:
Form1(void) //constructor for Form1;
{
InitializeCompo nent();
pic_usbapi^ p_usbapi = gcnew pic_usbapi; // Statement #1
}
//some code here
private:
pic_usbapi p_usbapi; //Declaration of an object, no hat.

// some time later a member function is called:
p_usbapi.led1() ; // I don't want to do this, but it
works.
// p_usbapi -led1(); // does not work here, because p_usbapi
is not a handle.
// weird, how can it not be a
handle, its a reference class!

//*************** *************** ***** CODE #2
*************** *************** *************** *
//This code throws an exception during runtime, because p_usbapi object
does not exist:
//---------------------------------------------------------------------------------

#include "pic_usbapi c.h"
public:
Form1(void) //constructor for Form1;
{
InitializeCompo nent();
pic_usbapi^ p_usbapi = gcnew pic_usbapi; // Statement #1
}
//some code here
private:
pic_usbapi^ p_usbapi; //Declaration of a tracking handle

// some time later a member function is called:
p_usbapi->led1(); // PROBLEM: Exception is thrown, as p_usbapi
object does not exist.
// I verified that p_usbapi does not
exist using the debugger.
// And, of course, also after compiling
the program

//
---------------------------------------------------------------------------------

Jan 7 '07 #1
0 1205

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2929
by: Nalaka | last post by:
Hi, I created a sinple web service that returns a dataSet. Then I created a client program that uses this web service (that returns the Dataset). My question is, how did the client figure out to create a "DataSet" as the return type from the webservice?
0
5116
by: Richard Gregory | last post by:
Hi, I have the wsdl below, for an Axis web service, and when I select Add Web Refernce in Visual Studio the proxy is missing a class representing the returnedElementsType (see reference.cs below the wsdl). This complex type is a collection of another complex type(elementType), and the Reference.cs has an array of these rather than the single returnedElementsType. If If I want to be able to obtain these elements from the SOAP response I...
15
8330
by: cedgington | last post by:
I wanted to take advantage of the large set of functionality offered by the framework, so for my latest project I'm using managed C++ with .NET v2. I'm using the gcnew operator in two different ways, and I'm confused about the lifetime of objects and whether or not I should be calling delete. Here are two examples: ref class SYMBOL : public IComparable { public: // Constructor / destructor
14
4867
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it can be done using the designer but I intentionally don't want to use that. The one reason is that you cannot change the code generated by the designer. The other could be that you have more free hand and control to design your GUI. 2....
3
3163
by: hedbonker | last post by:
OK - I am new to .net C++. Trying to write a simple app that creates an XML output file based on some values that a user puts in a form. After looking in the help, the sample code provided was this: XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = (" "); using (XmlWriter writer = XmlWriter.Create("personal.xml", settings)) { // Write XML data.
5
2865
by: Steven T. Hatton | last post by:
I haven't thought about rethrowing an exception in a long time, and tried to do it the wrong way. Now I'm curious about /why/ it's wrong. My expectation was that the exception instance would simply be passed up the call stack as the same object. It would therefore behave polymorphically. Is the behavior shown below compiler-specific? I'm using gcc 4.0.2. In terms of what goes on at runtime, why does the `throw e' act as if I threw a...
3
1297
by: pkolinko | last post by:
Hi everyone, I am writing a small low level embedded USB application using C++/CLI windows Forms. I am sort of new to the C++/CLI and having trouble understanding what happens in this very simple line of code: I have a reference class pic_usbapi, declared in "pic_usbapi.h". I want to create an object of the class and have a tracking handle for it.
6
7475
by: DaTurk | last post by:
Hi, I have several interfaces in CLI that I access via c#. My problem is, is that down in the unmanaged c++, which the CLI lies on top of, I have a lot of c_str() happening. But all of my methods in CLI return System::String^. I originally just gcnew'd System::String^ passing in the c_str(). But I can't really have as many gcnew's as I'm using for overhead and for fear of leaks. So my question is this, how can I get the char*...
0
1197
by: whm | last post by:
The page http://msdn2.microsoft.com/en-us/708fb7c4(VS.80).aspx has this code: ServiceController^ sc = gcnew ServiceController; if ( sc ) { ... Section 15.4.6 in ECMA 372 (the C++/CLI spec) seems to indicate that gcnew either returns a handle to an object or it throws but the Microsoft-written code above seems to imply that gcnew could return
1
458
by: dogbert1793 | last post by:
Hello, Does one need to delete pointers allocated with gcnew? Or does the garbage collector do it? I always though the garbage collector did it, but I saw this example today on MSDN: StreamReader^ sr = gcnew StreamReader( "TestFile.txt" ); try
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9163
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3830
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3000
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.