Javascript to .Net

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have researched a lot trying to get this. So as a last resource I will
have to ask here.

What would be the equivalent to the following javascript excerpt:

TheDataManager MyDMgr = new TheDataManager();

File MyDataFile = new File(strDataPath);

// Create a new file if and only if it doesn't already exist
boolean mNoFileExists = MyDataFile.createNewFile();

//new file was created
if ( mNoFileExists )
{
// Write out the data to disk using serialization
FileOutputStream fos = new FileOutputStream(MyDataFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(MyDMgr);
oos.close();
}
 
This isn't JavaScript. It's Java.

Assuming you have the source to TheDataManager class, it should be a pretty
straightforward conversion. What specifically do you have questions about?

--Bob
 
Oh, geez, sorry. Yes, it is Java. I've been working with programs in both
Java and Javascript, and suddenly I got themmixed up. Sorry about that.

I am translating from Java and Javascript to .Net (don't ask me why, I am
just following orders from my boss) and these lines are giving me a hard
time, though they seem very simple.

Yes, you can safely assume I have the implementation for class
TheDataManager. What I interpret from this Java code is that the programmer
wants to serialize and save to disk a TheDataManager instance class. The
thing is that I am not 100% sure about it, so I don't know exactly what to
do. That's why I have been looking for the "literally equivalent" lines of
codes for the Java lines I posted here.

If you wouldn't mind, how would you write these lines, say in C# or VB.Net?

Thank you very much.

C
 
Carlitos said:
What would be the equivalent to the following javascript excerpt:

TheDataManager MyDMgr = new TheDataManager();

File MyDataFile = new File(strDataPath);

// Create a new file if and only if it doesn't already exist
boolean mNoFileExists = MyDataFile.createNewFile();

//new file was created
if ( mNoFileExists )
{
// Write out the data to disk using serialization
FileOutputStream fos = new FileOutputStream(MyDataFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(MyDMgr);
oos.close();
}

First of all, this is Java and not JavaScript. The conversion to C#
should be straight forward as long as a) you've implemented the
TheDataManager class and b) the serialized files don't have to be
interoperable with the Java implementation.
If these conditions are met the following code should do the trick:

TheDataManager myDMgr=new TheDataManager();

if (!File.Exists(strDataPath)) {
FileStream stream=new FileStream(strDataPath,FileMode.Create);
BinaryFormatter formatter=new BinaryFormatter();
formatter.Serialize(stream, myDMgr);
stream.Close();
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Thank you Anders.

As I said in another post of this thread, yes, I realized this was not
Javascript after I posted the message.

Thanks again.

C
 
Back
Top