Understanding instantiation?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Understanding instantiation?

  1. #1
    Join Date
    Apr 2014
    Posts
    13

    Understanding instantiation?

    I've been coding in multiple linear languages (VB5/6, ASP Classic, etc.) for years. I'm finding that C# is quite different in its foundation and implementation, and I'm having some difficulty understanding what I think should be simple concepts. It's really like I need to just brain-dump what I know about classic coding. Hopefully some of you can help me ...

    I'm having problems understanding instantiation of an object multiple times, and then recalling a specific instance of it. If I was using arrays, I'd just use the element values to retrieve the data.

    So let's say I want to code a game in C# ... e.g. Monopoly (look out for Hasbro!). I want to code in the various aspects of the spaces. Things like the name, color, price, etc. I suppose I want to instantiate an object instance for each space.

    How would I correctly go about instantiating the spaces, and recalling the fields of a specific space?

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,951

    Re: Understanding instantiation?

    Create a class, and use that. Search for many samples @ MSDN
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    49

    Re: Understanding instantiation?

    c# takes much from java and c++ object instantiation works like any other language

    this is not perfectly exact but
    perfectly fine to think of it like this in a basic use manner

    SomeClass reftoobj;
    // SomeClass is a memory blueprint declaration
    // reftoobj is a variable of type reference it refers to a memory location
    // here its a uninstantiated object as no memory has yet been allocated
    // at present reftoobj is simply refering to a invalid memory area
    // thus it is said to be a null reference
    // in c# internally you can think of it like
    // a special flag marks it as such making c# much like java in this respect

    reftoobj = new SomeClass();
    // c# asks the os to allocate memory with
    // the size and structure of a SomeClass blueprint thats what new does
    // new returns a address pointer to the reference which it uses to access
    // the memory safely set aside for it to use with the structure of SomeClass
    // to say it is now instantiated its no longer null unless..
    // the os cannot get that much memory it will throw a out of memory exception

    SomeClass someotheref = reftoobj;
    // here we only make a reference someotheref and give it the memory pointer
    // of reftoobj to use for itself so now both of these address the same memory
    // area if reftoobj was null then someotherref is null as well

    reftoobj = new SomeClass();
    // this basically makes reftoobj take a pointer to a new memory location so
    // the old one if not assigned tosomeotheref can no longer be addressed

    reftoobj = null;
    // the reference to the memory location has been flaged null
    // calling reftobj.SomeMethod(); will cause a null reference exception
    Last edited by willmotil; July 18th, 2014 at 10:39 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center