|
If this is not already in a for loop, then how is the current value of i determined?
in any event, it will fail for i=0 since there is no element whose index = -1.
Perhaps we need a to see just a wee bit more of your code in order to help?;)
|
|
|
|
|
<br />
for (i = mySquares.Count - 1; i > 0; i--)<br />
{<br />
((Rectangle)mySquares[i]).X = <br />
((Rectangle)mySquares[i-1]).X;<br />
((Rectangle)mySquares[i]).Y = <br />
((Rectangle)mySquares[i-1]).Y;<br />
}<br />
I'm trying to update the x,y co-ordinates of the rectangles in the arrary in a Timer.Tic() method.
it tells me that my left operand has to be a property but is Rectangle.X not a property?
|
|
|
|
|
Interesting, try this:
for (int i = mySquares.Count - 1; i > 0; i--) {
Rectangle r1 = (Rectangle)mySquares[i];
Rectangle r2 = (Rectangle)mySquares[i-1];
r1.X = r2.X;
r1.Y = r2.Y;
}
Burt Harris
|
|
|
|
|
Burt Harris (msft) wrote:
Rectangle r1 = (Rectangle)mySquares[i];
Rectangle r2 = (Rectangle)mySquares[i-1];
Unfortunately that won't work either.
The problem is that Rectangle is a value type so the modifications you are doing are being done to the unboxed value which is discarded. Instead, you should create a new Rectangle object and place it back in the collection.
for (int i = mySquares.Count - 1; i > 0; i--)
{
Rectangle r1 = (Rectangle)mySquares[i];
Rectangle r2 = (Rectangle)mySquares[i-1];
mySquares[i] = new Rectangle(r2.X, r2.Y,
r1.Width, r1.Height);
}
[Edit]I should have posted this sooner instead of catching up on CP, Richard beat me to it :P[/Edit]
James
- out of order -
|
|
|
|
|
Rectangle is a struct, so that probably won't work anyway, since you're changing the coordinates of an unboxed copy of your rectangle. Try something like:
for (i = mySquares.Count - 1; i > 0; i--)
{
mySquares[i] = new Rectangle(
((Rectangle)mySquares[i-1]).Location,
((Rectangle)mySquares[i]).Size);
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
|
|
|
|
|
Thanks everyone for your replies it is working now (Well at least the assignment problem I had
|
|
|
|
|
Is this the only way to 'fall through' on switch statements?
<br />
int x = 3;<br />
switch(x){<br />
case 0:<br />
goto case 1;<br />
case 1:<br />
goto case 2;<br />
case 2:<br />
goto case 3;<br />
...<br />
}<br />
*->>Always working on my game, teach me
*->>something new.
cout << "dav1d\n";
|
|
|
|
|
|
IMHO something like this would be a cludge that screams poor design.
If you need to use goto to 'fall through' a switch then you need to consider redesigning your application.
<br />
switch(x)<br />
{<br />
case 0:<br />
case 1:<br />
case 2:<br />
DoAction();<br />
break;<br />
.<br />
.<br />
.<br />
}<br />
Probably not what your doing though.
|
|
|
|
|
yeah indeed. perhaps you could consider getting each of your case statements to call functions instead of each other.
not sure what your exact need is, but all i know is from previous experience that fallthrough statements come back to bite you in the butt later when you least expect them.
|
|
|
|
|
No, you can 'fall' through so long as there is no code between the case statements.
And yes, it sucks.
Christian
No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002
|
|
|
|
|
Anyone know how to get system info at runtime. I specifically need to find out what COM ports are installed.
Thanks
-Sam
|
|
|
|
|
Hi Sam,
Take a look at System.Management namespace, its a wrapper for WMI, you might find what you are looking for.
Cheers
Kannan
|
|
|
|
|
Try this out:
private void btnComPorts_Click(object sender, System.EventArgs e)
{
txtOutput.Clear();
string server = @"\\" + txtServer.Text;
ManagementScope ms = new ManagementScope(server + @"\root\cimv2");
ObjectQuery oq = new ObjectQuery("select deviceID from win32_SerialPort");
ManagementObjectSearcher mos = new ManagementObjectSearcher(ms, oq);
ManagementObjectCollection moc = mos.Get();
foreach(ManagementObject mo in moc)
{
txtOutput.AppendText(mo["DeviceID"].ToString());
txtOutput.AppendText("\r\n");
}
}
Matt is a network administrator for an auditing company in the midwest. He is shamelessly looking for Windows programming side work.
|
|
|
|
|
Thanks Matt, worked excellent!
|
|
|
|
|
I've seen many really good examples of how to add scrolling credits to your Visual C++ applications, but I haven't found any yet for a C# Windows application. Are there any out there?
|
|
|
|
|
How can I test for a null string?
if (str == "")
doesn't work if str is a string containing just \0.
|
|
|
|
|
if (str == null)
[edit]Note that .NET makes a distinction between null and "" - both are possible. The first one throws a Null exception, when you try to use it, the second one bevaves like you expect from a zero-length string.[/edit]
If I could find a souvenir / just to prove the world was here [sighist]
|
|
|
|
|
So, if (str == null || str.Length == 0) then.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
|
|
|
|
|
I am not sure what you mean by "contains just \0". If you mean something like the C string terminator this is not really used and if you happen to get strings like this you will have to test for this case explicitly.
If you want to treat null references the same as empty strings you need a test like this:
if((str == null) || (str == ""))
Do not check the string length, by the way. Strings can contain characters such as writing direction markers that do not show. Hence, their length is > 0 but they are still empty!
cheers
erik
|
|
|
|
|
Yes, I did mean the C string terminator. If you write out a string to memory using MemoryStream and StreamWriter and then read it back in with a MemoryStream and StreamReader, you get that terminating \0 at the end of the string. However, (str == null), (str.Length == 0), and (str == "") all fail. The only solution I found was to use string.Trim to manually remove the \0.
|
|
|
|
|
Hi!
I have a problem thats been itching for days now and i really need help with this one.
I want to get the text that i write to begin from the bottom of the RichRTextBox, just like they having int the mIRC program.
im building a chat program and i need that thing too work.
hopes for an answer!
thanks
// trones
|
|
|
|
|
If you change the HideSelection property to false it will keep scrolling to the bottom. It will still start the text at the top, but will scroll down with it.
Maybe you could just pad it off the bat to bring it down?
Hope that helps..
-Sam
|
|
|
|
|
I want my C# program to be able to, at runtime, check a directory for small "drivers", display the drivers in a comboBox, and use the driver.
The drivers would just take in a string, parse it, and call a set number of functions like SetPos(x,y), SetHeading().....
It would be nice if they could be written in different languages, but that isn't necessary.
What would be the best way to go about this, could anyone point me to some info.
Thanks...
-Sam
|
|
|
|
|
The drivers could be DLL containing classes which implement a specific interface. With Reflection, you load them dinamically, and scan for the interface.
To simplify things (security), the drivers would be in a subdirectory of your application.
This would allow your users to create drivers in any .NET application.
If you want more end-user flexibility in choosing languages, go for COM and publish a typelibrary with an interface.
I see dumb people
|
|
|
|