c# books

seek said:
He said C#, not C++ :confused:

Anyway, I have two books on C#; Beginning Visual C# 2005 and Programming C# 4th Edition. The former is much bigger (c. 1000 pages, compared to Programming C#'s 600 pages). To be honest, I found Programming C# to be a better book than than Beginning Visual C# 2005; it's more compact, but still covers pretty much everything that the first book does. It also focuses more on how you'd actually get things done in C#, although it covers the theory as well. It is more of a book for people who already know how to program, though, so if you're new to the whole thing, I'd go for Beginning Visual C#.
 
basically i am trying to convert a little c++ console app to a C# program with gui

how do i declare an array once so i can use with lots of textboxes which capture the different array elements (this make sense?)

here is code I am working on

i can get to work if i declare in each instance of textbox, but i know this is just wrong way to do this , any tips please?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace labels
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

private void textBox4_TextChanged(object sender, EventArgs e)
{
int FormulaTotal = 0;
FormulaTotal = int.Parse(textBox4.Text);
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
string RefID = textBox1.Text;
}

private void textBox2_TextChanged(object sender, EventArgs e)
{
string Epcode = textBox2.Text;
}

private void textBox3_TextChanged(object sender, EventArgs e)
{
string Colourist = textBox3.Text;
}

private void Form1_Load(object sender, EventArgs e)
{
string[] basics = new string[] { " ", " ", " ", " ", " ", " ", " ", " ", " " };
int [] Amounts =new int[] {0,0,0,0,0,0,0,0,0};


}

private void textBox5_TextChanged(object sender, EventArgs e)
{
string[] basics = new string[9] ;
basics[0] = textBox5.Text;


}

private void textBox6_TextChanged(object sender, EventArgs e)
{


}


}
}
 
Nope, this one:
http://www.amazon.co.uk/gp/product/0764578472/202-1364628-1790251?v=glance&n=266239

However, if you already know C++, then Programming C# would probably be a better choice:
http://www.amazon.co.uk/gp/product/0596006993/202-1364628-1790251?v=glance&n=266239

As for the C# application, you need to declare the array as a field of the Form1 class, otherwise it won't be accessible outside the scope of the event handler:
Code:
public class Form1
{
    private string[] m_Basics = new string[9];

    // rest of the class here...
}

Then in each of the event handlers:
Code:
m_Basics[n] = textBox.Text;
... where n is the key to which you want to assign the value.

You might want to look at the Dictionary class as well, which might be better suited for this.
 
my knowledge of c++ is basics at best, just what I have taught myself .


he is C++ code I'm trying to convert

#include <iostream>

using namespace std;

void label ();

int main ()
{

float total;
int numberOfBasics;
int basicArray [8];
float amountArray [] = {0,0,0,0,0,0,0,0,0};
float endAmount [] = {0,0,0,0,0,0,0,0,0};
cout << "Please enter the number of basics: ";
cin >> numberOfBasics;
cout <<"please enter the formula total: ";
float FormulaTotal=0;
cin >> FormulaTotal;
int x = 0;

while (x < numberOfBasics)
{
int x=0;
cout <<"Enter basic: ";
cin >> basicArray [x]; // get recipe
cout <<"Enter amount: ";
cin >> amountArray [x];
++x;
}

total = amountArray[0]+ amountArray[1] + amountArray[2]
+ amountArray[3] +amountArray[4]+amountArray[5]
+amountArray[6]+amountArray[7]+amountArray[8];
x=0;
while (x < numberOfBasics)
{
x=0;
float formTotal;
endAmount[x] = amountArray[x]/total*formTotal;
++x;
}
cout<< "\n\n\nFormula\n";

x=0;

while (x < numberOfBasics)
{
x=0;
cout<< basicArray[x];
cout<< " : ";
cout<< endAmount[x];
cout<<"\n";
++x;
}

char quit[6];
cin >> quit;
return 1;
}
 
Last edited:
Sorry to sound harsh, but does that C++ program actually work? :confused:

You seem to be doing some pretty odd things, for example resetting the value of x to 0 at the beginning of each iteration in your loops. This would make them infinite loops.

Also, you're redeclaring x within the first loop. I don't know C++ that well, but surely that shouldn't actually compile?
 
yes it compiles and does what i want it too, probably isn't coded very well mind you :eek:


i am resetting the value of x so that the loop only runs equal to the amount if basics selected at start. I just put this together with what I could remember from when I went to college.
 
Last edited:
As inquisitor says,

while (x < numberOfBasics)
{
int x=0;
cout <<"Enter basic: ";
cin >> basicArray [x]; // get recipe
cout <<"Enter amount: ";
cin >> amountArray [x];
++x;
}

Will infinate loop... but yeah it will compile. Very poor code.
 
yeah thats a start.. would rewrite it for you if I knew what you were trying to do in plain english :P Things like
float amountArray [] = {0,0,0,0,0,0,0,0,0};
float endAmount [] = {0,0,0,0,0,0,0,0,0};

Are obviously gonna limit you to 9 inputs.
 
Last edited:
C++ won't complain about a redeclared variable in nested scopes, however it will complain if the variable is redeclared in a normal scope. It has lots of little querks like this :P (Or rarther java/C# scoping rules are different)
This is wrong also,

float formTotal; // You redeclared formTotal but with no value (I.e undefined behaviour when you use it next).
endAmount[x] = amountArray[x]/total*formTotal;

Instead of manually adding each item of the array to a total you can just use something like,

amountTotal += amountArray[x]; in your loop.

You can also dynamically allocate the memory needed to hold the arrays.
 
Last edited:
Una said:
yeah thats a start.. would rewrite it for you if I knew what you were trying to do in plain english :P Things like
float amountArray [] = {0,0,0,0,0,0,0,0,0};
float endAmount [] = {0,0,0,0,0,0,0,0,0};

Are obviously gonna limit you to 9 inputs.

basically I want to enter up to 9 basics(ingredients) with 9 values(in grams to 1 decimal palace. and have a the output averaged down to the total(normall 100grams) ideally i want to be able to format output and print.
 
Back
Top Bottom