Jump to content

Could someone explain this code to me? How to put it into C#?

Hey everybody,

 

I received this piece of VB code as advice on how to write my "Save button" event for my program...

 Dim sbDA As New System.Text.StringBuilder        For Index As Integer = 0 To My.Forms.Form2.clbDiscA.Items.Count - 1            sbDA.AppendLine(String.Format("{0},{1}",                    If(My.Forms.Form2.clbDiscA.GetItemChecked(Index), True, False), My.Forms.Form2.clbDiscA.Items(Index)))        Next        IO.File.WriteAllText("C:\Users\Pikey10\Documents\Visual Studio 2013\Projects\Planner3\Read Write\clbDiscA.txt", sbDA.ToString) 

It was used to write the boxes checked and unchecked of a checklistbox to text file on my harddrive.

 

Could someone explain to me what the different parts mean?

 

So I understand StringBuilder collects strings in a control....

 

I don't quite understand his use of Index as integer = 0 To My.Forms.Form2....Count - 1

Is this setting up sbDA.AppendLine to read through lines of code starting from zero to infinity? (zero to -1?)

 

The String.Format ("{0},{1}" parts looks like a Boolean situation?  True = 0, False = 1?

 

The rest of it I think I sort of get.

 

Bonus points to anyone who can help me convert this to C#! :D

 

Thanks!

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah, there is quite a bit else I would have to translate across, as well - accessing controls in other forms is what I am working on now...how appendline and stringbuilder translate....yikes! :P 

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

Here's a couple basics

Dim variableName As ClassName()// becomesClassName variableName = new ClassName();//orvar variableName = new ClassName();

Here's an example using your StringBuilder

Dim sbDA As New System.Text.StringBuilder// becomesSystem.Text.StringBuilder sbDA = new System.Text.StringBuilder();// or simplyvar sbDA = new System.Text.StringBuilder();

Another tip is to use Imports (vb) or using (C#) above your class so you don't have to type out the full class namespace.

// VBImports System.TextPublic Class Example    Public Sub New()        Dim sbDA As New StringBuilder // // Since you're importing the namespace above, you don't need to type System.Text every time    End SubEnd Class// C#using System.Text;public class Example{    public Example() // This is a constructor in C# and is used instead of the Sub New() in VB    {        var sbDA = new StringBuilder(); // Since you're using the namespace above, you don't need to type System.Text every time    }}

C# doesn't have My.Forms so you'll have to access your CheckedListBox in another way.

Link to comment
Share on other sites

Link to post
Share on other sites

Here's a couple basics

Dim variableName As ClassName()// becomesClassName variableName = new ClassName();//orvar variableName = new ClassName();

Here's an example using your StringBuilder

Dim sbDA As New System.Text.StringBuilder// becomesSystem.Text.StringBuilder sbDA = new System.Text.StringBuilder();// or simplyvar sbDA = new System.Text.StringBuilder();

Another tip is to use Imports (vb) or using (C#) above your class so you don't have to type out the full class namespace.

// VBImports System.TextPublic Class Example    Public Sub New()        Dim sbDA As New StringBuilder // // Since you're importing the namespace above, you don't need to type System.Text every time    End SubEnd Class// C#using System.Text;public class Example{    public Example() // This is a constructor in C# and is used instead of the Sub New() in VB    {        var sbDA = new StringBuilder(); // Since you're using the namespace above, you don't need to type System.Text every time    }}

C# doesn't have My.Forms so you'll have to access your CheckedListBox in another way.

 

 

I like C# so far - it seems cleaner in some ways than Visual Basic.  Like assigning variables seems more straightforward

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah. Just for clarification, the var keyword in C# is explained here.

 

Normally I use it instead of the typed name when it's obvious to the reader.

var name = "berderder"; // this is obviously a string so I use varstring name = GetName(); // it's not obvious on first glance what type GetName() returns, so I don't use var
Link to comment
Share on other sites

Link to post
Share on other sites

 

Yeah. Just for clarification, the var keyword in C# is explained here.

 

Normally I use it instead of the typed name when it's obvious to the reader.

var name = "berderder"; // this is obviously a string so I use varstring name = GetName(); // it's not obvious on first glance what type GetName() returns, so I don't use var

This is all great, thanks...I am using the posts on here for reference

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×