Assign a value to a class from a method within the class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • !NoItAll
    Contributor
    • May 2006
    • 297

    Assign a value to a class from a method within the class

    I know this is possible, and it is probably something simple that I am missing...
    I have a fairly complex class to handle deserialization of an XML file. I wish to place a [class].loadfile method within the class. It looks something like this:

    Serialization decoration left out for simplicity...

    Code:
    Public MyClass 
        Property datatype as new cls_datatype
        Property data as new cls_data
        Public Sub LoadFile(byVal Filename as String)
             If My.Computer.FileSystem.FileExists(Filename)then
                 Dim XMLString as String = My.Computer.FileSystem.ReadAllText(Filename)
                 Me = Deserialize(XMLString, Gettype(datatable))
              Else
                 Throw New ArguementException("File not found!")
              End if
         End Sub
    End Class
    The Deserialization is working just fine (that's my own code), but assigning the object value to Me is not correct. What I want is for an instantiation of this class to have a LoadFile method (similar to the XML class LoadFile method) - but I cannot figure out how to set the value of the class from within the class.
    Any help would be greatly appreciated!
  • MrMancunian
    Recognized Expert Contributor
    • Jul 2008
    • 569

    #2
    I'm afraid I don't understand what you're trying to do here... What does Me refer to? Or what should it refer to? You say you want to set the value of the class from within the class, but does that mean that you want to create a new instance of the class? If not, what is the datatype of Me and what is the output from Deserialize?

    Comment

    • !NoItAll
      Contributor
      • May 2006
      • 297

      #3
      Ok - I figured it out (with the help of a colleague) and, of course it was so obvious that I am embarrassed!

      All I needed to do was create an instance of the class inside the method, then use that instance to assign the properties. Here's the new code example

      Code:
      Public MyClass 
          Property datatype as new cls_datatype
          Property data as new cls_data
          Public Sub LoadFile(byVal Filename as String)
               
               If My.Computer.FileSystem.FileExists(Filename)then
                   Dim MyTemp as new MyClass
                   Dim XMLString as String = My.Computer.FileSystem.ReadAllText(Filename)
                   MyTemp = Deserialize(XMLString, Gettype(MyClass))
                   _datatype = MyTemp.datatype
                   _data = MyTemp.data
                Else
                   Throw New ArguementException("File not found!")
                End if
           End Sub
      End Class
      Now - my apologies to all because my original example was confusing and incorrect - the datatype I needed to pass to my deserializer was MyClass and the reference to datatable was confusing.
      So what this gives me is a class (MyClass) with a method that has Loadfile so now I can create an instance of MyClass and then call the Loadfile method.
      Example:
      Code:
      Dim NewClasss as New MyClass
      NewClass.Loadfile("c:\somefolder\somefile.xml")
      Using this same technique I could also add MyClass.LoadXML and pass in the prequalified XML string.

      Comment

      • !NoItAll
        Contributor
        • May 2006
        • 297

        #4
        Yes - I meant for the Me keyword to represent the current class - and I knew it was not valid here - so I can see why it would be confusing to someone who understands how classes work better than I. Yes - I did need to create an instance of the class - but for some reason just didn't think of that. It took a colleague who writes in C# about 4 seconds to look at my class and tell me that ... I feel a bit embarrassed actually...

        Comment

        Working...