1

1

Someone has provided me with example code and it's not written in the language I understand. Is it possible to convert code between C# and VB.NET?

For example, I have been given this code in Visual Basic (credit: Peter Simpson):

Public Class ChooserExamples
    Sub Example()
        Dim Ch As ASCOM.Utilities.Chooser
        Dim SelectedDevice As String

        'Create a new chooser and set the required device type
        Ch = New ASCOM.Utilities.Chooser ' Create a new chooser component and set its device type
        Ch.DeviceType = "Telescope"

        'Select the required device - use one of:
        SelectedDevice = Ch.Choose 'Show the chooser dialogue with no device or with your device pre-selected
        SelectedDevice = Ch.Choose("Simulator.Telescope")

        MsgBox("The selected device is: " & SelectedDevice)

        'Clean up chooser object
        Ch.Dispose()
        Ch = Nothing
    End Sub
End Class

What is the equivalent in C#?

flag

4 Answers

3

Go to Telerik Code Converter and paste in your code sample. The converter will convert the code to your preferred language.

Here is the above sample converted to C#:

public class ChooserExamples
{
    void Example()
    {
    	ASCOM.Utilities.Chooser Ch;
    	string SelectedDevice;

    	//Create a new chooser and set the required device type
    	Ch = new ASCOM.Utilities.Chooser();
    	// Create a new chooser component and set its device type
    	Ch.DeviceType = "Telescope";

    	//Select the required device - use one of:
    	SelectedDevice = Ch.Choose;
    	//Show the chooser dialogue with no device or with your device pre-selected
    	SelectedDevice = Ch.Choose("Simulator.Telescope");

    	MsgBox("The selected device is: " + SelectedDevice);

    	//Clean up chooser object
    	Ch.Dispose();
    	Ch = null;
    }
}

//=======================================================
//Service provided by Telerik (www.telerik.com)
//Conversion powered by NRefactory.
//Built and maintained by Todd Anglin and Telerik
//=======================================================

Easy, isn't it?

It works both ways, C# to VB as well.

link|flag
Note that the Telerik converter doesn't work for 'unsafe' code. – Tim Long Jan 5 at 18:53
0

From what is posted I would guess that the code needs to be better formatted so it is split in to separate lines.

Maybe the line separators are not what is expected.

link|flag
0

That code is 'orrible! and I'm not sure what it has to do with Astronomy?

public class Test


   {

        public void EPostaYolla(string sGonderen, string aAlanlarTO, string aAlanlarCC, string aAlanlarBCC, string aEklentiler, string sKonu, string mMesaj, MesajYollamaSekli bEPostaMotoru)
        {
            string sBodyTurkceGiris = "";

            if ((string.IsNullOrEmpty(sGonderen)) | (string.IsNullOrEmpty(aAlanlarTO)) | (string.IsNullOrEmpty(sKonu)) | (string.IsNullOrEmpty(mMesaj)) | (bEPostaMotoru <= 0))
                return;

            System.Net.Mail.SmtpClient obMesajMotoru = default(System.Net.Mail.SmtpClient);
            System.Net.Mail.MailMessage obMesaj = default(System.Net.Mail.MailMessage);
            string[] arAlanlarTO = null;
            string[] arAlanlarCC = null;
            string[] arAlanlarBCC = null;
            string[] arEklentiler = null;
            int iSayac = 0;
            string sSMTPSunucu = VeriDondur(UygulamaAyariAl("Bulten SMTPSunucu"), VeriTipi.Metin);
            string sSMTPKullaniciAdi = VeriDondur(UygulamaAyariAl("Bulten SMTP Kullanici Adi"), VeriTipi.Metin);
            //Dim sSMTPSifre As String = VeriDondur(UygulamaAyariAl("Bulten SMTPSifre"), VeriTipi.Metin)
            int iSMTPPortNo = VeriDondur(UygulamaAyariAl("BultenSMTP Port"), VeriTipi.TamSayi);
            string sSMTPSunucu = My.Settings.SMTPSunucusu;
            string sSMTPSunucu = My.Settings.SMTPSunucusu;
            string sSMTPSifre = My.Settings.SMTPSifre;
            int iSMTPPortNo = My.Settings.SMTPPortNo;

            arAlanlarTO = Strings.Split(aAlanlarTO, ";");
            arAlanlarCC = Strings.Split(aAlanlarCC, ";");
            arAlanlarBCC = Strings.Split(aAlanlarBCC, ";");
            arEklentiler = Strings.Split(aEklentiler, ";");

            switch (bEPostaMotoru) {


                case MesajYollamaSekli.OturumAcarak:
                    obMesajMotoru = new System.Net.Mail.SmtpClient();
                    System.Net.NetworkCredential ncHesap = new System.Net.NetworkCredential(sSMTPKullaniciAdi, sSMTPSifre);
                    obMesajMotoru.Host = sSMTPSunucu;
                    obMesajMotoru.Port = iSMTPPortNo;
                    obMesajMotoru.UseDefaultCredentials = false;
                    obMesajMotoru.Credentials = ncHesap;
                    obMesaj = new System.Net.Mail.MailMessage();

                    obMesaj.From = new System.Net.Mail.MailAddress(sGonderen);
                    for (iSayac = 0; iSayac <= Information.UBound(arAlanlarTO); iSayac++) {
                        if ((arAlanlarTO(iSayac) > "") & (AdresKontrol(arAlanlarTO(iSayac)) == arAlanlarTO(iSayac))) {
                            obMesaj.To.Add(new System.Net.Mail.MailAddress(arAlanlarTO(iSayac)));
                        }
                    }

                    for (iSayac = 0; iSayac <= Information.UBound(arAlanlarCC); iSayac++) {
                        if ((arAlanlarCC(iSayac) > "") & (AdresKontrol(arAlanlarCC(iSayac)) == arAlanlarCC(iSayac))) {
                            obMesaj.CC.Add(new System.Net.Mail.MailAddress(arAlanlarCC(iSayac)));
                        }
                    }

                    for (iSayac = 0; iSayac <= Information.UBound(arAlanlarBCC); iSayac++) {
                        if ((arAlanlarBCC(iSayac) > "") & (AdresKontrol(arAlanlarBCC(iSayac)) == arAlanlarBCC(iSayac))) {
                            obMesaj.Bcc.Add(new System.Net.Mail.MailAddress(arAlanlarBCC(iSayac)));
                        }
                    }

                    obMesaj.Subject = sKonu;
                    obMesaj.Body = sBodyTurkceGiris + mMesaj;
                    if (!string.IsNullOrEmpty(aEklentiler)) {
                        for (iSayac = 0; iSayac <= Information.UBound(arEklentiler); iSayac++) {
                            if (DosyaAdiDondur(arEklentiler(iSayac)) > "") {
                                obMesaj.Attachments.Add(NewSystem.Net.Mail.Attachment(HttpContext.Current.Server.MapPath(arEklentiler(iSayac))));
                            }
                        }
                    }
                    obMesaj.IsBodyHtml = true;
                    obMesajMotoru.Send(obMesaj);
                    obMesaj.Dispose();
                    obMesajMotoru = null;
                    break;
            }
        }

        public enum MesajYollamaSekli
        {
            OturumAcarak = 1,
            OturumAcmadan = 2
        }
    }
link|flag
-1

i couldn't to that with telerik converter how can i conver this vb codes to c#?

Gelen mesaji, belirtilen adresten, belirtilern adreslere, belirtilentipte e-posta motoru kullanarak yollar Gönderen adres Alan adres(ler) Karbon kopya yollanacak adres(ler) Gizli karbon kopya yollanacakadres(ler) Eklenecek dokümanların göreceliadresleri Mesajın başlığı Mesajın içeriği (HTML) Kullanılacak eposta motoru (1: OturumAçılan, 2: Direkt Yollanan)

Sub EPostaYolla(ByV al sGonderen As String, ByVal aAlanlarTO As String,ByVal aAlanlarCC As String, ByVal aAlanlarBCC As String, ByVal aEklentilerAs String, ByVal sKonu As String, ByVal mMesaj As String, ByValbEPostaMotoru As MesajYollamaSekli) Dim sBodyTurkceGiris As String = ""

if (sGonderen = "") Or (aAlanlarTO = "") Or (sKonu = "") Or (mMesaj= "") Or (bEPostaMotoru <= 0) Then Exit Sub

Dim obMesajMotoru As System.Net.Mail.SmtpClient, obMesaj AsSystem.Net.Mail.MailMessage Dim arAlanlarTO() As String, arAlanlarCC() As String, arAlanlarBCC() As String, arEklentiler() As String Dim iSayac As Integer Dim sSMTPSunucu As String = VeriDondur(UygulamaAyariAl("Bulten SMTPSunucu"), VeriTipi.Metin) Dim sSMTPKullaniciAdi As String =VeriDondur(UygulamaAyariAl("Bulten SMTP Kullanici Adi"), VeriTipi.Metin)'Dim sSMTPSifre As String = VeriDondur(UygulamaAyariAl("Bulten SMTPSifre"), VeriTipi.Metin) Dim iSMTPPortNo As Integer = VeriDondur(UygulamaAyariAl("BultenSMTP Port"), VeriTipi.TamSayi) Dim sSMTPSunucu As String = My.Settings.SMTPSunucusu Dim sSMTPSunucu As String = My.Settings.SMTPSunucusu Dim sSMTPSifre As String = My.Settings.SMTPSifre Dim iSMTPPortNo As Integer = My.Settings.SMTPPortNo

arAlanlarTO = Split(aAlanlarTO, ";") arAlanlarCC = Split(aAlanlarCC, ";") arAlanlarBCC = Split(aAlanlarBCC, ";") arEklentiler = Split(aEklentiler, ";")

Select Case bEPostaMotoru

'''''''''''''''''''''''' CDO - Oturum Acarak ''''''''''''''''''''''''

Case MesajYollamaSekli.OturumAcarak obMesajMotoru = New System.Net.Mail.SmtpClient() Dim ncHesap As New System.Net.NetworkCredential(sSMTPKullaniciAdi, sSMTPSifre) obMesajMotoru.Host = sSMTPSunucu obMesajMotoru.Port = iSMTPPortNo obMesajMotoru.UseDefaultCredentials = False obMesajMotoru.Credentials = ncHesap obMesaj = New System.Net.Mail.MailMessage

obMesaj.From = New System.Net.Mail.MailAddress(sGonderen) For iSayac = 0 To UBound(arAlanlarTO) If (arAlanlarTO(iSayac) > "") And(AdresKontrol(arAlanlarTO(iSayac)) = arAlanlarTO(iSayac)) ThenobMesaj.To.Add(New System.Net.Mail.MailAddress(arAlanlarTO(iSayac))) Next For iSayac = 0 To UBound(arAlanlarCC) If (arAlanlarCC(iSayac) > "") And(AdresKontrol(arAlanlarCC(iSayac)) = arAlanlarCC(iSayac)) ThenobMesaj.CC.Add(New System.Net.Mail.MailAddress(arAlanlarCC(iSayac))) Next For iSayac = 0 To UBound(arAlanlarBCC) If (arAlanlarBCC(iSayac) > "") And(AdresKontrol(arAlanlarBCC(iSayac)) = arAlanlarBCC(iSayac)) ThenobMesaj.Bcc.Add(New System.Net.Mail.MailAddress(arAlanlarBCC(iSayac))) Next obMesaj.Subject = sKonu obMesaj.Body = sBodyTurkceGiris & mMesaj If aEklentiler <> "" ThenFor iSayac = 0 To UBound(arEklentiler) If DosyaAdiDondur(arEklentiler(iSayac)) > "" ThenobMesaj.Attachments.Add(NewSystem.Net.Mail.Attachment(HttpContext.Current.Server.MapPath(arEklentiler(iSayac)))) Next End If obMesaj.IsBodyHtml = True obMesajMotoru.Send(obMesaj) obMesaj.Dispose()
obMesajMotoru = Nothing End Select End Sub

Public Enum MesajYollamaSekli OturumAcarak = 1 OturumAcmadan = 2 End Enum

link|flag
-1 because this is not an answer. If you want to ask a new question, please create a new question. – Tim Long Jul 13 at 22:24

Your Answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.