Click to See Complete Forum and Search --> : I need help with this C# to VB.NET


enb141
07-23-2004, 12:05 PM
Hi, I took this code from this site
http://www.codeproject.com/aspnet/createlogfiles.asp

Specifically in this part

public CreateLogFiles()
{
//sLogFormat used to create log files format :
// dd/mm/yyyy hh:mm:ss AM/PM ==> Log Message
sLogFormat = DateTime.Now.ToShortDateString().ToString()+" "+DateTime.Now.ToLongTimeString().ToString()+" ==> ";

//this variable used to create log filename format "
//for example filename : ErrorLogYYYYMMDD
string sYear = DateTime.Now.Year.ToString();
string sMonth = DateTime.Now.Month.ToString();
string sDay = DateTime.Now.Day.ToString();
sErrorTime = sYear+sMonth+sDay;
}

in vb.net this is what I did:

Public Function CreateLogFiles()
'sLogFormat used to create log files format :
' dd/mm/yyyy hh:mm:ss AM/PM ==> Log Message
sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> "

'this variable used to create log filename format "
'for example filename : ErrorLogYYYYMMDD
Dim sYear As String = DateTime.Now.Year.ToString()
Dim sMonth As String = DateTime.Now.Month.ToString()
Dim sDay As String = DateTime.Now.Day.ToString()
sErrorTime = sYear + sMonth + sDay
End Function

Well the problem is that in C# the function public CreateLogFiles() is automatically executed when sw.WriteLine(sLogFormat + sErrMsg); it's called from this function:

public void ErrorLog(string sPathName, string sErrMsg)
{
StreamWriter sw = new StreamWriter(sPathName+sErrorTime,true);
sw.WriteLine(sLogFormat + sErrMsg);
sw.Flush();
sw.Close();
}


How I can do that trick in VB.NET?

When I convert this to VB.NET it's not working

public Sub ErrorLog(sPathName As String, sErrMsg As String)
Dim sw As New StreamWriter
sw = new StreamWriter(sPathName+sErrorTime,true)
sw.WriteLine(sLogFormat + sErrMsg)
sw.Flush()
sw.Close()
End Sub

Michel_vd
07-27-2004, 08:01 AM
Hi,

what i think happens is this:

you say:
function public CreateLogFiles() is automatically executed when sw.WriteLine(sLogFormat + sErrMsg);

i think the function CreateLogFiles is not executed when you execute sw.WriteLine, but when the object CreateLogFiles is created (it is the constructor)

you didn't mention another part of the example code, which is:
CreateLogFiles Err = new CreateLogFiles();
Err.ErrorLog(Server.MapPath("Logs/ErrorLog"),ex.Message);

here, you create an object of the type CreateLogFiles.
What happens in dot net: it calls for the default constructor, which is a function with the same name of the class, so when you create an object of class AA, the function AA of that class is called.
So when you create an object of the type CreateLogFiles, the function CreateLogFiles of that class is called.

Ypu probably created a class with another name and pasted the CreateLogFiles function in that other class.
At that time, the function CreateLogFiles is no longer the constructor of your class but it is a normal function, which doesn't execute automaticly when the class is instantiated.

enb141
07-27-2004, 09:30 AM
You are amazing man, thanks for that tip, it will help me a lot in the future :) :) :) :)

enb141
07-27-2004, 09:49 AM
I got another question, when I try to do this in vb.net

Dim Err = New CreateLogFiles()

the Compiler removes it

Dim Err = New CreateLogFiles

and I also have a confusion with

Dim Err As New CreateLogFiles and
Dim Err = New CreateLogFiles

so do you know if the constructor can be called from VB.NET?

Michel_vd
07-27-2004, 02:28 PM
Originally posted by enb141
I got another question, when I try to do this in vb.net

Dim Err = New CreateLogFiles()

the Compiler removes it

Dim Err = New CreateLogFiles
Tried it, and it does with me too.
I presume you use visual studio?
I think, but i'm not sure, it removes the () because the class has only one constructor, with no arguments.
If you have a class with a constructor that needs an argument, you have to specify the argument within the () , like this:
Dim c As New System.IO.StringReader("string given to the constructor")

But if there is only one constructor (and your class has only one) and it has no arguments (as your constructor is), you can, obviously, skip the ()

It's a bit strange to me, because it would me more consistent to use the () for clearity.

But for example a button object, which has also one constructor with no arguments, can be declared like this in visual studio:
Dim a As New Button

while the MS help says this in their documentation:
Dim button1 As New Button()




and I also have a confusion with

Dim Err As New CreateLogFiles and
Dim Err = New CreateLogFiles

In line 1 you say:
Create (dim) a new variable of the type (AS) CreateLogFiles and instantiate (NEW) with a CreateLogFiles

in line 2 you say:
Create (dim) a new variable (the type not specified!) and instantiate (NEW) it with a CreateLogFiles

So in line 2 you have an object of which YOU know it is a button, but the compiler does not.

You can check it:
type line 1 and type Err. and you will get all button members because the compiler knows it's a button

type line 2 and type Err. and you will get only the object members (about 5) because the compiler only knows it's an object (which is a very general thing) so it has no knowledge, design time, that the variable is a button.


so do you know if the constructor can be called from VB.NET?

When you create an object (instantiate a class) in dot net, in any dot net language, so also in VB.net, you always call a constructor.

It can be with no arguments (like with your class) or it can be overloaded (different constructors with different parameters) .



Hope this answers your question,

Michel.

enb141
07-27-2004, 04:01 PM
Thanks again for your tips, in other place I found that VB.NET uses this contructor

Public Sub New()
---Code to be autoexecuted here
En Sub