Skip to main content
gamedev.net
🔒 Locked

[.net] Java to C# code port please

Started by LuckyOne 3 replies 750+ views
Original Post
LuckyOne
LuckyOne
How do i port this part of code to C#:

  static {
    logSystem = new MyLogSystem();
  }

Full semi-Source here:

public final class MyLogSystem {
  
  private static MyLogSystem logSystem;
  private static File logFile;

  static {
    logSystem = new MyLogSystem();
  }

  // Private constructor
  private MyLogSystem() {
    try {
      logFile = new File("logs.txt");
      if (!logFile.exists())
        logFile.createNewFile();
    } catch (IOException ioex) {
      ioex.printStackTrace();
    }
  }
  
  public static MyLogSystem getInstance() {
    return logSystem;
  }
  
  public void writeOnLog(Exception e) {
    // write on the log
  }

}

// Then, you use it like this:
Exception e;
MyLogSystem.getInstance().writeOnLog(e);

Originaly started Here
aidan_walsh
aidan_walsh
Does this not do the same thing?

Sorry, but I have never seen the static {} construct in Java before.

private logSystem = new MyLogSystem();
www.aidanwalsh(.net)(.info)
LuckyOne
LuckyOne
No that will not do it. I need to create Global Class. In this example it logging class.
IN the 1st run-time i open filename, and from any class i could just pass informaion directly to log class without reinitializing it again.
davepermen
davepermen
static LogFile logFile = new LogFile("Path");

should do the trick.. not?
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
d_emmanuel
d_emmanuel
A C# static constructor is the equivalent to Java's static initializers.

class A{ static A() {   // Perform class intialization. }}


However, in this case, I believe davepermen's solution will give you what you want.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.