Solved

C# Problem convertion from vb6

Posted on 2012-12-30
4
449 Views
Last Modified: 2012-12-30
I try to translate a VB6 proc to c# and can't !! (I'm a C# beginner)
I have troubles with the variables types and the translation from the VB6 "&"  to the C# "||"
Could an expert help me
Here comes the original VB6 code and my "translation"

The original VB6 proc

Private Sub GetStatusBits()

    Dim Digit As Integer
	Dim nSpO2_StatusBit(7) As Integer   'used for DF1 also
	im nSpO2_Status As Integer 'Use for DF1 also
	Dim nStat2 As Integer
    
    'CONVERT SPO2 STATUS BYTE TO BINARY BYTE ARRAY
    For Digit = 7 To 0 Step -1
        If (nSpO2_Status And (2 ^ Digit)) Then
            nSpO2_StatusBit(Digit) = 1
        Else
            nSpO2_StatusBit(Digit) = 0
        End If
        If (nStat2 And (2 ^ Digit)) Then
            nStat2Bit(Digit) = 1
        Else
            nStat2Bit(Digit) = 0
        End If
    Next Digit

End Sub

Open in new window


My C# translation
private void GetStatusBits()
	{
		int Digit = 0;

		//CONVERT SPO2 STATUS BYTE TO BINARY BYTE ARRAY
		for (Digit = 7; Digit >= 0; Digit += -1) 
                {
	        if ((nSpO2_Status )& (Math.Pow(2, Digit))))  
                {
				nSpO2_StatusBit[Digit] = 1;
			    } 
            else 
                {
				nSpO2_StatusBit[Digit] = 0;
			    }
			if ((nStat2 = 0) // & (Math.Pow(2, Digit)))) 
                {
				nStat2Bit[Digit] = 1;
			   }
               else 
                {
				nStat2Bit[Digit] = 0;
			    }
		}
	}

Open in new window

0
Comment
Question by:luciepaul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38731170
Where is the declaration for nStat2Bit in your original VB6 code?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38731174
This is contingent on the declaration of nStat2Bit, but I believe the translation is accurate:

private void GetStatusBits()
{
    int Digit = 0;
    int[] nSpO2_StatusBit = new int[7];
    int nSpO2_Status;
    int nStat2;

    //CONVERT SPO2 STATUS BYTE TO BINARY BYTE ARRAY
    for (Digit = 7; Digit >= 0; Digit += -1)
    {
        if (((nSpO2_Status) & ((int)Math.Pow(2, Digit))) != 0)
        {
            nSpO2_StatusBit[Digit] = 1;
        }
        else
        {
            nSpO2_StatusBit[Digit] = 0;
        }

        if ((nStat2 & ((int)Math.Pow(2, Digit))) != 0)
        {
            nStat2Bit[Digit] = 1;
        }
        else
        {
            nStat2Bit[Digit] = 0;
        }
    }
}

Open in new window

0
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 38731176
Hi,
Please explain the error and problems, but what i have conceived is as following:

First of all you have not declared nSpO2_StatusBit in your c# code.
Secondly in VB nSpO2_StatusBit(7) means you have 8 element in array from 0 to 7. On the other hand you have only 7 elements in c# from 0 to 6.

Thanks
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38731194
Be sure to make the change that IJZ mentioned. Change line 4 to:

int[] nSpO2_StatusBit = new int[8];

Open in new window


I always seem to forget that little detail when translating VB to C#  : )
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question