473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

!NoItAll
297 100+
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...

Expand|Select|Wrap|Line Numbers
  1. Public MyClass 
  2.     Property datatype as new cls_datatype
  3.     Property data as new cls_data
  4.     Public Sub LoadFile(byVal Filename as String)
  5.          If My.Computer.FileSystem.FileExists(Filename)then
  6.              Dim XMLString as String = My.Computer.FileSystem.ReadAllText(Filename)
  7.              Me = Deserialize(XMLString, Gettype(datatable))
  8.           Else
  9.              Throw New ArguementException("File not found!")
  10.           End if
  11.      End Sub
  12. End Class
  13.  
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!
Feb 13 '12 #1

✓ answered by !NoItAll

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

Expand|Select|Wrap|Line Numbers
  1. Public MyClass 
  2.     Property datatype as new cls_datatype
  3.     Property data as new cls_data
  4.     Public Sub LoadFile(byVal Filename as String)
  5.  
  6.          If My.Computer.FileSystem.FileExists(Filename)then
  7.              Dim MyTemp as new MyClass
  8.              Dim XMLString as String = My.Computer.FileSystem.ReadAllText(Filename)
  9.              MyTemp = Deserialize(XMLString, Gettype(MyClass))
  10.              _datatype = MyTemp.datatype
  11.              _data = MyTemp.data
  12.           Else
  13.              Throw New ArguementException("File not found!")
  14.           End if
  15.      End Sub
  16. End Class
  17.  
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:
Expand|Select|Wrap|Line Numbers
  1. Dim NewClasss as New MyClass
  2. NewClass.Loadfile("c:\somefolder\somefile.xml")
  3.  
Using this same technique I could also add MyClass.LoadXML and pass in the prequalified XML string.

3 1609
MrMancunian
569 Expert 512MB
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?
Feb 15 '12 #2
!NoItAll
297 100+
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

Expand|Select|Wrap|Line Numbers
  1. Public MyClass 
  2.     Property datatype as new cls_datatype
  3.     Property data as new cls_data
  4.     Public Sub LoadFile(byVal Filename as String)
  5.  
  6.          If My.Computer.FileSystem.FileExists(Filename)then
  7.              Dim MyTemp as new MyClass
  8.              Dim XMLString as String = My.Computer.FileSystem.ReadAllText(Filename)
  9.              MyTemp = Deserialize(XMLString, Gettype(MyClass))
  10.              _datatype = MyTemp.datatype
  11.              _data = MyTemp.data
  12.           Else
  13.              Throw New ArguementException("File not found!")
  14.           End if
  15.      End Sub
  16. End Class
  17.  
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:
Expand|Select|Wrap|Line Numbers
  1. Dim NewClasss as New MyClass
  2. NewClass.Loadfile("c:\somefolder\somefile.xml")
  3.  
Using this same technique I could also add MyClass.LoadXML and pass in the prequalified XML string.
Feb 17 '12 #3
!NoItAll
297 100+
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...
Feb 17 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Edvard Majakari | last post by:
Hi, I just found py.test and converted a large unit test module to py.test format (which is actually almost-no-format-at-all, but I won't get there now). Having 348 test cases in the module and...
11
by: Bob Rock | last post by:
Hello, I'd like to be able to allow instanciation of a class (class Class_A) only from another class method (class Class_B). And I'd like to write the necessary code to enforce this behavior...
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
12
by: Mark Kurten | last post by:
i have the code below: for starters the value of txtempid = 1 after i go through this routine (i put a break point on the txtname.value line), the value of txtEmpID is 2 (Just like it is...
3
by: Toco | last post by:
Hello. I have method (called GetKey) in a class that returns a string. Also, I have in another class, a method which makes a call to the GetKey method. What I wish to do is to do an evaluation of...
12
by: peregrine_falcon12 | last post by:
Is there a way to get a pointer to a class from inside one of the class's methods?
6
by: HockeyFan | last post by:
Class A
1
by: f3l | last post by:
I want to use non existing methods of a class: class x{ function y(){ } //more stuff }
13
by: Hussein B | last post by:
Hi, I'm familiar with static method concept, but what is the class method? how it does differ from static method? when to use it? -- class M: def method(cls, x): pass method =...
1
by: Jeff Lynn | last post by:
How do I invoke super's method with subclass's method of the same name? For example, Class A has two subclasses, B and C. Both B and C have function "DoSOmething()" that is very similar except 10%...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.