www.webdeveloper.com
Results 1 to 6 of 6

Thread: I need help with this C# to VB.NET

  1. #1
    Join Date
    Feb 2004
    Posts
    19

    I need help with this C# to VB.NET

    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

  2. #2
    Join Date
    Jul 2004
    Location
    The Hague, the Netherlands
    Posts
    22
    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.

  3. #3
    Join Date
    Feb 2004
    Posts
    19
    You are amazing man, thanks for that tip, it will help me a lot in the future

  4. #4
    Join Date
    Feb 2004
    Posts
    19
    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?

  5. #5
    Join Date
    Jul 2004
    Location
    The Hague, the Netherlands
    Posts
    22
    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.

  6. #6
    Join Date
    Feb 2004
    Posts
    19
    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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles