Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hope anyone could point to the right destination, I am trying to figure out how I can check for the existence of a specific object with a specific fieldname, hope you can help! this is how I imagined it:

pseudocode:

C#
if ((fileReaderObj with field name == test)== null)
   {
     Filereader fileReaderObj = new Filereader(test2);
   }
   else 
   {
    // do something else 
    Console.WriteLine("Hey");
   } 


Said in other words:

When i create the object I assign a specific name to a public field in the object:

C#
Filereader fileReaderObj = new Filereader(test2);


when checking for object, how could I check if an object with that field name already exists?
Posted
Updated 20-Oct-11 2:05am
v2
Comments
I.explore.code 20-Oct-11 6:38am    
What do yo mean "existence of a specific object with a specific fieldname"?? just a null check?? or existence in a collection?? or something else??
dahund 20-Oct-11 7:59am    
No iam trying to create and object for each user that uses the system, and I need to separate these objects from one of the other so I don't have to recreate them all the time (When the object is created it pulls in allot of data from an SQL database, which takes some time) this time.

My idea was that I could use a specific field name for identification of an object, thereby checking if an object with a specific field name exists!!

Maybe there is another way?? naming the object, maybe but dont now how.

There is a fundamental misunderstanding of source vs compiled code here. The field name you give an object in source only has meaning at coding time – the fact you called that variable 'fileReaderObj' is only relevant at compile time, at runtime that information is not present.

What you're doing sounds like it could be replaced by using an existing entity management system (like NHibernate), but if you are doing it yourself for good reason: you need to store a runtime reference, either in the object or in a hash map that you look up into whenever you try to get one. For example:
Dictionary<string, FileReader> cache = new Dictionary<string, FileReader>();

FileReader GetFileReader(string key){
 FileReader r;
 if(!cache.TryGetValue(key, out r)) cache[key] = r = new FileReader(key);
 return r;
}

void SomeMethod(){
 // instead of this:
 // FileReader fileReaderObj = new FileReader(test);
 // use this:
 FileReader fileReaderObj = GetFileReader("test");
}


Note that the key is set at runtime to a string, not a field name. (Depending on what 'test' is you might need to pass it as well, if the FileReader needs information within that object.)
 
Share this answer
 
v2
Comments
dahund 20-Oct-11 7:41am    
Looks like this is what i need, hopefully you can answer some questions, or correct me I am wrong.

As I understand this, each time I create and object I assign a key(string) which is stored in the Dictionary(cache) next time I want to create a object i first check if the key(string) exists in Dictionary if not I create, else I do something different ( working with object); is this correct ?

and one last thing; Does this check if object runtime reference exists??:

if(!cache.TryGetValue(key, out r)) cache[key] = r = new FileReader(key);

Thx so very much :-)
BobJanova 20-Oct-11 16:36pm    
Roughly speaking, yes. The GetFileReader method handles the look up and checking if an entry with that key already exists, so in code that wants a FileReader, it is as simple as
  FileReader myFileReader = GetFileReader(key);
(as in the last line of the solution).

Yes, the line you quoted tries to get the existing value, and if there wasn't one, TryParse returns false, so a new FileReader is created and inserted into the cache. It's also assigned to r so it gets returned from the method.
dahund 20-Oct-11 7:45am    
sry, one last question, when working with this object reference. how do i use referenc (key.somemethod()) instead of obj.somemethod ?
BobJanova 20-Oct-11 16:36pm    
Once you've got a reader out of the cache with
  FileReader myFileReader = GetFileReader(key);
... you have a normal reference and you can just call myFileReader.SomeMethod() directly.
dahund 20-Oct-11 17:07pm    
perfect :-) i think this will work!!! but i would have to clean dictionary from time to time, since garbage collection could have removed some of the objects making my reference invalid!!! But again thank you, have been looking for something like this in a long time.
Do you mean whether there's a file handle open for a specific file? Because when you're trying to open an already opened file you get an IO Exception that indicates that the file could not be opened because it's already being used.
 
Share this answer
 
v2
If you are about to read text file.. here is the way you do. if its bitmap you can use bmpImg.Save() for creation..whereas for existance verification FILE.EXISTS() method you need to use.

C#
if (system.IO.FILE.Exists(filePath))
    //File.OpenRead(); read file body
else
  //File.CreateText(); Create file and write text into it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900