Friday, July 31, 2009

RE: Writing C# in Microsoft Studio? (with code)?

Ok, so I have code included this time. (it took me 5 min to write it in VB but no luck with C#, and for the person who said you can't write C# in Visual Studio...you can, but thanks)





This is the VB code:











Public Class Form1





Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


Dim radius As String


Dim area As String


Dim circumfrence As String


Dim diameter As String


Dim PI As String





PI = 3.1428


radius = TextBox1.Text


area = PI * radius * radius


circumfrence = 2 * PI * radius


diameter = radius * 2





Label3.Text = area


Label5.Text = diameter


Label7.Text = circumfrence








End Sub





End Class











I can set up the code in C# (this is for a windows application by the way) but it is being very stubborn in letting me take a users input and using it as a number. Thanks for any help! =)

RE: Writing C# in Microsoft Studio? (with code)?
Once you get the hang of it, C# is just as easy as VB


I've free-handing this, so I'm not sure it's going to be perfect..


I don't quite understand why you use strings for everything... numbers would be easier...





I use double.Parse() in here. this assumes you have entered good data. Look up TryParse to test your user's input...





Public Class Form1





Private Sub Button1_Click(ByVal sender System.Object, ByVal e As System.EventArgs) Handles Button1.Click


double radius;


double area;


double circumfrence;


double diameter;


double PI;





PI = double.Parse(3.1428); //can't remember if you need to parse this


radius = double.Parse(TextBox1.Text);


area = PI * radius * radius


circumfrence = double.parse(2) * PI * radius


diameter = radius * double.parse(2)





Label3.Text = area.ToString();


Label5.Text = diameter.ToString();


Label7.Text = circumfrence.ToString();








End Sub





End Class








That's the gist. Use the Parse method... I dont' have VS in front of me and can't remember if you have to .Parse those constants, but it will still work if you do it.

dried flowers

No comments:

Post a Comment