|
Random rnd = new Random();
rnd.Next();
easier than what? :P
Two lines is neither Hard nor Complex.
I suppose you could make your own method that would allow you to turn your 2 lines into 1, but I don't think it'll save you much devlopment time!
Random needs a seed and so needs to be constructed as you might want the same seed as a previous time or a new seed.
You then need to get each random number so a method call is needed, which also has multiple overloads so you can specifiy the number range if you wish.
If you want to remove all the flexability provided by the different parameters you can pass to the constructor and method then go ahead but as I said is it really worth it to save a single line of code?
Thats assuming you only use it once of course! the more times you use rnd.Next() without creating a new instance of rnd the difference in total lines gets less and less.
|
|
|
|
|
Random rnd = new Random(); rnd.Next();
one line
|
|
|
|
|
F16I wrote: Random rnd = new Random(); rnd.Next();
one line
int r = new Random().Next();
really one line
---
single minded; short sighted; long gone;
|
|
|
|
|
Extending off of originSH's post, you generally don't want to reseed the generator each time. THe seed is taken from the system clock and as a result if you do multiple calls back to back within a few dozen milliseconds while reseeding before each number generated you'll get the same return for all of them.
--
You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer
|
|
|
|
|
Is there a way to set the ForeColor for only the displayed text in a ComboBox control? In other words, can I set the text of the selected item to, say, Color.Red, while keeping the other items in the list as Color.Black (or any other color I desire)?
Thanks.
|
|
|
|
|
Hello,
Basically you could inherit your own ComboBox from System.Windows.Forms.ComboBox:
public class SpecialComboBox : System.Windows.Forms.ComboBox
{
In the classes constructor you have to change the "DrawMode" property:
public SpecialComboBox()
{
this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
}
Then you have to override "OnDrawItem":
protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
{
using(System.Drawing.SolidBrush sb = new System.Drawing.SolidBrush(System.Drawing.Color.Red))
{
e.Graphics.DrawString(this.GetItemText(this.Items[e.Index]), e.Font, sb, e.Bounds);
}
base.OnDrawItem (e);
}
Note: I only tested it with "DropDownStyle" set to "System.Windows.Forms.ComboBoxStyle.DropDown", and I think behaviour changes then.
I think this article[^] covers it up better.
Hope it helps!
All the best,
Martin
|
|
|
|
|
Hi!
I encountered a problem when setting TextBox.Text variable to String which contains both right-to-left (ex. Arabic or Hebrew) and left-to-right (ex. digits) characters. The characters from this string are automatically reordered for display. For example, when I put there a String
"1" + \u05d0 + "2" + \u05e1
(four characters - two digits and two hebrew)
the TextBox displays text
"1" + \u05e1 + "2" + \u05d0
(two hebrew characters are exchanged).
What I noticed during analyzing this issue:
1. String isn't changed itself - when I print TextBox.Text in Console, it is the same as it was previously - so it is only a matter of displaying.
2. Display order is changed after setting TextBox.RightToLeft property to Yes. However, it still differs from the original one.
Is there any option to present String in TextBox as is? (in raw mode)? I've googled a lot, but unfortunately didn't find any solution.
Thanks in advance for your reply,
Tomek.
|
|
|
|
|
Hi,
I used an object of class type derived from XmlTextReader which implemented IDisposable. What member function of XmlTextReader should I call to clean up so that I can call them in the Dispose function of my derived class. If I understand correctly I really should only call Close() but do I need to call other functions like for example Flush()?
Having implemented the IDisposable pattern, I can now use my object in the form
using(MyDerivedXmlTextReader _mdXml = new MyDerivedXmlTextReader(...))<br />
{...}<br />
TIA!
I am a SysAdmin, I battle my own daemons.
|
|
|
|
|
By wrapping the class in the using statement, the Dispose method is automatically being called when the using block ends. Generally (but not always), Close is synonymous with Dispose . You can verify that by looking at the MSDN docs or using Reflector to look at the code. The Flush method generally is used to flush any data in the internal buffer to disk. Again, generally, the Close method will typically call Flush .
|
|
|
|
|
Okay now my problem:
I want to have a program (for example form1) that holds a data structure
if i click on call, form1 loads dynamically another program (showform)
shoform needs to get to the data structure in form1 to view this sturct, but how??
sry for bad english ^^
|
|
|
|
|
|
Der M wrote: I want to have a program (for example form1) that holds a data structure
if i click on call, form1 loads dynamically another program (showform)
Do you really mean different programs, or only forms (in the same program)?
If you mean different forms, this[^] article will help you.
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
I mean diffrent Programs.
|
|
|
|
|
How would you suggest that I go about multiplications for decimal and double types since they are not allowed by VS2005?
I'm not sure why they are not allowed by Microsoft, but, is there some valid reason for that?
In the following snippet the code will break at the following line:
_balance += _amount + (amount * 0.1);
Error:
Error 1 Operator '*' cannot be applied to operands of type 'decimal' and 'double'
--------------------------------------------------------------------------------------------
Code:
using System;
using System.Collections.Generic;
using System.Text;
using Wrox.Interfaces;
using Wrox.Classes;
namespace Wrox.Interfaces
{ public interface IBankAccount
{
void PayIn(decimal amount);
bool Withdraw(decimal amount);
decimal Balance
{ get;}
}
}
namespace Wrox.Classes
{
public class VenusBankAccount : IBankAccount
{
public Exception InsufficientFunds;
private decimal _balance;
public void PayIn (decimal _amount) {
_balance += _amount;
}
public bool Withdraw (decimal _amount) {
if (_balance > _amount)
{
_balance -= _amount;
return true;
}
else
{
throw InsufficientFunds;
}
}
public decimal Balance {
get
{
return _balance;
}
}
public void PayInWithCommission (decimal _amount) {
_balance += _amount + (amount * 0.1);
}
}
}
namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
IBankAccount myFirstBankAccount = new VenusBankAccount();
myFirstBankAccount.PayIn(1000);
VenusBankAccount myRealVenusBankAccount = new VenusBankAccount();
myRealVenusBankAccount.PayInWithCommission(1000);
}
}
}
Jon
|
|
|
|
|
You express a decimal literal as 0.1M if I recall correctly.
|
|
|
|
|
Try it like this:
_balance += _amount + (amount * 0.1m); The m (or M)tells the compiler to treat the number as a decimal value, without it the number is treated as a double .
The reason this is giving you an error is that there are no implicit conversions between floating-point types and the decimal type. A decimal denotes a 128-bit data type. Compared to floating-point types, the decimal type has a greater precision and a smaller range, which makes it suitable for financial and monetary calculations. The double keyword denotes a simple type that stores 64-bit floating-point values.
|
|
|
|
|
That is an excellent explanation.
|
|
|
|
|
|
Could someone point me in the direction of an article perhaps on how to write DLL's in C# and call them from C++ or Delphi or something other than a .NET language?
I'm hoping this is even possible?
Cheers
|
|
|
|
|
Dewald wrote: I'm hoping this is even possible?
Exposing the .NET assembly as either COM objects or Web Services would do that. Alternatively you can use C++/CLI to reference the C# managed assembly (DLL), wrap the .NET object API and expose a Native C++ or C API that non .NET languages can call. Based on the small amount of information you provided I would guess COM might be a good solution for you.
|
|
|
|
|
|
I have been programming in C# for years now, but I have never gotten a straight answer on this:
we always declare "using System.Whatever" at the top of a .cs file to make life easier when we program, but is there a cost associated with having many of them declared, especially (or even) if they arent used, or are they treated much the same as comments and they get stripped out during the compile if they arent used?
It's been bugging me for years.
______________________
Mr Griffin, eleventy billion is not a number...
|
|
|
|
|
Hello,
Look at this thread[^] and specially at "Guffa's" post!
All the best,
Martin
|
|
|
|
|
Awesome, thank you
______________________
Mr Griffin, eleventy billion is not a number...
|
|
|
|
|
From what I understand is that the using statements work the same as in C++ in the since you don't have to add the namespace to each command you are trying to use/call, but they have the added benefit of calling the garbage collector to look for those associated objects in your code. Makes is so you don't have to manually call dispose to it for the mem to be cleaned up during exit.
I guess there might be a *small* downfall to them but, I doubt it is worth messing with and isn't much faster than removing one IO write....
more info here: http://msdn2.microsoft.com/en-us/library/zhdeatwt(VS.80).aspx[^]
|
|
|
|