|
x = DynamicArray == null ? -1 : DynamicArray.Length;
is just an alternate (shorter) syntax for:
if (DynamicArray == null) x = -1; else x = DynamicArray.Length;
|
|
|
|
|
Oh it's just the ternary operator
result = condition ? true-value : false-value
It just selects one of the values based on whether the condition is true or not.
So basically it's
if (condition)
result = true-value;
else
result = false-value;
But as an expression, instead of a statement
|
|
|
|
|
Nope x = DynamicArray == null ? -1 : DynamicArray.Length; is shorthand for
int x = -1;
if(DynamicArray != null)
x = DynamicArray.Length;
|
|
|
|
|
Thanks I think I have a better Idea and something new to play with.
|
|
|
|
|
Hm you raise an interesting point - is the second argument always evaluated?
|
|
|
|
|
No, but the ternary operator syntax is unclear if you haven't seen it before.
|
|
|
|
|
So... let me get this straight.. does it act like the expression form of
result = arg1;
if (!condition)
result = arg2;
Or like:
result;
if (condition)
result = arg1;
else
result = arg2;
|
|
|
|
|
The second one - see also my comment below.
|
|
|
|
|
Good good, my world is back to normal, thank you
|
|
|
|
|
Like the Second:
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int x = true ? ++a : ++b;
Console.WriteLine("a={0}, b={1}", a, b);
int y = false? ++c : ++d;
Console.WriteLine("c={0}, d={1}", c, d);
outputs:
1, 0
0, 1
Meaning that the "false" value of the ternary operator never gets evaluated (infact VS highlights it as unreachable).
|
|
|
|
|
Ok so it's like I've always thought, but then, why did you post that other way?
|
|
|
|
|
for (int i = 0; i < NewServerVersionTableArray.Length)
{
if (NewServerVersionTableArray[i].AssistSuiteApplication.MasterApplication = true.ToString())
{
string MyAppLocation = NewServerVersionTableArray[i].AssistSuiteApplication.PAppLocWS;
string MyAppName = NewServerVersionTableArray[i].AssistSuiteApplication.FAppNameWS;
Process MyApplicationOpen = NewServerVersionTableArray[i].AssistSuiteApplication.MasterApplication = true.ToString() ?
File.GetAccessControl(MyAppLocation + "\\" + MyAppName)
Process oAccess = Process.Start(MyMSAccessPath, MyAppLocation + "\\" + MyAppName)
:
null
}
}
Basically if MasterApplicaiton = true to use the tirshery command to try and open a file?
|
|
|
|
|
What is it supposed to do?
|
|
|
|
|
Essentially
Check a boolean variable that identifies if a application is told it is to startup.
If the boolean value is true then startup the application. If the boolean value is false skip and continue on to the next applicataion (essentially do nothing).
|
|
|
|
|
So essentially here is how I changed the code around...
I think I am correct in the logic.
Can you confirm?
for (int i = 0; i < NewServerVersionTableArray.Lengthl; i++ )
{
string MyAppLocation = NewServerVersionTableArray[i].AssistSuiteApplication.PAppLocWS;
string MyAppName = NewServerVersionTableArray[i].AssistSuiteApplication.FAppNameWS;
string MyMSAccessPath = PeerAssistLoader.Properties.Settings.Default.DefaultMSAccess;
if (File.Exists(MyMSAccessPath))
{
Process MyOpenApp = StandardProcedures.Convert_TrueFalse(NewServerVersionTableArray[i].AssistSuiteApplication.MasterApplication) ? Process.Start(MyMSAccessPath, MyAppLocation + "\\" + MyAppName) : null;
bool result = MyOpenApp == null ? StandardProcedures.ErrorLogWriter("Unable to Open the Application: " + MyMSAccessPath + " " + MyAppLocation + "\\" + MyAppName) : StandardProcedures.DebugLogWriter("Opened the Application: " + MyMSAccessPath + " " + MyAppLocation + "\\" + MyAppName);
bool result = MyOpenApp == null ? StandardProcedures.DebugLogWriter("Unable to Open the Application: " + MyMSAccessPath + " " + MyAppLocation + "\\" + MyAppName) : StandardProcedures.DebugLogWriter("Opened the Application: " + MyMSAccessPath + " " + MyAppLocation + "\\" + MyAppName);
}
}
|
|
|
|
|
Better yet would this be the correct way of doing this?
Process MyOpenApp = StandardProcedures.Convert_TrueFalse( NewServerVersionTableArray[i].AssistSuiteApplication.MasterApplication) ?
Process.Start(MyMSAccessPath, MyAppLocation + "\\" + MyAppName) : null
|
|
|
|
|
Pure Laziness.
int x = -1;
if(DynamicArray != null)
x = DynamicArray.Length;
Is shorter than
int x;
if(DynamicArray == null)
x = -1;
else
x = DynamicArray.Length;
When I started to reply, no-one had (at that point) explained what the ternary operator did, I wasn't trying to make a point that my OP was better or more accurate or anything, someone just beat me to the punch. If I had to implement the length property , and it needed to return -1 in the manner requested in the top post I'd actually do it like this:
public const int UninitializedLength = -1;
public int Length
{
get
{
if(DynamicArray == null)
return UninitializedLength;
return DynamicArray.Length;
}
}
|
|
|
|
|
Not true; Ian's answer above is more nearly the correct one. The value of x is not set until the expression has been evaluated, at which time it is set to the value of the 'true-value' or the 'false-value' as appropriate. Since true-value and false-value may also be expressions, even ternary expressions, only one of the values will ever be evaluated, as is the rule for all if/else expressions. Also in the expression
x = e ? true : false;
the type of x must have been previously declared, it is not inferred from the context.
|
|
|
|
|
x = DynamicArray == null ? -1 : DynamicArray.Length;
Let me see if I understand this somewhat...
could I use this in such a way... no i don't think so I think I might need to
What i was think was this...
<array not="" initilized="">
private void Procedure1(ref MyArray[] DynamicArray)
{
<array thru="" logic="" determined="" not="" to="" create="" array="">
dynamicarray.length = DynamicArray == null ? -1 : DynamicArray.Length;
}
if (dynamicarray.length = -1)
{
MessageBox.Show("No Array Created")
}
|
|
|
|
|
No, the length is read-only (and even if it weren't, it would probably have checked for the length to be >= 0)
PS: you don't need to ref an array unless you intend to replace the array entirely
|
|
|
|
|
Thanks you all have been a big help.
|
|
|
|
|
My Current work around is initialize the array at 1 length and leave all values null. Then the application just outputs a blank record. Or skips on the other filters.
|
|
|
|
|
Is there a reason you are using a [dynamic] array?
The List<T> should to provide pretty much everything need to get the job done.
That said, you should initialise the array with zero members to start with, otherwise you ar elikely to get a world of pain when other developers come to use your code.
modified on Friday, September 18, 2009 10:40 AM
|
|
|
|
|
That is what I did was initilize it to 0th to resolve the issue.
The reason for not using List<t> is I am still learning how to use the List<t>? functionality. I Only used the Dynamic array for the first 2 arrays in the applicaiton and the others I have been using ArrayLists which is way better in managing then Dynamic Arrays.
But List<t> I have not grasped yet. As I want to be able to place multiple sub members to each entry and I have not figured it out yet. Though I am experimenting with it. I agree it would be the best way to handle what I am doing but A) I did not know List<t> when the applicaiton began development, and B) I have to learn somewhere.
|
|
|
|
|
Learning and using the generic list is going to be much easier than coding a fully functional dynamic array by hand. If you are already using arrays much of the syntax is the similar!
|
|
|
|