|
Hi guys,
I know how single dimensional arrays works but I don't understand at all these GetUpperBound and GetLowerBound methods for multi dimensional arrays. I know somebody would have a very simple way to educate me so i could have a broad smile on my face.
Thanks
string[,] myarray = new string[,] { { "01", "Honda" }, { "02", "GM" }, { "04","Ford"} };
myarray.GetUpperBound(0);
|
|
|
|
|
Look at this msdn article:
[^]
|
|
|
|
|
Every array has at least 1 dimension, for instance:
string[] x1 = new string[] {...} // is 1 dimensional
string[,] x2 = new string[,] {...} // is 2 dimensional
string[,,] x3 = new string[,,] {...} //is 3 dimensional
Each dimension has upper and lower bounds, which in most cases is easiest to think about as a capacity, since the lower bound is almost always 0.
The GetUpper and LowerBounds functions help you get the bounds (lowest possible index to highest possible index) for the given dimension, so...
Here we are creating a 1 dimentional array with a capacity of 5 elements.
string[] x1 = new string[5];
now we pass the target dimension (0 index based) to the function...
GetLowerBounds(0) would say 0.
GetUpperBounds(0) would say 4.
Why does upper bounds say 4? Because the bounds functions are returning 0 based indexes, thus, the capacity is 5, and the 5 indexes of each 'space' are 0,1,2,3, and 4.
If we were to do this:
GetLowerBounds(1) or GetUpperBounds(1) we would get an exception, because our array has only 1 dimension (thus we would pass index 0).
More reading here:
http://msdn.microsoft.com/en-us/library/system.array.getlowerbound.aspx[^]
http://www.java2s.com/Tutorial/CSharp/0220__Data-Structure/ArrayGetLowerBoundGetUpperBound.htm[^]
|
|
|
|
|
When you use an initializer list, the outermost array is the first dimension and the innermost array is the last dimension. So, myarray[0,0] is "01" and myArray[0,1] is "Honda" (notice that I didn't change the index for the first dimension). Another way of seeing it is that the first index of the first dimension points to {"01", "Honda"}... you have to specify an index into the second dimension to get either "01" or "Honda".
In the example you gave, the first dimension (GetUpperBound(0)) has an upper bound of 2 (because there are 3 items, the third item would be referenced by index 2). The second dimension (GetUpperBound(1)) has an upper bound of 1 (because there are 2 items in each subdimension, the second items are referenced by index 1).
If that doesn't make sense, play with it a bit and you'll eventually get it via osmosis.
By the way, in C# the lower bound should always be 0.
|
|
|
|
|
aspdotnetdev wrote: you'll eventually get it via osmosis.
osmosis? that's what doesn't happen when you put your C# book under your pillow and hope it will "sink in" while you're asleep.
|
|
|
|
|
Osmosis is when you hear Arnold say "My CPU is a neural net processor -- a learning computer" and only understand what that means years later when you understand the underlying concepts from different perspectives.
|
|
|
|
|
Hi,
While drawing treenodes, I'm having difficulties drawing the the boxes with pluses and minuses for node expansion. And also, I'm unable to simulate the hottracking action for each node when mousing over. Can you please help?
thank you
|
|
|
|
|
You need to elaborate your scenario to understand the problem u r facing
|
|
|
|
|
Maybe this code should help you to understand... :
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
....
if ((e.Node != null) && (e.Node is MyTreeNode))
{
MyTreeNode myTreeNode = e.Node as MyTreeNode;
Rectangle bounds = e.Bounds;
if (myTreeNode.CHECKBOX == true)
{
ControlPaint.DrawCheckBox(e.Graphics, bounds.Left, bounds.Top, 16, 16, ButtonState.Checked);
bounds = Rectangle.Inflate(bounds, 16, 0);
}
// Draw the background and node text for a selected node.
if ((e.State & TreeNodeStates.Selected) != 0)
{
// Draw the background of the selected node. The NodeBounds
// method makes the highlight rectangle large enough to
// include the text of a node tag, if one is present.
e.Graphics.FillRectangle(Brushes.BlueViolet, bounds);
// Retrieve the node font. If the node font has not been set,
// use the TreeView font.
Font nodeFont = e.Node.NodeFont;
if (nodeFont == null) nodeFont = this.Font;
// Draw the node text.
e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, Rectangle.Inflate(bounds, 2, 0));
}
// Use the default background and node text.
else
{
e.DrawDefault = true;
}
// If a node tag is present, draw its string representation
// to the right of the label text.
//e.Graphics.DrawString(e.Node.Tag.ToString(), this.Font,
// Brushes.Yellow, e.Bounds.Right + 2, e.Bounds.Top);
// If the node has focus, draw the focus rectangle large, making
// it large enough to include the text of the node tag, if present.
if ((e.State & TreeNodeStates.Focused) != 0)
{
using (Pen focusPen = new Pen(Color.Black))
{
focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
Rectangle focusBounds = bounds;
focusBounds.Size = new Size(focusBounds.Width - 1, focusBounds.Height - 1);
e.Graphics.DrawRectangle(focusPen, focusBounds);
}
}
}
else
{
e.DrawDefault = true;
}
}
|
|
|
|
|
thanks. So I guess if I want those boxes with plus/minus to the left of the node for expansion and contraction, I need to draw them myself.
|
|
|
|
|
xilefxilef wrote: I need to draw them myself
That's what 'OwnerDrawm' is all about
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)
|
|
|
|
|
hi, is it possible not to allow text selection and cursor blinking in textboxes when clicked? The problem is; with one click cursor places in the textbox and with double click text in the textbox is selected and highlighted. I just want nothing to happen when clicked
|
|
|
|
|
Use a Label control instead?
|
|
|
|
|
well, shame on me why didn't I think of that!
|
|
|
|
|
Or disable it. I think you can still set the background and foreground colors so it isn't greyed out.
|
|
|
|
|
Hi,
i have a string in this format YYYYMMDD HH:MM:SS how can i convert this string in a datetime ?
thanks for help.
|
|
|
|
|
I'm not going to spoon feed you, however I can recommend this little article[^].
|
|
|
|
|
When it comes to these type of questions... GOOGLE is your best friend!
|
|
|
|
|
I am new to this whole regex thing. I have read up on it but i still need some help. I have expressions (shown below) and I want to get the values (substring). For example, in example 1 i want to search for my property ([[[MyProperty1]]]) and I want to get back 'Value'. In example 2, i want to be able to search for [[[MyProperty2]]] and have value2 as the result.
Cant you help me write the pattern as well as explain to me what is being done in the pattern to acheive this goal. I have tried '(?<=\[{3}MyProperty1).*?(?=<\]{3})' but it does not work.
Thanks in advance.
Example 1: [[[MyProperty1]]] Value
Example 2: [[[MyProperty1]]] Value [[[MyProperty2]]] value2 [[[MyProperty3]]] value 3
|
|
|
|
|
The main problem may be that [ and ] are special characters in RegEx and need to be escaped \[ and \] .
Oh, I see you did.
I'm unfamiliar with <= and =< , but I just whipped this (?:\[\[\[(?'Name'.*?)\]\]\](?'Value'[^\[]*)) up with my RegexTester[^].
modified on Friday, April 9, 2010 1:20 PM
|
|
|
|
|
I used the escape sequence for the [. If I run '\[{3}MyProperty\]{3}, I get back '[[[MyProperty]]]'. So I think it may be something else. I think it may be the fact that I am trying to use lookahead/lookback.
|
|
|
|
|
Fayu wrote: I am trying to use lookahead/lookback
Why would you?
|
|
|
|
|
I found an example on the web which used that to get the substring. Again, I am new to this. What would you suggest?
|
|
|
|
|
What I added to my first response.
|
|
|
|
|
I didnt realize you updated your comment.
The 'Value' is unknown, can I use * as a wild card?
|
|
|
|