|
-
Jun 19th, 2016, 09:07 AM
#1
Thread Starter
Junior Member
[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.
 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.
 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.
 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?
-
Jun 19th, 2016, 10:48 AM
#2
Re: C# error in code
Hello,
See comments and code fixes below:
Issue 1, equality operator in C# is "=="
Csharp Code:
if ((first > 31) & (bytes.Length == 1)) { foreach (byte item in bytes) { result.Append(Strings.Chr(item)); } } else { }
Issue 2, your cannot use the method name "cript" in an assignment statement. Declare a new variable instead
to hold the result.
Csharp Code:
string criptVal = string.Empty; if (strToCript == string.Empty) { criptVal = string.Empty; return criptVal; } if (Strings.Asc(Strings.Mid(strToCript, 1, 1)) == 255) { criptVal = strToCript; return criptVal; }
Issue 3, you forgot to include the open/close parenthesis() when instantiating an object
Csharp Code:
Random random = new Random(); x = Convert.ToInt32(random.Next() * 1000);
- kgc
Last edited by KGComputers; Jun 19th, 2016 at 10:52 AM.
-
Jun 20th, 2016, 06:50 AM
#3
Thread Starter
Junior Member
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:
 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?
 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;
}
-
Jun 20th, 2016, 07:01 AM
#4
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
-
Jun 20th, 2016, 10:15 AM
#5
Thread Starter
Junior Member
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.
-
Jun 20th, 2016, 10:47 AM
#6
Re: [RESOLVED] C# error in code
Glad I could help..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|