|
That's an interesting hypothetical world. In the real world, if I do this:
static void Main(string[] args)
{
List<int> items = new List<int>(new int[] { 1, 2, 3, 4, 5, 1, 2, 3, 4 });
List<int> nextIndices = new List<int>();
try
{
for (int i = 0; i < items.Count() - 1; i++)
{
nextIndices.Add(FindIndexAfter(items, i, toMatch => toMatch == items[i]));
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
for (int j = 0; j < nextIndices.Count(); j++)
{
Console.WriteLine(nextIndices[j]);
}
Console.WriteLine("Program complete...");
Console.ReadKey();
}
static int FindIndexAfter<T>(List<T> items, int startIndex, Func<T, bool> predicate)
{
if (startIndex < 0)
throw new ArgumentOutOfRangeException("startIndex",
"Start index cannot be negative.");
if (startIndex >= items.Count())
throw new ArgumentOutOfRangeException("startIndex",
"Start index must be less than the number of items");
startIndex++;
while (startIndex < items.Count())
{
if (predicate(items[startIndex]))
return startIndex;
startIndex++;
}
return -1;
}
I print:
5
6
7
8
-1
-1
-1
-1
Program Complete
Passing startIndex as ref (which is close to what you are suggesting as the default), I get this:
1
3
5
7
Program Complete
Since structs are just immutable classes, you now also return by reference, which would mean that you would be filling the list with references to variable i instead of the values returned by the function. I'm not going to try to simulate that sort of return, but I'm pretty sure you would end up with a list filled with items.Count() when you were done. So, now you either have to invent some "return by value" notation or do something like return new int(startIndex);
Along the same line, you also now only have assignment by reference. Which means that
int i = 1;
int j = i;
j++;
Console.WriteLine(i);
now prints 2 instead of 1. Again, you would need some new syntax to specify "assign by value" instead of by reference.
All in all, I just don't think "structs are just immutable classes" (and the default pass-by-reference that comes from it) would actually make anything better.
|
|
|
|
|
Ignore what Calla says - a struct can indeed have methods, properties and events. In fact, there are only three real differences between a struct and a class:
1) A struct is created on the stack, not the heap.
2) A struct cannot have a parameterless constructor - it must have at least one parameter.
3) A struct is always sealed - you cannot inherit from it.
The first difference is the biggie: it means that you deal with the instance directly, not via a reference. Why this is important is a bit complex, so it would be best for you to read a good book on C# as it is very important that you understand what happens properly, if you are going to use structs instead of classes. At least the authors will have experience in explaining the difference to beginners, and teh added benefit of pictures, which make things a lot easier to understand!
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
Hrm.. I never said a struct couldn't have methods, properties or events (though I can understand how my reply could be interpreted like that). I was only trying to give a (simple) direction of when to use a struct and when to use a class. But maybe I was being slobby..
|
|
|
|
|
Calla wrote: Use a struct if you have no need for methods or to use inheritance and you only need a structure to store data
Sounds like you can't use methods to me!
Seriously, I don't think it's fair on beginners to give information that is misleading (not that you meant to, I'm sure) - it could send them down completely the wrong path. The difference between a struct and a class is a bugger to explain unless you can see how they are reacting to the info, which is why I suggested a book!
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
OriginalGriff wrote: Sounds like you can't use methods to me!
...or like you read the reply as the devil reads the bible
Seriously though, I admit it's a poor description but only as a result of trying to keep things short and provide a quick reply. I do realize you have a responsability when providing an answer to a beginner of making sure it's accurate and not misleading.
|
|
|
|
|
It was clear enough to anyone who already knows what what you mean.
|
|
|
|
|
Structs are value types - what that means in your situation is that it should only be used to represent a value.
For example an int has one particular value lets say 1. If you need an int with the value of 2 then it must be a different int as there is no way that 1 can be 2. Therefore, once a value is set for a struct it should not be changed by a property, method or anything else. This is called immutability and something all structs should adhere to (although there are exeptions in the framework). So all properties will be readonly and any methods that normally mutate the object should return a new instance of the struct with the required values and leave the original instance intact.
Also, the fields that make up a struct (must be private) should be value types themselves and should not, when combined, have a large memory footprint.
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Keep it simple; don't use structs.
|
|
|
|
|
How Can i change some Controls Size at runtime like .Net?
I mean :
I wanna users change the size of controls by clicking on control and then change the size like when designing the form !
|
|
|
|
|
Try this[^]. So many hits!
..Go Green..
|
|
|
|
|
|
I didn't get you. I guess, the link that I gave you lists so many articles explaining what you asked.
..Go Green..
|
|
|
|
|
I know google.
First i search google then if no good and then Codeproject.com
|
|
|
|
|
Not so easy, a few steps are involved. You need some event for deciding to change to your "edit" mode, e.g. a right mouse click on the control. Then, draw a rectangle around it or mark it otherwise. When you receive a mouse button down event on the border of that marked control, set a flag. Then observe the mouse move events and resize your control. When the mouse button is released, remove your flag. You may need to call Refresh() time and again for getting the things drawn nicely.
|
|
|
|
|
Thanks!
but i didnt understand
May u plz give me an example for this.
I need this code for my project!
PEASE HELP
|
|
|
|
|
This may help u
private void button1_Click(object sender, EventArgs e)
{
button1.Size = (new Size(50, 100));
}
Cheers
Rajesh B --> A Poor Workman Blames His Tools <--
|
|
|
|
|
NO ..
Not Like This
I want something like the previous solution !
But Dont know how to do that ?
|
|
|
|
|
I think U have to maintain a configuration file with control default properties.,
when u need the default properties (old properties), read & set the properties from configuration file.
Rajesh B --> A Poor Workman Blames His Tools <--
|
|
|
|
|
The Solution of bhiller is best .But i cant do that .
PLZ help !
|
|
|
|
|
Okay, I think I got your question now.
Try this: Allow the User to Resize Controls at Runtime[^]. The code is in VB. But once you understand the concept you can write your own code to do that or you may convert it using the conversion tools available online.
Let me know if this is what you wanted.
And ya, I found the link using Google.
..Go Green..
|
|
|
|
|
Hi,
I draw a rectangle over some text using solid brush. The content is invisible after drawing that rectangle. Is there any other brush which gives the transparency.
|
|
|
|
|
See here[^] for some samples on using the Opacity property.
It's time for a new signature.
|
|
|
|
|
Hi Experts,
I am trying to convert a VB application to C# .net. I came across an enum which contained values as
stsAvailabilityBits = &HF,<br />
stsFormAccessAllowed = &H1,<br />
stsValidUser = &H2,<br />
stsSystemAvailable = &H4,<br />
stsPermissionGranted = &H8,<br />
stsInquiryOnlyBits = &HF0,<br />
stsFormInquiryOnly = &H10,<br />
stsUserInquiryOnly = &H20,<br />
stsSystemInquiryOnly = &H40,<br />
stsRecordLocked = &H80,<br />
stsShutdownMinutesBase = &H100,<br />
stsShutdownMinutes = &HF00,<br />
stsErrorReturned = &H1000,<br />
stsFatalError = &H2000,<br />
stsTableEmpty = &H4000,<br />
stsResultsReturned = &H8000,<br />
stsParentControl = &H10000,<br />
stsLoadResultset = &H20000,<br />
stsSpecialPrivBitsReturned = &H40000,<br />
stsRecordAccessReturned = &H80000,<br />
stsTableLockRequested = &H100000,<br />
stsTableLockConflict = &H200000,<br />
stsMultipleLockConflict = &H400000,<br />
stsAvailBit23 = &H800000,<br />
stsUserCanExport = &H1000000,<br />
stsUserCanRename = &H2000000,<br />
stsUserCanReport = &H4000000,<br />
stsUserCanDelete = &H8000000,<br />
stsGetForInquiry = &H10000000,<br />
stsWebUser = &H20000000,<br />
stsAvailBit30 = &H40000000,<br />
stsAvailBit31 = &H80000000<br />
I am not able to understand what are these. I think they are hexadecimal values. If they are then how can i convert them into decimal/integer values in C#.net?
Please help!
Thanks!
Regards,
Samar
|
|
|
|
|
try this code...
private string getDecodedKey(string strKey)
{
string strTemp = string.Empty;
string[] strSplitKey = strKey.Split(' ');
foreach (string strHex in strSplitKey)
{
try
{
int intVal = Convert.ToInt32(strHex, 16);
strTemp = strTemp + Char.ConvertFromUtf32(intVal);
}
catch
{
strTemp = strTemp + strHex + "GET DECODED KEY";
}
}
return strTemp;
}
|
|
|
|
|
Hi Ice,
I tried running your code with the value of strKey as "&HF" and it showed me the error
"Could not find any recognizable digits."
on this line of code
int intVal = Convert.ToInt32(strHex, 16); .
Am i doing anything wrong here?
Regards,
Samar
|
|
|
|
|