|
|
I knew that But i want to do something like my question ?
|
|
|
|
|
As a rule, you cannot create an instance in single line if block.
so,
jojoba2011 wrote: if(i>0) List<string> Age = new List<string>();
is illegal.
Now, to be able to do this, you need to cover them in curly brackets. which means that the list you created doesn't exist outside the if block and hence the error.
|
|
|
|
|
string strAge;
int i=0;
if (i > 0)
{
List<string> Age = new List<string>();
strAge = Age.ToString();
}
else if (i == 0)
{
List<int> Age = new List<int>();
strAge = Age.ToString();
}
Check if this works.
Tej Aj,
http://opexsolution.com/forum/[^]
|
|
|
|
|
1. The above code still does not compile: you must give value strAge before the if blocks.
2. I think the OP might want something more than just the type name of the list.
|
|
|
|
|
string strAge = "";
int i = 0;
if (i > 0)
{
List<string> Age = new List<string>();
strAge = Age.ToString();
}
else if (i == 0)
{
List<int> Age = new List<int>();
strAge = Age.ToString();
}
I have checked this. It works fine. Try checking it again by initializing strAge="" at the start.
Also strAge is already declared before if block.
Tej Aj,
http://opexsolution.com/forum/[^]
|
|
|
|
|
Is there any issue if you use a List<object> for doing this?
int i=0;
List<object> Age = new List<object>();
string strAge = Age.ToString();
WWW, WCF, WWF, WPF, WFC .... WTF
|
|
|
|
|
That's kind of pointless. You've created a generic of type object which is the same as just having a none-generic List. What you are doing here is requiring that both types need to be boxed/unboxed which is an expensive operation - this is precisely the type of thing that generics were introduced to avoid.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
You are correct that boxing/unboxing is an expensive operation, but in this particular scenario we can’t have a generic list declared at the top and can be used for both int and string.
WWW, WCF, WWF, WPF, WFC .... WTF
|
|
|
|
|
A hint, format it better, like this:
int i=0;
if(i > 0){
List<string> Age = new List<string>();
}
else {
if(i==0) {
List<int> Age = new List<int>();
}
}
string strAge = Age.ToString();
This way (maybe this is personal, but for me it is easier) you'll see that Age is in fact only declared within the if and else scope. Not outside.
V.
|
|
|
|
|
Do you want to use a generic list whose type depends on the value of some other variable? If so, you could use something like:
System.Collections.IList myList;
bool useInts = true;
if (useInts)
myList = new System.Collections.Generic.List<int>();
else
myList = new System.Collections.Generic.List<string>();
if (useInts)
myList.Add(7);
else
myList.Add("John Doe");
Console.WriteLine(myList[0].ToString());
It will allow data of the defined type only, i.e. when you useInts is true, a myList.Add("John Doe"); statement will throw an exception.
|
|
|
|
|
Hi all,
I am going to write an application in c# (probably with asp.net) which will take pc microphone to communicate with users over existing GSM operators.
It will make the conversation with users distributed over the country and record the conversation.
How can I proceed.
please provide me as much information as possible.
|
|
|
|
|
This is really a stupid question in this forum, take it to the design and architecture forum where you may get someone to discuss this with you.
Even there you will need to have some ideas of your own and asking for as much info as possible is just plain rude!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
|
hi! i want to create a popup window in my desktop application,which appear at program startup on the position above the system clock, please give some general ideas to do so.
thanks
|
|
|
|
|
Use ShowDialog to display an instance of your popup. What are you having difficulties with?
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
You might also want to look into notifyicon and it's applications.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
|
|
Thanks a lot for your link.
It can be very useful and I'll keep it, but it isn't what I'm looking for.
I'm looking for a code that can integrate with the System Shell:
it should add some menu items to the Shell context menu, while the user is right-clicking on an Explorer icon.
The context menu shouldn't be shown when the user clicks on a button of a windows form.
However, thanks a lot again!
|
|
|
|
|
Do not ask the same question again by referencing another post.
This is a voluntary site, someone can choose to answer your question, or not. If there had been an answer you would have gotten one with your first post. What you have done here is very rude and inconsiderate.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
hi
a function called Save() saves user input to database
should i write try catch block like this
private void Save()
{
try
{
}
catch (SqlException sqlEx)
{
MessageBox.Show(sqlEx.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
or should i write it like this in the function or event handler that calls Save();
try
{
Save();
}
catch (SqlException sqlEx)
{
MessageBox.Show(sqlEx.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
and what is the difference, if there was a difference
and which is a better coding practice
thanks
|
|
|
|
|
It depends on what you're going to do with the exception. If the exception is of no consequence to external code and can be handled within the Save method itself, then keep the try/catch in there.
If the calling code may (now or in the future) want to do something depending on the exception then wrap the Save in the calling code.
The second way is more flexible in the long term IMO. If you decide to use the first then it may be worth changing the Save to return a bool to indicate success or failure to the calling code so it still has the option of retrying the Save operation.
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)
|
|
|
|
|
DaveyM69 wrote: If the exception is of no consequence to external code
my english is not very good, i don't get what you mean by this
by the way i'm using llblgen
|
|
|
|
|
I mean if the calling code will be able to continue regardless of specific exceptions then it's safe to catch these inside the save.
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)
|
|
|
|