J
James Matthews
Why when you create a class instance do you have to declare the class name
before.
newClass ras = new newClass();
Thanks
James
before.
newClass ras = new newClass();
Thanks
James
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
James Matthews said:Why when you create a class instance do you have to declare the class name
before.
newClass ras = new newClass();
Thanks
James
James said:Why when you create a class instance do you have to declare the class
name before.
newClass ras = new newClass();
Thanks
James
Gino said:Hope this helps.
The first part of the statement, "newClass ras" creates a varible "ras" of
type "newClass" on the stack.
It's value is set to null.
Gino said:The first part of the statement, "newClass ras" creates a varible "ras" of
type "newClass" on the stack.
It's value is set to null.
Actually a 'debuggable' builds (in most cases that build fromJon Skeet said:No - its value is unassigned, at least as far as the C# language is
concerned. An implementation could choose to clear the memory, but it
doesn't have to, as far as I'm aware. You won't be able to "see" the
contents of the memory until it's been definitely assigned anyway.
Actually a 'debuggable' builds (in most cases that build from
'DEBUG'-Configuration), its preset with null. But only, so that the debugger
has something to show, if it's stop before the assignment and the
programm has a specific behavior if the execution point is changed through
the debugger.
Conceptually local variables don't have initial values (if they aren't
initialized explicitly.)
Then what is a "null reference error" if there is no such thing as the
value
of an instance in the stack being set to null.
example:
SqlConnection myConnection;
myConnection.Open();
Throws a "null reference error" because myConnection has not been
instantiated.
When myConnection.Open(); is called the compiler goes to
"myConnection" in the stack and reads the value ( which is "null") and
returns a "null reference error".
Gino said:Then what is a "null reference error" if there is no such thing as the value
of an instance in the stack being set to null.
example:
SqlConnection myConnection;
myConnection.Open();
Throws a "null reference error" because myConnection has not been
instantiated.
Gino said:SqlConnection myConnection;
myConnection.Open();
Throws a "null reference error" because myConnection has not been
instantiated.
Jon Skeet said:Indeed. There's another case which is interesting in this respect, by
the way - out parameters and reflection. If you dynamically invoke a
method and use out parameters, you get to see the current value, which
indeed is 0/null/etc in .NET. It's tough for a language spec to
foresee all these possibilities, IMO.
Christof Nordiek said:I don't understand what you mean. Could you show an example that
demonstrates this issue.
Christof Nordiek said:I don't understand what you mean. Could you show an example that
demonstrates this issue.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.