I realize you would not normally do this but:
- I want to place this into a function library
- I want to learn how to pass the stream writer stream between functions
I realize that I cannot pass streamwriter_cla as a class but I do not know how to pass it.
Thanks for you thoughts.
function streamwriter_instance_fun(streamwriter_cla : class, path_str : String)
{
// Create an instance of StreamWriter to write text to a file.
streamwriter_cla = new StreamWriter(Application.dataPath + path_str);
}
function streamwriter_write_fun(streamwriter_cla : class, string_str : String)
{
// Add to file using Write
streamwriter_cla.Write(string_str);
}
function streamwriter_writeline_fun(streamwriter_cla : class, string_str : String)
{
// Add to file using WriteLine
streamwriter_cla.WriteLine(string_str);
}
function streamwriter_close_fun(streamwriter_cla : class, path_str : String)
{
streamwriter_cla.Close();
}
Pass the stream itself by reference pointer.
I do not understand how to do that… that is why I am writing.
Care to give me a code example?
function streamwriter_instance_fun(streamwriter_cla : class, path_str : String)
Thanks.
You can pass around the BaseStream or you can just pass the StreamWriter as a StreamWriter(Or TextWriter if you want to be able to implement other Writers).
The examples are in C# but all classes on the MSDN are available to JavaScript with the same Methods and Properties.
Try not to pass an object around as an object or class generic castable type unless absolutely necessary. Lower to base classes or maintain it’s type or else you are just asking for casting and type error issues in the future.
Lol.
I appreciate the didactic efforts but learning object oriented coding is down the road for me. This is why I chose to code using Unity and javascript… so I could be coding now.
If some one comes across this and wants to just fill in how to pass this in a code example that would be great and appreciated.
Otherwise I guess I will have to drop this endeavor and try again a year from now.
Thanks.
function streamwriter_write_fun(streamwriter_cla : StreamWriter, string_str : String)
{
// Add to file using Write
streamwriter_cla.Write(string_str);
}
Lather, rinse, repeat.
JavaScript is still object oriented. Bite the bullet.
Oh man…
I actually tried that but when it did not syntax highlight in Unitron I assumed it was wrong.
I have not tried it yet but I will assume it is right.
Thanks for assisting me. So concise too! : )
Thanks.
One caveat from a method:
function streamwriter_instance_fun(streamwriter_cla : class, path_str : String)
{
// Create an instance of StreamWriter to write text to a file.
streamwriter_cla = new StreamWriter(Application.dataPath + path_str);
}
You’ll want to rewrite this to create and return the StreamWriter to be reused:
function streamwriter_instance_fun(path_str : String) : StreamWriter
{
// Create an instance of StreamWriter to write text to a file.
return new StreamWriter(Application.dataPath + path_str);
}
var myStreamWriter:StreamWriter = streamwriter_instance_fun("somePath");
streamwriter_write_fun(myStreamWriter, "asdf");
streamwriter_close_fun(myStreamWriter);