|
To store the values to the database you would for example:
- create a SqlConnection[^]
- create a SqlCommand[^] and set INSERT INTO... statement to it
- add SqlParameters[^] to the SqlCommand.Parameters collection for your values
- open the connection
- create a loop where you set the parameter values and then execute the command
- close the connection
|
|
|
|
|
Hello
I need some help writing a couple of classes in C#.
One of them should encapsulate a point in a grid and the other one has a method to get 2 points with different X values and calculate the slope.
I'm quite lost since I haven't done any programming for over 2 years and my apologies if this doesn't make any sense.
here's what I got so far with the first class, it looks sketchy to me.
class Pointless
{
private int X, Y;
public Pointless(int nullx, int nully)
{
X = nullx;
Y = nully;
}
public void setX(int i)
{
i = 2;
X = i;
}
public void setY(int b)
{
b = -4;
Y = b;
}
public int getX()
{
return X;
}
public int getY()
{
return Y;
}
}
|
|
|
|
|
Can't you use two System.Drawing.PointF s and calculate the slope from that?
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
|
What's the purpose of the i = 2; and b = -4; ?
You might want to use properties rather than methods (automatic properties even).
You may even just want the members (X and Y) to be public, and perhaps readonly.
And have you considered using a struct instead?
|
|
|
|
|
Well the i and b were supposed to set the values of X and Y. Like I said I'm very rusty and have been scratching my head over this all day and feel like I'm moving backwards by now.
Yes properties seem much cleaner
Is this right?
public int pointX
{
get
{
return X;
}
set
{
X = 2;
}
}
|
|
|
|
|
X = value; would likely work better.
|
|
|
|
|
Two points that differ only on the X axis have a slope of 0.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|
Good, we can check the result of the calculation against a known value.
|
|
|
|
|
what do you think of this?
class Pointless
{
private int X, Y;
public Pointless()
{
X = 0;
Y = 0;
}
public int pointX
{
get
{
return X;
}
set
{
X = value;
}
}
public int pointY
{
get
{
return Y;
}
set
{
Y = value;
}
}
}
class Line
{
Pointless p1;
Pointless p2;
double slope;
public Line(Pointless p1_inn, Pointless p2_inn)
{
p1.pointX = 2;
p1.pointY = -4;
p2.pointX = 5;
p2.pointY = 3;
reiknaslope();
}
private double reiknaslope()
{
slope = (p1.pointX - p2.pointX) / (p1.pointY - p2.pointY);
return slope;
}
}
|
|
|
|
|
System.Drawing.Point already exists in the framework and the formula for slope of two points is:http://cs.selu.edu/~rbyrd/math/slope/[^] Your version is not correct?
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|
You probably shouldn't be setting the Xs and Ys in the Line constructor.
|
|
|
|
|
fixed it
now how the hell do I get it to write the number in console?
class Line
{
Pointless p1;
Pointless p2;
double slope;
public Line()
{
reiknaslope();
}
private double reiknaslope()
{
p1.pointX = 2;
p1.pointY = -4;
p2.pointX = -5;
p2.pointY = 3;
slope = (p2.pointY - p1.pointY) / (p2.pointX - p1.pointX);
return slope;
}
}
|
|
|
|
|
Pass the X and Y values to the constructors of Pointless.
Pass the instances of Pointless to the constructors of Line.
See the System.Console.WriteLine method for how to output.
|
|
|
|
|
Could you please show me how to do that?
|
|
|
|
|
You already know how, you actually had those parts right earlier.
|
|
|
|
|
I'm sorry I think my brain is fried I have no idea what you are talking about.
|
|
|
|
|
Your first example of Pointless had a reasonable constructor.
Your first example of Line took two Pointlesses as parameters, but didn't actually use them.
|
|
|
|
|
something like that?
also I get an "Unreachable code" warning on the Console.writeline, what am I doing wrong?
class Line
{
public double slope;
public Line()
{
reiknaslope();
}
private double reiknaslope()
{
Pointless pa;
Pointless pb;
pa = new Pointless();
pb = new Pointless();
return slope = (pb.pointY - pa.pointY) / (pb.pointX - pb.pointY);
Console.WriteLine(slope.ToString());
}
}
public Pointless()
{
X = 0;
Y = 0;
}
|
|
|
|
|
The line before it is returning from the method so it will never be reached.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Ok I got it, but it only writes out whole numbers, I remember there was some trick to write double values.
|
|
|
|
|
So do you have it working now?
|
|
|
|
|
I don't think my head can take much more banging on the desk.
The way I expect you will want to use these classes is:
Pointless p1 = new Pointless ( 2 , -4 ) ;
Pointless p2 = new Pointless ( -5 , 3 ) ;
Line l = new Line ( p1 , p2 ) ;
System.Console.WriteLine ( "{0: 0.00}" , l.Slope ) ;
Can you write your classes to work that way? I won't tell you how, you have all the ingredients.
modified on Thursday, January 22, 2009 5:03 PM
|
|
|
|
|
I wonder if Cooks share the same problem with people that want to be cooks. They have all of the ingredients, a picture of the end result, infinite documentation, and a ridiculous amount of time for trial and error yet the peanut butter and jelly sandwich still ends up tasting like gravel and looking like pudding.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|