Results 1 to 11 of 11

Thread: Converting Function from VB.NET - C#

  1. #1

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Converting Function from VB.NET - C#

    Anyone know how to make this function work?

    This is the function that i made a while back in VB.NET(Works) that writes to an RTB (rich Text Box).

    VB Code:
    1. Public Sub AddChat(ByVal ParamArray aT() As Object)
    2.  
    3.         Dim i As Integer
    4.  
    5.         With Chat
    6.             .SelectionStart = .Text.Length
    7.             .SelectionLength = 0
    8.         End With
    9.  
    10.         For i = 0 To UBound(aT) Step 2
    11.  
    12.             With Chat
    13.                 .SelectionColor = aT(i)
    14.                 .SelectedText = aT(i + 1)
    15.             End With
    16.  
    17.         Next i
    18.  
    19.  
    20.         With Chat
    21.  
    22.             .SelectionLength = 0
    23.             .SelectionStart = .Text.Length
    24.             .SelectedText = Microsoft.VisualBasic.ControlChars.CrLf
    25.  
    26.         End With
    27.  
    28.  
    29.  
    30.     End Sub


    to use the function in VB.NET

    it would be:
    VB Code:
    1. AddChat(Color.White, "Blah Blah Blah")


    How would this look in C#?
    Last edited by PlaGuE; May 15th, 2006 at 01:13 AM.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Converting Function from VB.NET - C#

    There are links to code converters in my signature. Note that C# has no equivalent to the With key word. Also, you should use aT.GetUpperBound(0) instead of UBound (which you should in VB.NET too if you ask me). Finally, the escape sequence "\r\n" is the equivalent of CrLf.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: Converting Function from VB.NET - C#

    --EDIT-- Forgot to add the conversion that i made...(Not feelin well..Hada go to the latrine..hehe... so i musta hit post...sorry)

    Using
    VB Code:
    1. public void AddChat(params object[] aT)
    2. {
    3.  int i;
    4.  Chat.SelectionStart = Chat.Text.Length;
    5.  Chat.SelectionLength = 0;
    6.  for (int i = 0; i <= UBound(aT); i += 2) {
    7.    Chat.SelectionColor = aT(i);
    8.    Chat.SelectedText = aT(i + 1);
    9.  }
    10.  Chat.SelectionLength = 0;
    11.  Chat.SelectionStart = Chat.Text.Length;
    12.  Chat.SelectedText = Microsoft.VisualBasic.ControlChars.CrLf;
    13. }


    using http://www.developerfusion.co.uk/uti...btocsharp.aspx


    EDIT#2::

    Whats wrong with this?
    VB Code:
    1. public void AddChat(params object[] aT)
    2.         {
    3.             int i;
    4.             rtf_Chat.SelectionStart = rtf_Chat.Text.Length;
    5.             rtf_Chat.SelectionLength = 0;
    6.             for (int i = 0; i <= UpperBound(aT); i += 2)
    7.             {
    8.                 rtf_iRC_Chat.SelectionColor = aT(i);
    9.                 rtf_iRC_Chat.SelectedText = aT(i + 1);
    10.             }
    11.             rtf_Chat.SelectionLength = 0;
    12.             rtf_Chat.SelectionStart = rtf_Chat.Text.Length;
    13.             rtf_Chat.SelectedText = Microsoft.VisualBasic.ControlChars.CrLf;
    14.         }

    i also tried

    VB Code:
    1. public void AddChat(params object[] aT)
    2.         {
    3.             int i;
    4.             rtf_wnd.rtf_Chat.SelectionStart = rtf_wnd.rtf_Chat.Text.Length;
    5.             rtf_wnd.rtf_Chat.SelectionLength = 0;
    6.             for (int i = 0; i <= UpperBound(aT); i += 2)
    7.             {
    8.                 rtf_wnd.rtf_iRC_Chat.SelectionColor = aT(i);
    9.                 rtf_wnd.rtf_iRC_Chat.SelectedText = aT(i + 1);
    10.             }
    11.             rtf_wnd.rtf_Chat.SelectionLength = 0;
    12.             rtf_wnd.rtf_Chat.SelectionStart = rtf_Chat.Text.Length;
    13.             rtf_wnd.rtf_Chat.SelectedText = Microsoft.VisualBasic.ControlChars.CrLf;
    14.         }
    but rtf_Chat had a protection error.

    What to do?

    Sorry if i sound noobish...
    Last edited by PlaGuE; May 15th, 2006 at 01:36 AM.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Converting Function from VB.NET - C#

    When you use an array in a for loop in C# don't use the upper bound with a "<=", rather use the length with a "<". Also, when you index things like arrays in C# you use square brackets, not parentheses.
    Code:
                for (int i = 0; i < aT.Length; i += 2)
                {
                    rtf_wnd.rtf_iRC_Chat.SelectionColor = aT[i];
                    rtf_wnd.rtf_iRC_Chat.SelectedText = aT[i + 1];
                }
    I must say too, that using an object paramarray as you have is not a good idea when each parameter has a specific meaning. There is no guarantee whatsoever that every the first parameter will be a colour and the second text and so on. That is, in my opinion, a flawed way to pass arguments.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: Converting Function from VB.NET - C#

    It was my first function... Was learning...
    Now i know.
    I should read up more n fix it... Cuz im still learnin C#
    Last edited by PlaGuE; May 15th, 2006 at 01:57 AM.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Converting Function from VB.NET - C#

    "int i" is declared twice.
    I don't live here any more.

  7. #7

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: Converting Function from VB.NET - C#

    Sorry for bumpin this... seemingly dead topic... but... i finally was able to get back to it.


    okay i took out the first declaration of "int i" in the for loop.
    which gives me this.
    VB Code:
    1. public void AddChat(params object[] aT)
    2.         {
    3.             rtf_Chat.SelectionStart = rtf_Chat.Text.Length;
    4.             rtf_Chat.SelectionLength = 0;
    5.             for (int i = 0; i < aT.Length; i += 2)
    6.             {
    7.                 rtf_Chat.SelectionColor = aT[i];
    8.                 rtf_Chat.SelectedText = aT[i + 1];
    9.             }
    10.             rtf_Chat.SelectionLength = 0;
    11.             rtf_Chat.SelectionStart = rtf_Chat.Text.Length;
    12.             rtf_Chat.SelectedText = Microsoft.VisualBasic.ControlChars.CrLf;
    13.         }

    That takes care of that error.
    which was....
    VB Code:
    1. Error   1  
    2. A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i',
    3. which is already used in a 'parent or current' scope to denote something else

    the only two errors left deal with the aT object.
    VB Code:
    1. Error   2  
    2. Cannot implicitly convert type 'object' to 'System.Drawing.Color'.
    3. An explicit conversion exists (are you missing a cast?)
    VB Code:
    1. Error   3  
    2. Cannot implicitly convert type 'object' to 'string'.
    3. An explicit conversion exists (are you missing a cast?)

    I still cant figure out whats wrong.
    Last edited by PlaGuE; May 29th, 2006 at 01:37 AM.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  8. #8
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: Converting Function from VB.NET - C#

    This might help you; it has more features than your function and doesn't require that every other argument be a color. I wrote it a while ago.

    Code:
    public void WriteTextF(params object[] o)
    {
       Type tCurrent;
       Color cCurrent = Color.White;
       Font fCurrent = new Font("Courier New",8.25f);
    
       for(int i = 0; i < o.Length; i++)
       {
          tCurrent = o[i].GetType();
    
          if (tCurrent == typeof(string))
          {
             // write text to textbox
             RTB.SelectionStart = RTB.Text.Length;
             RTB.SelectionLength = 0;
             RTB.SelectionFont = fCurrent;
             RTB.SelectionColor = cCurrent;
             RTB.SelectedText = (string)(o[i]);
             RTB.SelectionStart = RTB.Text.Length;
          }
          //update color choice
          else if (tCurrent == typeof(Color))
          {
             cCurrent = (Color)(o[i]);
          }
          //update font choice
          else if (tCurrent == typeof(Font))
          {
             fCurrent = (Font)(o[i]);
          }
          // bad type! bad!
          else 
          {
             throw new Exception("Unsupported Type");
          }   
    
    
       }
    
    
    }
    
    // usage:
    Font f = new Font("Tahoma",12);
    WriteTextF("White courier",Color.Gold,"gold courier",f, "gold tahoma", "more gold tahoma", Color.Red, "red tahoma.");
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Converting Function from VB.NET - C#

    What's wrong is that references of type object cannot be implicitly converted to references of any other type because the compiler has no guarantee whatsoever that they will refer to an object of the specified type. I told you that you should pass objects of the specific type that you want to use. If you choose to ignore that advice then you need to explicitly convert the object references to the types you want using casts. By doing so you are telling the compiler that you do guarantee that the references will refer to objects of the type that you say. If the app crashes because they weren't then it's your fault and not the compiler's. Bascially it's bad practice to use a more general type than you intend to use without good reason. The fact that you don't want to rewrite the function to take objects of the correct types through two separate arrays, a keyed collection or your own custom type doesn't strike me as a good reason.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Angry Re: Converting Function from VB.NET - C#

    Quote Originally Posted by jmcilhinney
    What's wrong is that references of type object cannot be implicitly converted to references of any other type because the compiler has no guarantee whatsoever that they will refer to an object of the specified type. I told you that you should pass objects of the specific type that you want to use. If you choose to ignore that advice then you need to explicitly convert the object references to the types you want using casts. By doing so you are telling the compiler that you do guarantee that the references will refer to objects of the type that you say. If the app crashes because they weren't then it's your fault and not the compiler's. Bascially it's bad practice to use a more general type than you intend to use without good reason. The fact that you don't want to rewrite the function to take objects of the correct types through two separate arrays, a keyed collection or your own custom type doesn't strike me as a good reason.

    @ Sunburnt: Thanks for the function. It's exactly How my function worked in VB.Net... Exept it was barebones....
    Thanks.I'll be usin it as what its meant to be... A learning tool.


    @jmcilhinney:
    I told you that you should pass objects of the specific type that you want to use. If you choose to ignore that advice then you need to explicitly convert the object references to the types you want using casts.
    I am still new to this [Desktop Programming]... So I didnt understand what you were saying.Because i didnt know what i had to do. Nore could i find any refrences to that on the web... where i learn this stuff... cuz atm i cant get "books"...Sorry.
    Last edited by PlaGuE; May 29th, 2006 at 02:30 AM.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Converting Function from VB.NET - C#

    If you are passing Color objects and only Color objects then your arguments should reflect that. If you're passing pairs of Color and String object sthen your arguments should refelect that. You'd have a number of choices. Two arguments of type Color[] and String[], one argument of type Dictionary(Of Color, String) or an array or collection of a custom type that has a Color property and a String property. All three of those ways will ensure that you get the correct types passed to your method in the correct ratios. The only thing you'd have to checkfor is that your Strings weren't null references.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width