Hi all,
I'm new to C# and have a question please. Here's the problem. I'm trying to modify some C# program where I call some methods within if... else if... directives repeatedly:
if (some val >= 40 && other val < 0)
{
DrawArrowDown("MyDnArrow" , true, 1, (hh+0.0002), Color.OrangeRed);
DrawDiamond("MyDiamond", true, 1, (hh+0.0011), Color.Blue);
DrawTriangleDown("MyDownTriangle", true, 1, (hh+0.0013), Color.Blue);
}
else if (some val < 40... && ... >=30 ...)
{
DrawArrowDown("MyDnArrow" , true, 1, (hh+0.0002),
Color.OrangeRed);
DrawDiamond("MyDiamond", true, 1, (hh+0.0011),
Color.Blue);
DrawTriangleDown("MyDownTriangle", true, 1, (hh+0.0013),
Color.Blue);
}
else if (some val < 30... && ... >=20 ...)
{
DrawArrowDown("MyDnArrow" , true, 1, (hh+0.0002),
Color.OrangeRed);
DrawDiamond("MyDiamond", true, 1, (hh+0.0011),
Color.Blue);
DrawTriangleDown("MyDownTriangle", true, 1, (hh+0.0013),
Color.Blue);
}
else if...
I came from Oracle PL/SQL world where there used to be no classes but
procedures, functions and packages... In Oracle I'd simply create some procedure with parameters to call those repetitive Draw...() statements, smth like this: Procedure PlotSignals(arrowhandle, true, arrow_pos, color)... and call this proc. in if... else if... passing the correct parameters. How can I simplify this and create some procedure in C# please? Any examples would be much appreciated.
Thank you very much.
Loading
Sam HobbsPosted Nov 10, 2010, 4:23 PM
In the C# language, it is impossible to have anything outside a class; that is a fundamental design criteria of C#. The designers of C# are object-oriented enthusiasts and they want to emphasize the advantages of object-oriented programming. It is possible to write programs that essentially use global items using a static members but usually it is possible to develop more object-oriented solutions.
You sure are benefiting from the time spent by others to help you. Note that most of what you are asking for help with is already explained very well in articles and books. Forums are usually intended for questions that are not easily answered in the dovumentation and in articles and in books. I hope you can get a good book about C#; it will answer many, many more questions.
ArthurPosted Nov 9, 2010, 6:26 PM
Thanks for your reply. Here's what I was looking for:
void PlotSignals(string direction, int arrowPos, Color arrowColor)
{
if (direction == "up")
DrawDot("up arrow", arrowPos, arrowColor);
else if (direction == "down")
DrawDot("down arrow", arrowPos, arrowColor);
}
// To call...
protected override void OnBarUpdate()
{
if (....)
PlotSignals("up", 2, Color.Green);
else if (...)
PlotSignals("down", 2, Color.Red);
}
Is there a global variable in C# that can hold the values throughout the session and programmer can manipulate with it passing some global values from one program unit to another...? Suppose I have few objects, e.g. some separate windows forms that I'm running in my current session. How can I pass a value from one form to another please? This maybe not a good example but I cannot come up with anything better.
TonyPosted Nov 9, 2010, 3:29 PM
Hi Art.
Sorry it took me a while to get back to you. I've been really busy.
Ok... let's take a step back here. This is what I'm seeing from your last post:
So you have onBarUpdate() that is currently calling draw() methods based on conditions, like this:
protected void onBarupdate(){
if (some val >= 40 && other val < 0)
{
DrawArrowDown();
}
And what you're wanting is some method that you can call that will contain all of the draw() methods like this:
protected void onBarupdate(){
myUltimatDrawMethod(
}
Is that correct?
Here's a couple questions for you Art. Do all of your draw methods share the same number AND type of parameters? Also, are you needing to call certain draw() methods based on a particular set of parameters?
ArthurPosted Nov 5, 2010, 9:14 AM
This time I'd like to ask about methods. While I'm slowly grasping the concept of classes I'd like to learn about methods please.
Here's my orig example again:
// this progr. is from charting software - Ninja Trader.
protected override void OnBarUpdate()
{
if (some val >= 40 && other val < 0)
{
DrawArrowDown("MyDnArrow" , true, 1, (hh+0.0002), Color.OrangeRed);
DrawDiamond("MyDiamond", true, 1, (hh+0.0011), Color.Blue);
DrawTriangleDown("MyDownTriangle", true, 1, (hh+0.0013), Color.Blue);
}
else if (some val < 40... && ... >=30 ...)
{
DrawArrowDown("MyDnArrow" , true, 1, (hh+0.0002), Color.OrangeRed);
DrawDiamond("MyDiamond", true, 1, (hh+0.0011), Color.Blue);
DrawTriangleDown("MyDownTriangle", true, 1, (hh+0.0013), Color.Blue);
}
.........
}
In OnBarUpdate() I'm calling methods (Draw...() methods) within if's. I'd like to be able to call my method with all all those Draw...() methods already inside, e.g.:
Method PlotSignals(int arrow_dir, int arrow_position, arrow_color)
{
if (arrow_dir == 2) // arrow down then
{
DrawArrowDown("MyDnArrow" , true, 1, arrow_pos, arrow_color);
DrawDiamond(....)
DrawTriangleDown(....);
}
else // arrow_dir == 1 - arrow up
{
DrawArrowUp().....
......
}
}
Then in OnBarUpdate() I'd simply call PlotSignals(pa1, par2....) method with a single statement instead of repeatedly calling Draw...() methods as below:
protected override void OnBarUpdate()
{
if (some val >= 40 && other val < 0)
{
// this method has all
PlotSignals(2, 5, arrow_color); // 2 - arrow down, 5 - arrow position on the chart, color...
}
else if (some val < 40... && ... >=30 ...)
{
PlotSignals(1, 5, arrow_color); // 1 - arrow UP, 5 - arrow position on the chart, color...
}
.........
}
This is what I'm trying to accomplish and I think is better and simpler/cleaner approach...
Thank you very much.
Art.
ArthurPosted Nov 3, 2010, 11:55 AM
And Thank you Diego. You guys rule!
Thank you very much to all again.
Art.
TonyPosted Nov 3, 2010, 10:53 AM
Hi Arthur.
There aren't two nested classes. There's one class and two constructors. Let me explain.
The class declaration is: public class DrawClass{
The constructors are:
public DrawClass() AND public DrawClass(
So in OOP, a class is not the same thing as an object. A class is a blueprint for an object. In order to create an object from a class we have to instantiate it, or better put, create a new object. And that looks like this:
DrawClass myDrawClass = new Drawclass()
When you intantiate an object, you're actually calling the constructor. So....
DrawClass myDrawClass = new DrawClass() - This is hitting the blank constructor.
DrawClass myDrawClass = new DrawClass(
Constructorrs allow you to pass in values upon creation of the object. Take a person class for example.
Person myPerson = new Person("Frank", "Smith");
If we had a class called person that had the attributes of 'firstName' and 'lastName', we could create a new object and pass in a first and last name. So then if we needed to GET the values we could then do this:
myPerson.firstName;
myPerson.lastName;
And our output would be "Frank Smith". Does this make more sense?
DiegoPosted Nov 3, 2010, 10:38 AM
If you want to comment a block you can use
/* block here
block here */
ArthurPosted Nov 3, 2010, 9:09 AM
public class DrawClass
{
......
TonyPosted Nov 2, 2010, 9:06 PM
Below is one example of each of your methods being called through a class (container).
//Class declaration - these are comments btw ;)
public class DrawClass
{
}
Then in your Main() method you would instantiate the object like this.
public static void Main()
{
//here you have your original if statements
if (....)
{
DrawClass drawACircle = new DrawClass(circle, 1, black);
}
else if (more ....)
{
DrawClass drawASquare = new DrawClass(square,1, purple);
}
}
You can even call them directly like this DrawMeSomething.DrawCircle();
}
else if (...)
{
DrawMeSomething.DrawSquare();
}
Does this help??
ArthurPosted Nov 2, 2010, 7:21 PM
PlotSignals(int arrow_dir, int arrow_pos, string(or whatever C# type?) arrow_color)
{
if (arrow_dir == 2) // arrow down
{
DrawArrowDown("MyDnArrow" , true, 1, arrow_pos, arrow_color);
DrawDiamond(....)
DrawTriangleDown(....);
}
else // arrow_dir == 1 - arrow up
{
DrawArrowUp().....
}
}
...
I do not know if I need to create a class and how to call above procedure/class in C#. What should the PlotSignals be? Some class or smth else to call it within the same program? Second, I do not know how to declare type or whatever this is in C# for things such Color.Blue, which is last parameter in Draw...()
Thank you very much.
DiegoPosted Nov 2, 2010, 7:00 PM
So man i thing you should put the if's source inside of the method
like this:
DrawArrowDown(string text ,boolean b, int i, float f, int val int someVal)
{
if(some val >= 40 && other val < 0)
{
//do something
}
else if()
...
}
Then you would to call him once.
Hope to help you.
ArthurPosted Nov 2, 2010, 4:22 PM
Thanks for your reply.
I'm trying to simplify the process. Instead of repeatedly calling those Draw...() methods I'd like to be able to create some program unit or some kind of container that would have those Draw...() statements in it. Then I'd like to call that program unit in my if...s. For ex., create a program, say PlotSignals (which arrow-up or down parameter) where I would put those Draw...() once. Then call PlotSignals(with parameters) in place of Draw...() as above in my orig. post. I do not want repeatedly call Draw...() over and over again. Currently, if I have to change the arrow position or color in my DrawArrowDown("MyDnArrow" , true, 1, (hh+0.0002), Color.OrangeRed); I have to repeat this as many times as many DrawArrow...() methods are in my program - if I have 100 Draw...() methods then I'd be changing arrow color and pos. 100 times. The goal is to be able to do this only once.
Thank you,
Art.
TonyPosted Nov 2, 2010, 3:58 PM
Hi Arthur -
So are you trying to figure out how to create a method in C#? I wasn't completely clear from your post above.
Here's a basic example of a method:
I'm not sure if this helps or not. If you need something more than this let me know.
Tony D.