0

I have a basic object oriented architecture question. Suppose I have a class in VB.NET called OutputLog. It has methods "Create", "Write", and "Read".

In my program I have numerous classes in which I want to write to one log file. So, if I have a startup class for me program, which has:

Public Class Main

  logFile = new OutputLog()
  logFile.create("c:\abc.log")
  logFile.write("first entry to the log file")

End Class


Public Class Two

  "I want to write to this same log file in this class"

End Class

Public Class OutputLog

    Methods to "Create", "Write" and "Read"

End Class

What is the best way to architect this? I could pass the reference to logFile in the constructor of Class Two? Is this the best way to handle this? This would mean that for every class I want to write out to the log file I would need to pass logFile in the constructor?

Thanks

Ann Sanderson
  • 201
  • 1
  • 8
  • 13

3 Answers3

0

I can come up two ways to make a better architecture:

a) make all members of OutputLog static, such as Create, Write and Read. Therefore you can invoke these members anywhere.

b) read something about Singleton. consider OutputLog as a singleton. that would solve the problem too.

It is the last thing i would do to pass logFile to every constructor.

tess3ract
  • 183
  • 7
0

Your options are really:

You could pass a copy of the class around but this is a bit pointless and just means you need to add more and more code when you add more classes

Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
0

If you want to write in same file in separated class, you should declare your logfile var in module as public

Assumed you create Module1.vb in your project .. add this

Public logFile as New OutputLog

In your Class Main

Public Class Main

  logFile.create("c:\abc.log")
  logFile.write("first entry to the log file")

End Class


Public Class Two

  logFile.write("this is from class two")

End Class
matzone
  • 5,703
  • 3
  • 17
  • 20