Results 1 to 6 of 6

Thread: [RESOLVED] C# error in code

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2014
    Posts
    28

    Resolved [RESOLVED] C# error in code

    Hi Guys,

    I am trying to translate some functions from VBA to C# for a project but i got stuck on some error and I can't find anything on google to help me.

    The first error is in the part highlighted with bold. In the if statement i am trying to check : if ((first > 31) or (bytes.Length = 1)) then do stuff. But i get the error operand cannot be applied to types bool or int.
    Quote Originally Posted by code
    public string Decrypt(byte[] bytes)
    {
    int first, a,s,x,z;
    StringBuilder result;
    int position = 2;
    bool skip = false;

    if(bytes == null)
    {
    throw new ArgumentNullException("bytes");
    }
    if(bytes.Length == 0)
    {
    return string.Empty;
    }

    result = new StringBuilder(bytes.Length + 8);
    first = bytes[0];
    if ((first > 31) & (bytes.Length = 1))
    {
    foreach (byte item in bytes)
    {
    result.Append(Strings.Chr(item));
    }
    }
    else
    The second is is this: - the error is cannot assign to 'cript' because is a method group.
    Quote Originally Posted by code
    public string cript(string strToCript)
    {
    Int16[] ent = { 0, 10, 21, 31, 3, 14, 26, 24, 23, 16, 12, 8, 27, 17, 28, 1, 5, 2, 30, 7, 18, 29, 9, 4, 13, 6, 20, 19, 22, 25, 11, 15 };
    Int16[] usc = { 0, 11, 25, 22, 19, 20, 6, 13, 4, 9, 29, 18, 7, 30, 2, 1, 15, 28, 17, 27, 8, 12, 16, 23, 24, 26, 14, 3, 31, 21, 10, 5 };
    string c6 = Strings.Chr(216).ToString();
    string p = string.Empty;

    int x, z, r, a, s;
    string w2 = string.Empty;

    if (strToCript == string.Empty)
    {
    cript = string.Empty;
    return cript;

    }
    if(Strings.Asc(Strings.Mid(strToCript,1,1)) == 255)
    {
    cript = strToCript;
    return cript;

    }
    The 3rd and the last one is: - a new expression requires (),[] or{} after type.
    Quote Originally Posted by code
    Random random = new Random
    x = Convert.ToInt32(random.Next() * 1000);
    Do you guys have any idea how to solve this errors?

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Re: C# error in code

    Hello,

    See comments and code fixes below:

    Issue 1, equality operator in C# is "=="
    Csharp Code:
    1. if ((first > 31) & (bytes.Length == 1))  
    2.             {
    3.                 foreach (byte item in bytes)
    4.                 {
    5.                     result.Append(Strings.Chr(item));
    6.                 }
    7.             }
    8.             else
    9.             {
    10.             }

    Issue 2, your cannot use the method name "cript" in an assignment statement. Declare a new variable instead
    to hold the result.
    Csharp Code:
    1. string criptVal = string.Empty;
    2.             if (strToCript == string.Empty)
    3.             {
    4.                 criptVal = string.Empty;
    5.                 return criptVal;
    6.             }
    7.             if (Strings.Asc(Strings.Mid(strToCript, 1, 1)) == 255)
    8.             {
    9.                 criptVal = strToCript;
    10.                 return criptVal;
    11.             }

    Issue 3, you forgot to include the open/close parenthesis() when instantiating an object
    Csharp Code:
    1. Random random = new Random();
    2. x = Convert.ToInt32(random.Next() * 1000);

    - kgc
    Last edited by KGComputers; Jun 19th, 2016 at 10:52 AM.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2014
    Posts
    28

    Re: C# error in code

    Thank you KCG this solves all my problems

    However I have one more question.

    How come in VBA you are able to do this:
    Quote Originally Posted by codeVBA
    Public Shared Function cript(ByVal strToCript As String) As String

    Dim ent() As Int16 = {0, 10, 21, 31, 3, 14, 26, 24, 23, 16, 12, 8, 27, 17, 28, 1, 5, 2, 30, 7, 18, 29, 9, 4, 13, 6, 20, 19, 22, 25, 11, 15}
    Dim usc() As Int16 = {0, 11, 25, 22, 19, 20, 6, 13, 4, 9, 29, 18, 7, 30, 2, 1, 15, 28, 17, 27, 8, 12, 16, 23, 24, 26, 14, 3, 31, 21, 10, 5}
    Dim c6 As String = Chr(216)
    Dim p As String = String.Empty

    Dim x, z, r, a, s As Integer
    Dim w2 As String = String.Empty
    If strToCript = String.Empty Then cript = String.Empty : Exit Function
    If Asc(Mid$(strToCript, 1, 1)) = 255 Then cript = strToCript : Exit Function
    and in c# you are not allowed?

    Quote Originally Posted by code c#
    public string cript(string strToCript)
    {
    Int16[] ent = { 0, 10, 21, 31, 3, 14, 26, 24, 23, 16, 12, 8, 27, 17, 28, 1, 5, 2, 30, 7, 18, 29, 9, 4, 13, 6, 20, 19, 22, 25, 11, 15 };
    Int16[] usc = { 0, 11, 25, 22, 19, 20, 6, 13, 4, 9, 29, 18, 7, 30, 2, 1, 15, 28, 17, 27, 8, 12, 16, 23, 24, 26, 14, 3, 31, 21, 10, 5 };
    string c6 = Strings.Chr(216).ToString();
    string p = string.Empty;

    int x, z, r, a, s;
    string w2 = string.Empty;

    if (strToCript == string.Empty)
    {
    cript = string.Empty;
    return cript;
    }
    if(Strings.Asc(Strings.Mid(strToCript,1,1)) == 255)
    {
    cript = strToCript;
    return cript;
    }

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: C# error in code

    In short:
    C# != VBA

    Slightly longer:
    Because they are two completely different languages.

    Even longer:
    Because VBA is based on an older version of VB, where it was allowed, in fact at the time it was the only way to return a value from a function.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2014
    Posts
    28

    Re: C# error in code

    Thank you techgnome for the explanation and thank you KGComputers for the bug fixes.

    I have modified the code and now all seems fine.

  6. #6
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Re: [RESOLVED] C# error in code

    Glad I could help..
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

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