Results 1 to 11 of 11

Thread: [RESOLVED] Problems with my class...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Location
    My World
    Posts
    77

    Resolved [RESOLVED] Problems with my class...

    Hey all,

    It's good to be back on the forums yet again. I really tried hard this time to solve this one on my own, and i'm confident that after I get this resolved this will be the last major bug I have to fix. So....

    I have a class that i've created that stores a lot of information from a ListViewItem, a System.Array from one of my text boxes, and i'm trying to store the Text and Tag members from another ListViewItem for my images. The Text refers to the image name, and the Tag is the file path to the image. Everything was working fine until I started trying to assign values to the class members...

    This is my class:

    VB Code:
    1. Public Class LTask
    2.     Public Type As fMain.TaskType
    3.     Public Link As String
    4.     Public Name As String
    5.     Public Priority As String
    6.     Public StartDate As String
    7.     Public CompletionDate As String
    8.     Public Notes As System.Array
    9.     Public Images(Forms.ImagesWin.MAXIMAGES) As LImages
    10. End Class
    11.  
    12. Public Class LImages
    13.     Public Images_Text As String
    14.     Public Images_Tag As System.Object
    15. End Class

    This is the declaration of two objects that have the type of my class. The main problem here is that I can't use the New() method when i'm declaring these as arrays of my class...

    VB Code:
    1. Public PTasks(MAXTASKS), STasks(MAXTASKS) As LTask

    So basically what happens, to my knowledge, is that I declare both PTasks and STasks with a length of MAXTASKS in each, but everything inside both equals nothing. Am I right? Anyways, here's where I try to use the first, and the world come's crashing down. I get an error of 'Object Reference Not Set to Instance of an Object'.

    VB Code:
    1. Forms.MainWin.PTasks(NewTask.Index).Type = fMain.TaskType.Primary

    I know that the rest of this code is ok, the problem here is that NewTask.Index value references something inside Forms.MainWin.PTasks that apparently equals nothing. How can I fix this?

    Thanks guys.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,383

    Re: Problems with my class...

    you need to create each item first... currently when your code errors, it is because it is trying to assign a property on an object that does not yet exist...

    your declaration line
    VB Code:
    1. Public PTasks(MAXTASKS), STasks(MAXTASKS) As LTask
    creates 2 arrays, of type LTask, but like you said, the elements are set to nothing, which means they can not be accessed yet (except to check for nothing)...

    so what you need to do, is
    VB Code:
    1. Forms.MainWin.PTasks(NewTask.Index) = New LTask
    2. Forms.MainWin.PTasks(NewTask.Index).Type = fMain.TaskType.Primary

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Location
    My World
    Posts
    77

    Re: Problems with my class...

    Thanks, Ok now one more quick little glitch.

    This line gives me the same error:

    VB Code:
    1. Forms.MainWin.PTasks(Forms.MainWin.CurPTaskIndex).Images(i).Images_Text = Forms.MainWin.CurImages(i).Text

    The problem here is that Forms.MainWin.PTasks(Forms.MainWin.CurPTaskIndex).Images(i) is trying to reference nothing. It's pretty much the same problem as above except that I can't declare the size of this array within the class. How do I fix this?

    Thanks in advance.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,383

    Re: Problems with my class...

    you are trying to use it before it has any value... I would really need to see all the code to try to figure out what line you are missing or have in the wrong spot.

    to try to figure it out yourself.. step through the code using breakpoints.. and remember that any object needs to be created using the NEW keyword OR needs to be assigned to existing object before it can be used in code to look at its values...

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Location
    My World
    Posts
    77

    Cool Re: Problems with my class...

    The Images(i) array is declared as a member within my class. Is there a way to initialize the size of the array? I tried to do it the same place that I initialized the other two, but i don't think it worked. What do you think about this?

    VB Code:
    1. For i = 0 To MAXTASKS
    2.             PTasks(i) = New LTask()
    3.             STasks(i) = New LTask()
    4.             For j = 0 To Forms.ImagesWin.MAXIMAGES
    5.                 PTasks(i).Images(j) = New LImages()
    6.                 STasks(i).Images(j) = New LImages()
    7.             Next
    8.         Next
    Let your mind run free and your sword will follow.

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,383

    Re: Problems with my class...

    hmmm it looks ok to me.. but its nearing the end of the day, and sometimes code starts to get all jumbled up in my head.. try changing your declaration of the LImage class to
    VB Code:
    1. Public Class LImages
    2.     Public Images_Text As String = string.empty
    3.     Public Images_Tag As System.Object = nothing
    4. End Class

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Location
    My World
    Posts
    77

    Re: Problems with my class...

    Well, I tried it and it didn't work. I'm still not sure what's up, from the error it looks like the array still doesn't exist, but i'll keep searching...
    Let your mind run free and your sword will follow.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,239

    Re: Problems with my class...

    You should be able to determine definitively whether or not the array exists. When you get the error, select it and use Shift+F9 to see the current value (you may already be doing this, it isn't quite clear from your earlier statements).

    Is it only the Images() array within the PTasks() and STasks() arrays that doesn't exist?

    I would be tempted to add a constructorto the LTask class that sizes the images array, and initializes it all to new LImages(), rather than doing that externally.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Location
    My World
    Posts
    77

    Re: Problems with my class...

    You're correct about it being the images() array within the PTasks() and STasks() arrays that doesn't exist. It actually does exist because i tried setting the values that i wanted directly after my initialization of the values and it didn't give me an error, so that means that somewhere in my program the arrays are being screwed with and i need to figure out what's going on before i can go any farther forward. As soon as i find out what's going on i'll post an update, until then if you guys have any ideas feel free to comment... Thanks guys!
    Let your mind run free and your sword will follow.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,239

    Re: Problems with my class...

    You don't seem very new to this, so the most obvious suggestion I have could be almost insulting. You're right, though. If you can work with them in one place, and not in another, it sure suggests that you are re-creating the PTasks() and STasks(). It can't be that you are creating them on one instance of a form, then trying to work with them on another instance could it?

    However, this is an excellent reason for initializing the images in the constructor. Once the class is created, all components should be in a known good state. Somehow, somewhere, you are creating a class that is not in a known good state. That's what the constructor should do. Of course, in this case that's not necessarily that big a deal. If you are replacing a class with another accidentally, it really doesn't matter if one is in a useable state, and the other is in a known good, but garbage, state.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Location
    My World
    Posts
    77

    Re: Problems with my class...

    Thanks for all your help guys, after much debugging I finally finished all of the main features of my program and i'm moving on to the beta testing stage. Thanks so much guys, you always help me out.
    Let your mind run free and your sword will follow.

Posting Permissions

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



Click Here to Expand Forum to Full Width