locked
Convert VB.NET Code to C#.NET RRS feed

  • Question

  • i am trying to convert VB.NET code to C#.net as im not a VB dev i am having problem in converting the following code into c#, can someone translate it to me, i shall be really thankful to you guys,

    Public thReadUSB As New Threading.Thread(AddressOf ReadThread); Private Sub ReadThread(ByVal params As Object) 'was being used to stop the read thread. Just leaving it alone for now. boolStopRead = False tRead = New Timers.Timer tRead.Interval = 500 tRead.Start() Dim tParams(0) As Object tParams(0) = params delReadUSB.DynamicInvoke(params) End Sub Private delReadUSB As New ReadUSB(AddressOf ReadUSBThread); Public Sub ReadUSBThread(ByRef sr As StreamReader) Try If sr IsNot Nothing AndAlso sr.BaseStream IsNot Nothing Then '*** End of stream doesn't seem to work for USB. Do '*** sr.ReadLine will hang when the printer doesn't communicate back. Result += sr.ReadLine & vbCr 'Loop While boolStopRead = False Loop While sr.EndOfStream = False End If Catch ex As Exception sr.BaseStream.Flush() sr.BaseStream.Close() sr.Close() End Try End Sub

    Monday, August 8, 2016 8:35 AM

Answers

  • Hi KhawajaHassan,

    >>Every Thing is converted fine by conveter online but these two lines:

    Because these two lines have semicolon(;), which is not a VB sytax. please remove it and try to convert again.

    public Threading.Thread thReadUSB = new Threading.Thread(ReadThread);
    
    private void ReadThread(object @params)
    {
    	//was being used to stop the read thread. Just leaving it alone for now.
    	boolStopRead = false;
    
    	tRead = new Timers.Timer();
    	tRead.Interval = 500;
    	tRead.Start();
    	object[] tParams = new object[1];
    	tParams(0) = @params;
    
    	delReadUSB.DynamicInvoke(@params);
    
    }
    
    
    private ReadUSB delReadUSB = new ReadUSB(ReadUSBThread);
    public void ReadUSBThread(ref StreamReader sr)
    {
    	try {
    		if (sr != null && sr.BaseStream != null) {
    			//*** End of stream doesn't seem to work for USB.
    			do {
    				//*** sr.ReadLine will hang when the printer doesn't communicate back.
    				Result += sr.ReadLine + Constants.vbCr;
    
    				//Loop While boolStopRead = False
    			} while (sr.EndOfStream == false);
    
    		}
    
    	} catch (Exception ex) {
    		sr.BaseStream.Flush();
    		sr.BaseStream.Close();
    		sr.Close();
    	}
    }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 9, 2016 2:14 AM

All replies

  • Every Thing is converted fine by conveter online but these two lines:

    public System.Threading.Thread thReadUSB = new System.Threading.Thread(ReadThread);

    private ReadUSB delReadUSB = new System.Threading.Thread(ReadUSBThread));

    They show error as : A field initializer cannot reference to a non-static field, property, method

    Monday, August 8, 2016 8:51 AM
  • Hi KhawajaHassan,

    >>Every Thing is converted fine by conveter online but these two lines:

    Because these two lines have semicolon(;), which is not a VB sytax. please remove it and try to convert again.

    public Threading.Thread thReadUSB = new Threading.Thread(ReadThread);
    
    private void ReadThread(object @params)
    {
    	//was being used to stop the read thread. Just leaving it alone for now.
    	boolStopRead = false;
    
    	tRead = new Timers.Timer();
    	tRead.Interval = 500;
    	tRead.Start();
    	object[] tParams = new object[1];
    	tParams(0) = @params;
    
    	delReadUSB.DynamicInvoke(@params);
    
    }
    
    
    private ReadUSB delReadUSB = new ReadUSB(ReadUSBThread);
    public void ReadUSBThread(ref StreamReader sr)
    {
    	try {
    		if (sr != null && sr.BaseStream != null) {
    			//*** End of stream doesn't seem to work for USB.
    			do {
    				//*** sr.ReadLine will hang when the printer doesn't communicate back.
    				Result += sr.ReadLine + Constants.vbCr;
    
    				//Loop While boolStopRead = False
    			} while (sr.EndOfStream == false);
    
    		}
    
    	} catch (Exception ex) {
    		sr.BaseStream.Flush();
    		sr.BaseStream.Close();
    		sr.Close();
    	}
    }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 9, 2016 2:14 AM
  • In C# Language Specification version 5.0, this restriction is in §10.5.5.2 (Instance field initialization): "A variable initializer for an instance field cannot reference the instance being created." Thus, code like the following causes error CS0236:

    public class Class1
    {
        private System.Action action = new System.Action(this.Fun);
    
        private void Fun()
        {
        }
    }

    Instead, you can make the constructor assign the field:

    public class Class1
    {
        private System.Action action;
    
        public Class1()
        {
            this.action = new System.Action(this.Fun);
        }
    
        private void Fun()
        {
        }
    }

    If your class has multiple constructors, then make sure each of them assigns the field.

    Wednesday, August 10, 2016 6:28 PM