/    Sign up×
Community /Pin to ProfileBookmark

I need help with this C# to VB.NET

Hi, I took this code from this site
[url]http://www.codeproject.com/aspnet/createlogfiles.asp[/url]

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 [COLOR=red]public CreateLogFiles()[/COLOR] is automatically executed when [COLOR=red]sw.WriteLine(sLogFormat + sErrMsg);[/COLOR] 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

to post a comment

5 Comments(s)

Copy linkTweet thisAlerts:
@Michel_vdJul 27.2004 — 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.
Copy linkTweet thisAlerts:
@enb141authorJul 27.2004 — You are amazing man, thanks for that tip, it will help me a lot in the future ? ? ? ?
Copy linkTweet thisAlerts:
@enb141authorJul 27.2004 — I got another question, when I try to do this in vb.net

Dim Err = New CreateLogFiles[COLOR=red]()[/COLOR]

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?
Copy linkTweet thisAlerts:
@Michel_vdJul 27.2004 — [i]Originally posted by enb141 [/i]

[B]I got another question, when I try to do this in vb.net [/B]

Dim Err = New CreateLogFiles[COLOR=red]()[/COLOR]

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 [COLOR=red]()[/COLOR] 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 [COLOR=red]()[/COLOR] , 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 [COLOR=red]()[/COLOR]

It's a bit strange to me, because it would me more consistent to use the [COLOR=red]()[/COLOR] 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) .

[/QUOTE]


Hope this answers your question,

Michel.
Copy linkTweet thisAlerts:
@enb141authorJul 27.2004 — 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
×

Success!

Help @enb141 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...