|
class GlobalVariables
{
public static int Counter;
}
class App
{
public App()
{
GlobalVariables.Counter++;
}
}
static void Main(string[] args)
{
var a = new App();
var b = new App();
Console.WriteLine(GlobalVariables.Counter);
Console.ReadLine();
}
Needless to say that abusing it as a global variable is an anti-pattern. I don't know what you are trying to do, but this seems like the wrong way.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
No: you have to something to "hold" the value, a Field, or a Property.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
A Console Application is not meant to be instantiated more than once; you need to separate out what you want to create multiple instances of from the Console App "framework:"
namespace ConsoleApplication1
{
class Program
{
public static int Counter = 0;
static void Main(string[] args)
{
SomeClass s1 = new SomeClass();
SomeClass s2 = new SomeClass();
}
}
public class SomeClass
{
public int MyID { set; get; }
public SomeClass()
{
MyID = Program.Counter++;
}
}
}
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
hello every one,
I have a problem with my program,
I can select a image from one folder in open dialog but I can't move the image to be processed in another directory.
so pleas help me..
this is my program
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Dir
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private TransferFile transferFile = new TransferFile();
private void button1_Click(object sender, EventArgs e)
{
string cmd = "/c" + textBox1.Text;
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = cmd;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
richTextBox1.Text = process.StandardOutput.ReadToEnd();
}
OpenFileDialog ofd = new OpenFileDialog();
private void button2_Click(object sender, EventArgs e)
{
string Filename = @"E:\MyDir";
string result;
result = System.IO.Path.GetFileName(ofd.FileName);
Console.WriteLine("GetFileName('{0}') returns '{1}'", Filename, result);
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = " RGBImage.exe " + System.IO.Path.GetFileName(ofd.FileName);
}
}
|
|
|
|
|
Make it easy by using what .NET provides:
using System.IO;
string f1 = @"C:\test1";
string f2 = @"C:\test2";
string fname = "someJPG.jpg";
private void MoveFile(string folder1, string folder2, string filename)
{
string filePath1 = Path.Combine(folder1, filename);
if ((!File.Exists(filePath1)) || (! Directory.Exists(folder2)))
{
throw new ArgumentException("invalid file or folder path");
}
try
{
File.Move(filePath1, Path.Combine(folder2, filename));
}
catch (Exception ex)
{
Console.WriteLine("{0} error moving file", ex.Message);
}
}
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
hello,i have question as follows.
for example, I want to call fun1, then introduce vary x, and function as vary(fun2 or fun3), fun2 and fun3 have different varies. In fun1, can call fun2 or fun3 according to reference.
I think delegate can solve it ,but fun2 and fun3 have different number input, how to do it?
delegate void call() //?
main()
{
int x,y,z;
fun1(x,call)
}
fun1()
{
fun2(y) or fun3(y,z)?
}
fun2(int y);
fun3(int y,int z);
|
|
|
|
|
This code makes no sense: you call method 'fun1 with two arguments, but method 'fun1 takes no parameters. All your methods, and the delegate, are defined outside the scope of the Main method: what is the enclosing Class ?
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
modified 30-Nov-15 4:45am.
|
|
|
|
|
class1()
{
main()
{
int x,y,z;
fun1(int x, fun2)
}
fun2(int y);
fun3(int y,int z);
}
class2()
{
static fun1(int x, fun ?)
{
fun2() or fun3()
}
}
|
|
|
|
|
Please, show us the code you really have - don't try reinterpreting your code as it makes it impossible to understand.
This space for rent
|
|
|
|
|
Your second code example also make no sense, and will never compile.
I believe you are very "lost" in C#: now, there's nothing wrong with that; everyone here was once a "beginner," and went through some degree of confusion.
Here's how to get "un-lost:"
Get the Visual Studio Edition, get the free book by Charles Petzold, "Dot Net Zero" [^].
Start at the beginning of that book and study the basics of C# carefully, start programming the examples in that book.
cheers, Bill
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
i am sorry,indeed i am a beginner.
I don't know how to meet my requirement. My requirement as follows:
I want to code an optimization algrithm function to calculate Minimized value. It can input function that needed optimization, like fun2 or fun3 ,etc... that i said. It means if i input fun2 to optimization function ,it can search fun2's Minimized value, if input fun3, it can search fun3's Minimized value.But fun2 and fun3 or other functions don't have same style input variable,their input variable style and number is not same.
how can i realize it?
|
|
|
|
|
Are you looking for something like this:
static void Main()
{
int x = 1;
int y = 2;
int z = 3;
fun1(x, () => fun2(y));
fun1(x, () => fun3(y, z));
}
static void fun1(int x, Action call)
{
call();
}
static void fun2(int y) { ... }
static void fun3(int y, int z) { ... }
Lambda Expressions (C# Programming Guide)[^]
C# in Depth : The Beauty of Closures[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you very much,it 's near my requirement.
But one more requirement is that:
static void fun1(int x, Action call)
{
call();
}
take fun3(y,z) for example, call() is run fun3(y,z).
but if i want to run fun3(x,z) ,how to realize it? you know ,i want to change y value in fun1 to call fun2(int y) or fun3(int y,int z),because my code use loop to change y value in fun1,to let fun2 or fun3 be a minimum value.
|
|
|
|
|
So something like this:
static void Main()
{
int x = 1;
int z = 3;
fun1(x, y => fun2(y));
fun1(x, y => fun3(y, z));
}
static void fun1(int x, Action<int> call)
{
call(x);
}
static void fun2(int y) { ... }
static void fun3(int y, int z) { ... }
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Great Job!!! Thank you very much.
And i have other question bothering me.
I have to use chart control to plot 10 series including 1000 points per series in one chartarea. But after ploting, mouse move or zoom in or ContextMenuStrip operation is very slow and delay. I think maybe paint problem.
When i ask before, i get the answer is point number is too big.
But i found that if i plot 200 series with 1 point for example as follows, the operation is also slow. How can treat it?
follows is for example:
Series series1;
for(int i=0;i<200;i++)
{
series1=new series();
series1.Points.Add(new DataPoint(i,i));
chart1.Series.Add(series1);
}
|
|
|
|
|
smallkubi wrote: I think maybe paint problem. Why?
smallkubi wrote: When i ask before, i get the answer is point number is too big. The more points you need to draw, the longer it takes. If there had been a useless delay in the chart-control, people would have noticed.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I mean Dalay is not that Chart generating process is slow, when i finished plot, i want to operate on Chart, like mouse moving, Scroll... is slow.
|
|
|
|
|
Meaning it will have to recalculate which part it should show and repaint it. That takes time. Smaller charts with less points will be faster.
If you find that the Chart-control is "too slow" for your needs, you may have to implement your own.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
oh, do you know how to stop control auto refresh? i think if i can refresh it in mannual?
|
|
|
|
|
Stop the user from scrolling, panning and/or zooming, and you don't need to refresh.
If anything changes, a refresh would be required to get the updated data on screen.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Now i want to have the function of Zooming,scrolling... Anything changes, screen don't update automatically,I want to updated data on screen mannully, can it be possible?
|
|
|
|
|
Sure; use the drawing functions to draw your chart. Means writing code to calculate the length of the axes, drawing each point, drawing some labels.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi, i have another question...
If i want to use multi-thread to call fun1, how can i input the variable x, y,z?
for example:
static void Main()
{
int x = 1;
int z = 3;
Thread th=new Thread(fun1( ? )) //how can i do ?
th.start();
// fun1(x, y => fun2(y));
// fun1(x, y => fun3(y, z));
}
static void fun1(int x, Action<int> call)
{
call(x);
}
static void fun2(int y) { ... }
static void fun3(int y, int z) { ... }
|
|
|
|
|
Something like this:
Thread th = new Thread(() => fun1(x, y => fun3(y, z)));
Breaking it down:
Action<int> call = y => fun3(y, z);
ThreadStart start = () => fun1(x, call);
Thread th = new Thread(start);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Good Idea,i meet it.
And if i have a button at main Thread , it can abort Thread "th". How to code button click function?
And, if whether Thread th end or abort, i want to run" MessageBox.Show("Thread end")", how can i do it?
|
|
|
|
|