+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 18

Thread: Creating new instance of an object

  1. #1
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Creating new instance of an object

    Just wanted to check that when you use an object, is it given a value of null?

    Eg if I have
    Code:
    Public Class myObj
    {
       public int id {get; set;}
    
       public myObj()
       {
       }
    }
    
    And I use this in my page:-
    Code:
    myObj newObj;
    
    is the value of newObj=null?
    or do you have to create an instance?
    Code:
    myObj newObj = new myObj();
    
    would newObj=null now?

  2. #2
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Code:
    myObj newObj;
    
    this will be null.

    Code:
    myObj newObj = new myObj();
    
    this will not be null.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  3. #3
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    OK. So if I had this:-
    Code:
    myObj = newObj;
    
    Could I then do this?
    Code:
    if(ddl.selectedvalue=="-1")
    {
       newObj = new myObj();
       newObj.id=-1;
    }
    else if(ddl.selectedvalue!="")
    {
       newObj = myObj.GetObj(Convert.ToInt32(ddl.selectedvalue));
    }
    if(newObj!=null)
    {
    }else{
    }
    
    If I try that in VS, it gives me errors:-
    myObj is a 'type' but is used like a 'variable'
    newObj does not exist in the current context'

  4. #4
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    That was a typo, I corrected it above.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  5. #5
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    That's what I thought, but I was getting another error in VS.

    Just realised the variable was created inside a different if...end if, so wasn't available in the context I was using if(obj!=null)

    Doh...

  6. #6
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    It seems what you want is:
    Code:
    myObj newObj;
    if(ddl.selectedvalue=="-1")
    {
       newObj = new myObj();
       newObj.id=-1;
    }
    else if(ddl.selectedvalue!="")
    {
       newObj = myObj.GetObj(Convert.ToInt32(ddl.selectedvalue));
    }
    if(newObj!=null)
    {
    }else{
    }
    
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  7. #7
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Yeah, that's what I thought I was using, but I had it something like:-
    Code:
    if(thisistrue)
    {
      myObj newObj;
      if(ddl.selectedvalue=="-1")
      {
        newObj = new myObj();
        newObj.id=-1;
      }
      else if(ddl.selectedvalue!="")
      {
        newObj = myObj.GetObj(Convert.ToInt32(ddl.selectedvalue));
      }
    }
    if(newObj!=null)
    {
    }else{
    }
    

  8. #8
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Actually, I double checked my code and I have as in your post.

    But VS gives an error of "Use of unassigned local variable 'newObj'"

  9. #9
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    what line? and that's not an error, just a warning.
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  10. #10
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Here:-
    Code:
    myObj newObj;
    if(ddl.selectedvalue=="-1")
    {
       newObj = new myObj();
       newObj.id=-1;
    }
    else if(ddl.selectedvalue!="")
    {
       newObj = myObj.GetObj(Convert.ToInt32(ddl.selectedvalue));
    }
    if(newObj!=null)
    {
    }else{
    }
    
    It comes up in the Error List of VS. I guessed it might not be a problem, but just want to check that my !=null condition will actually work correctly.

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. Calling void isn't creating instance of an object
    By richyrich in forum .NET Development
    Replies: 5
    Last Post: March 14th, 2010, 11:43 AM
  2. Creating a scheduler
    By noFriends in forum .NET Development
    Replies: 8
    Last Post: June 29th, 2009, 08:35 AM
  3. Creating a drop down box from the css
    By dtz in forum HTML & CSS Help
    Replies: 1
    Last Post: March 12th, 2009, 09:10 AM
  4. Object reference not set to an instance of an object
    By Shem in forum .NET Development
    Replies: 4
    Last Post: July 22nd, 2008, 05:59 AM
  5. Object reference not set to an instance of an object
    By jmurrayhead in forum .NET Development
    Replies: 1
    Last Post: May 29th, 2008, 11:16 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

SEO by vBSEO