473,383 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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_usbapic.h"
public:
Form1(void) //constructor for Form1;
{
InitializeComponent();
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_usbapic.h"
public:
Form1(void) //constructor for Form1;
{
InitializeComponent();
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 1180

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

Similar topics

7
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...
0
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...
15
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...
14
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...
3
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...
5
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...
3
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...
6
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...
0
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)...
1
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: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.