Convert VB code to C#

MadIce
MadIce used Ask the Experts™
on
Trying to convert VB code to C#. Never really worked with C#. It's just taking a pulling the information from a JSON file so I can upload to a database. Have in VB but want to switch to C#. Below is the VB code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim filePath As String =
            IO.Path.Combine(_desktop, "Upload.txt")

        Dim data As Upload =
            JsonConvert.DeserializeObject(Of Upload)(IO.File.ReadAllText(filePath))
End Sub
Public Class Upload
        Public Property Primaryimage() As WebImage
        Public Property Secondaryimages() As List(Of WebImage)
        Public Property Notes() As List(Of String)
        Public Property SafetyPrecautions() As SafetyPrecautions
End Class
Class WebImage
        Public Property Alt() As String
        Public Property Src() As String
End Class
Public Class SafetyPrecautions
        Public Property Warnings() As List(Of String)
End Class

Open in new window


Might be all wrong but this is what I have for C#

public partial class frmProcessJsonFile : Form
    {
        private string _desktop;
        public frmProcessJsonFile()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string filePath = System.IO.Path.Combine(_desktop, "Upload.txt");

            Upload data = JsonConvert.DeserializeObject<Upload>(System.IO.File.ReadAllText(filePath));
                  }

        public class Upload
            {
             public IEnumerable<WebImage> Primaryimage { get; set; }
              
    }

        class WebImage
        {
            public string Alt { get; set; }
            public string Src { get; set; }
        }
    }

Open in new window


Any help would be appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
There are online converters that will do this for you:



e.g.
class SurroundingClass
{
    private void Form1_Load(object sender, EventArgs e)
    {
        string filePath = System.IO.Path.Combine(_desktop, "Upload.txt");

        Upload data = JsonConvert.DeserializeObject<Upload>(System.IO.File.ReadAllText(filePath));
    }

    public class Upload
    {
        public WebImage Primaryimage { get; set; }
        public List<WebImage> Secondaryimages { get; set; }
        public List<string> Notes { get; set; }
        public SafetyPrecautions SafetyPrecautions { get; set; }
    }

    class WebImage
    {
        public string Alt { get; set; }
        public string Src { get; set; }
    }

    public class SafetyPrecautions
    {
        public List<string> Warnings { get; set; }
    }
}

Open in new window

Commented:
Pretty close:

Form1.cs -
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace EE_Q29143073
{
    public partial class Form1 : Form
    {
        const string _desktop = "somepath should be here";
        public Form1()
        {
            InitializeComponent();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            var filePath = Path.Combine(_desktop, "Upload.txt");
            var data = JsonConvert.DeserializeObject<Upload>(File.ReadAllText(filePath));
        }
    }

    class Upload
    {
        public WebImage PrimaryImage { get; set; }
        public List<WebImage> SecondaryImages { get; set; }
        public List<string> Notes { get; set; }
        public SafetyPrecautions SafetyPrecautions { get; set; }
    }

    class WebImage
    {
        public string Alt { get; set; }
        public string Src { get; set; }
    }

    class SafetyPrecautions
    {
        public List<string> Warnings { get; set; }
    }
}

Open in new window

Form1.Designer.cs -
namespace EE_Q29143073
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.OnLoad);
            this.ResumeLayout(false);

        }

        #endregion
    }
}

Open in new window

-saige-

Commented:
Or as Kaufmed stated...  ;)

-saige-
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Author

Commented:
Thank you. So then this is why I thought I was off. See attached.Inconsistent accessibility error

Author

Commented:
Oh, needed to make class to be public. I think this will work but still reviewing.

Author

Commented:
Thanks for the help. It also fixed my overall problem and why I had to switch to C#.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial