|
public partial class Form1 : Form
{
myClass o = new myClass();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.DataBindings.Add("Text", o, "TheProp", false, DataSourceUpdateMode.OnPropertyChanged, string.Empty);
}
}
class myClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string theProp;
public string TheProp
{
get { return theProp; }
set
{
if (string.IsNullOrEmpty(value))
theProp = "Another Value";
else
theProp = value;
NotifyPropertyChanged("TheProp");
}
}
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
|
|
|
|
|
Thanks!
It works like a charm now...
I had set the databinding via the Visual Studio GUI using a BindingSource.
Apparantly it doesn't set the parameters exactly the same way. Setting the binding myself the way you showed made it work.
Greetings,
Davy
|
|
|
|
|
Hello,
I am new to C# and I was wondering how does one verify the prescence of the back end of a database? I am using my app as the front end for an MS access database. As an expert in VBA this was easy and you could call the windows dialog to find the backend.
Any help is greatly appreciated.
Art
|
|
|
|
|
2010JohnFByers wrote: verify the prescence of the back end of a database
Just try connecting to it.
2010JohnFByers wrote: call the windows dialog to find the backend.
Any help is greatly appreciated.
Art
So then you don't even have a connection string yet?
What are you asking?
|
|
|
|
|
That depends on how you communicate with the backend (if I have undertsood you correctly).
For instance, if you were to use Linq2Sql there is a "DatabaseExists" method which will return a boolean denoting that the database has a valid connection and is available (or not).
I'm sure there are plenty of other ways to do this but this might get you started.Tychotics
"The dinosaurs became extinct because they didn't have a space program. And if we become extinct because we don't have a space program, it'll serve us right!"
Larry Niven
|
|
|
|
|
I looked into Linq DatabaseExists code with Reflector, and actually it just tries to connect to the database and returns false if an exception is thrown.
|
|
|
|
|
Yes I am looking to verify that the backend exists and if not shutdown the app. I may be misunderstanding this but the reason I want to do this is that if you establish a connection to the backend and for some unknown reason it isn't there you would get an error. Actually I'd like to know what is done programnmatically to prevent errors when the backend isn't available etc. for a dataset and its binding?
Thanks!
|
|
|
|
|
hi,
I'm just learning C# after being a Delphi coder for waayyy to long
I was wondering, why isn't it possible to use an else statement with the foreach statement?
Check out my quick and dirty demo code:
namespace ConsoleApplication
{
class Product
{
public string Code { get; set; }
public string Description { get; set; }
}
class Program
{
static void Main(string[] args)
{
IEnumerable<Product> myProductList = new List<Product>
{
new Product() {Code = "CDR", Description = "CD-Rom"},
new Product() {Code = "DVD", Description = "DVD-Rom"},
new Product() {Code = "BRD", Description = "BlueRay Disc"},
};
var myProductSelection = (from aProduct in myProductList where aProduct.Code.StartsWith("R") select aProduct);
Console.WriteLine("The products that were found:");
foreach (var aProduct in myProductSelection)
{
Console.WriteLine("Found product: " + aProduct.Code);
}
Console.ReadKey();
}
}
}
The above code returns nothing, since none of the product codes start with a "R".
Would be nice if I could do this when my result is empty:
Console.WriteLine("The products that were found:");
foreach (var aProduct in myProductSelection)
{
Console.WriteLine("Found product: " + aProduct.Code);
}
else
{
Console.WriteLine("No products were found!");
}
Just some ranting.. any good solutions to this unlike something like this?
Console.WriteLine("The products that were found:");
int x = 0;
foreach (var aProduct in myProductSelection)
{
x++;
Console.WriteLine("Found product: " + aProduct.Code);
}
if (x > 0)
Console.WriteLine("No products were found!");
thx for any suggestions offered - Life would be so much easier if I had the source code!
- If C# had true garbage collection, most applications would delete themselves upon execution
|
|
|
|
|
Ted On The Net wrote: Just some ranting.. any good solutions to this unlike something like this?
I can only think of 1 fundamentally different solution - explicitly test for the number of items in myProductSelection.
Personally I would set a bool to true in the foreach rather than counting the iterations, but that is not that much different from the solution you gave.
There is no build-in way to do this, as far as I know.
|
|
|
|
|
does myProductSelection have a count or a length? Most strucutres that implement IEnumerable do. If so just check to see if the count is greater than zero.
|
|
|
|
|
yes, it does. One could implement it like this:
if (myProductSelection.Count() != 0)
{
foreach (var aProduct in myProductSelection)
{
Console.WriteLine("Found product:" + aProduct.Code);
}
}
else
{
Console.WriteLine("No products found!");
}
But why should I? If the foreach loop doesn't get executed, I know I've got the same result as if I used the above if-statement. So imo this gives me redundant code. Or do you disagree?- Life would be so much easier if I had the source code!
- If C# had true garbage collection, most applications would delete themselves upon execution
|
|
|
|
|
Ted On The Net wrote: But why should I?
Because it expresses the intention of the code?
I.e. if there are items loop through them otherwise produce message. Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
|
|
|
|
|
I understand why you want this, but there is no implicit way of doing it.
But, think that using foreach is a replacement for getting the enumerator, calling while(enumerator.MoveNext()) and setting a variable to enumerator.Current. (and at the end calling Dispose, if possible).
So, the best solution for this is doing something like:
using(var enumerator = enumerable.GetEnumerator())
{
if (enumerator.MoveNext())
{
do
{
var someVariable = enumerator.Current;
... the loop code...
}
while(enumerator.MoveNext());
}
else
{
... the else you want ...
}
}
And you can even create a method using delegates Action<T> for the value and non-generic Action for the else.
Something like:
ForEachOrElse(IEnumerable<T> enumerable, Action<T> actionForEachValue, action elseAction)
And to use it you call something like:
ForEachOrElse
(
enumerable,
(value) =>
{
... the code for each value...
},
() =>
{
... the code for else ...
}
); modified on Tuesday, March 9, 2010 9:34 AM
|
|
|
|
|
Well no offense, but I'm not really convinced that this is the best solution - it is rather confusing especially to people who never "manually" used an enumerator..
|
|
|
|
|
Console.WriteLine("The products that were found:");
foreach (var aProduct in myProductSelection)
{
Console.WriteLine("Found product: " + aProduct.Code);
}
else
{
Console.WriteLine("No products were found!");
}
Try this:
Console.WriteLine("The products that were found:");
int productsFound = 0;
foreach (var aProduct in myProductSelection)
{
Console.WriteLine("Found product: " + aProduct.Code);
productsFound++;
}
if (productsFound == 0)
Console.WriteLine("No products were found!");
|
|
|
|
|
Welcome
This is my first program. Sorry for my English.
My program runs some other program. Waits 10 seconds. I searched in hex 00 00 00 31 32 31 If you find it in the textbox will show the last 3 values and a search for 7 consecutive bytes. Please help
Process process = new Process();
process.StartInfo.FileName = "process.exe"
process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
process.Start();
System.Threading.Thread.Sleep(10000);
And now do not know how to deal with and find MemoryRead
|
|
|
|
|
Did you look at ReadProcessMemory [^]?
I don't really understand what you're asking, though.
|
|
|
|
|
I want to write a program that runs another program (process.exe), waiting 10 seconds, and looks in memory (process.exe) value hex 00 00 00 31 32 31 if he finds that the value is shown 31 32 31 and another 7 value
I look to readprocessmemory but i dont understand this. Can U make for me this program?
Its something like this <a href="">http://www.codeproject.com/KB/trace/minememoryreader.aspx</a> but my program is looking for value in hex
|
|
|
|
|
So then you would be reading as much memory as you can and search for those bytes, right?
|
|
|
|
|
Yes thats right
When the program find in the memory process.exe value 00 00 00 31 32 31 xx xx xx xx xx xx xx
it displays 31 32 31 xx xx xx xx xx xx xx
|
|
|
|
|
Hi,
I know I'm really close but not just quite there yet.
I'm reading data from a text file with the following code and as a newbie I am advancing bit by bit and I've come unstuck...again.
What I want to do now is in place of int num = 4991 I want to use Close[0] (which is a double) to compare against tmp (an int) but have absolutely no idea how to go about it.
Here is my code.
private string path = Cbi.Core.UserDataDir.ToString() + "PIn.txt";
private int[] levels;
private int count = 0;
private string readText = "";
<big>int num = 4991;</big>
<big>private int tmp, res = 0;</big>
private int? diff = null;
#endregion
protected override void Initialize()
{
CalculateOnBarClose = true;
Overlay = true;
PriceTypeSupported = false;
levels = new int[1000];
}
protected override void OnBarUpdate()
{
readText = File.ReadAllText("C:\\PIn.txt");
string [] split = readText.Split(new Char [] {';'});
foreach (string s in split)
{
count++;
tmp = int.Parse(s);
levels[count] = tmp;
if (tmp > <big>num</big>)
{
if(diff == null)
diff = <big>num</big>;
if ((tmp - <big>num</big>) < diff)
{
res = tmp;
diff = tmp - <big>num</big>;
}
}
}
Print(res);
}
Really appreciate the help,
suprsnipes
|
|
|
|
|
Hi,
1.
you could round your double to an int once and store it in a variable; a simple cast would suffice.
Alternative (not recommended): you could do everything using doubles.
2.
your code is wrong; test it with 0001 as the first number in the file!
3.
no need for private int? diff = null; as a simple private int diff = int.MaxValue; would work equally well, need less code, and fix the bug (2)
4.
your code has an artificial limitation of 1000 numbers in the file; it will crash for longer files.
Solution 1: use a List<int> instead of an array; a List behaves like an array that grows as needed.
Solution 2: don't store the numbers at all (your code isn't using them at all)
|
|
|
|
|
How would I do the following?
1. you could round your double to an int once and store it in a variable; a simple cast would suffice.
|
|
|
|
|
int roundedValue = (int)myOriginalDoubleValue;
BTW: do yourself a favor, go buy a book on C# and study it. It is the fastest and the best way of learning a new language thoroughly.
|
|
|
|
|
Thanks for your help Luc Pattyn.
I have revised the code from your suggestions which are much appreciated and I will be purchasing a book on C#.
Regards,
suprsnipes
|
|
|
|