|
-
Feb 26th, 2008, 11:49 PM
#1
Thread Starter
Fanatic Member
Can my.settings be used in a class?
I have made a error logging class which simply just writes to a text file. the mthod is called "log(text as string)"
The user has a option to "add timestamp" to begining of each line.
Instead of doing if statements on every exception, or activity to see if I should send the string with a timestamp or not, cant I just put the if statement in my class method?
for ex:
Code:
Public Class
Sub log(text as string)
if my.settings.addTimeStamp = true then
'add time
else
'leave it
end if
'log it
end sub
end Clas
-
Feb 27th, 2008, 12:13 AM
#2
Re: Can my.settings be used in a class?
Of course it can be used in a class. Just about everything IS a class. All your forms are classes, etc., etc. If you're using My.Settings there then you're already using it in a class.
Are you actually asking whether it can be accessed in a class library, i.e. a DLL? If so then the answer is "no". 'My' is a namespace under the root namespace of each project. Each application and library project has it's own My namespace. The Settings property you access in your application is a member declared in your application, therefore it only exists in your app.
What you could do is have your Log method take a Boolean parameter to indicate whether to add a timestamp. You can then simply pass My.Settings.AddTimeStamp as a parameter instead of having to use an If statement before calling it.
-
Feb 27th, 2008, 12:15 AM
#3
Thread Starter
Fanatic Member
Re: Can my.settings be used in a class?
Thanks for the reply.
Whats the difference between a class library and a class.
I thought all classes that I add to my solution turn into a .dll after compiling.
my log class is just right clicking on the solution and adding a class.
-
Feb 27th, 2008, 03:13 AM
#4
Re: Can my.settings be used in a class?
You don't add classes to the solution. The solution contains projects. You add classes to projects. Each project is compiled to an executable file; either an EXE or a DLL. An EXE is an application and a DLL is a library.
What would you say the difference is between a book and a library of books? So, what do you think the difference is between a class and a class library?
-
Feb 27th, 2008, 09:33 AM
#5
Re: Can my.settings be used in a class?
If you want to avoid doing if statements to find out whether you should include the timestamp or not on the log string, the answer is no, it's not possible. It doesn't matter where you build your log string, somehow you still have to check to see if the timestamp is needed every time you build the log string to write to the file.
-
Feb 27th, 2008, 01:30 PM
#6
Re: Can my.settings be used in a class?
I've used the My.Settings object in a class library.
Code:
Public Class SqlCode
Public Shared Function SelectCurrentAndPreviousEnrollments() As String
Dim sql As String = "Select * from PlanDataName.dbo.table"
Return sql.Replace("PlanDataName", My.Settings.PlanDataName)
End Function
End Class
-
Feb 27th, 2008, 10:02 PM
#7
Re: Can my.settings be used in a class?
I should qualify what I posted earlier. Yes you can use My.Settings in a class library, but the settings it accesses must be created in the class library project. The My you will be referring to is a namespace under your library project. It is NOT the same My that you refer to in any application that references that library. That means that you cannot create a setting in an application and then access that setting via My.Settings in a class library. That makes sense because the class library has no idea what application(s) might reference it. More than one could and they might not have the same settings defined.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|