|
Hi,
I am trying to use the settings.designer.cs file for a value that needs to be put into the app.config file.
My config(auto-generated by VS2005) file now looks like this:
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Settings1" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</sectionGroup>
</configSections>
<applicationSettings>
<General.Settings1>
<setting name="DatabasePath" serializeAs="String">
<value>C:\Web\App_Data\Messages.mdb;</value>
</setting>
</General.Settings1>
</applicationSettings>
</configuration>
From the code when i access this property i am using this code:
Settings1 set = Settings1.Default;
string str = set.DatabasePath;
return str;
The above line of code returns null... can anyone explain what am i missing here???
|
|
|
|
|
Try this:
Properties.Settings set = Properties.Settings.Default;<br />
string str = set.DatabasePath;<br />
return str;
or simply
return Properties.Settings.Default.DatabasePath;
Cheers,
Craig
** I'd rather try and fail than fail to try **
|
|
|
|
|
i know this question is dull but i need to ask it guys. If i have an array of strings lets say ( string[] array = {"1","2","4","boss","678"} ) and i need to pick one randomly without a specific string lets say "4" (the answer should not never be 4). May i know how ? Thanks for the help,
P.S: i can do it in different ways but i need it in one line of code and that, my friends , i dont know how.
Any help appreciated .... Thanks guys
|
|
|
|
|
I don't think your going to get it done in one line of code. If you are trying to keep your code simple, then I would write a routine that did the job and let calling it be your one line of code. So for example, I would write a function called GetRandomValue that handled all of the work, and then simply call that function when I needed it.
private object GetRandomValue(object[] possibleValues, object illegalValue)<br />
{<br />
Random randomIndexGenerator = new Random();<br />
int index = randomIndexGenerator.Next(possibleValues.Length-1);<br />
object randomValue = possibleValues[index];<br />
while(object == illegalValue)<br />
{<br />
index = randomIndexGenerator.Next(possibleValues.Length-1);<br />
randomValue = possibleValues[index];<br />
}<br />
return randomValue;<br />
}
|
|
|
|
|
Thanks man it has been very helpful ... but what i am really looking for is that i need to pick a random number from array0 excluding a certain number. the new array(the old one excluding that value)is array1(array1= array0-x) and then i need to pick randomly from array2 which is array1 excluding a new variable y) so array2 = array1 - y = (array0 - x)-y and so on. hope i made myself clear. I wanna thank you again
|
|
|
|
|
If you explain what you are trying to accomplish, it might be possible to give a useful answer. It might answer some of those questions, for example:
Is it the string value or the item in the array that is important?
May the string value occur more than once in the array?
Does the string value always occur at least once in the array?
Why do you need this in one line of code?
Why do you specify one line of code in a language that isn't line oriented?
How complex may that line of code be?
---
b { font-weight: normal; }
|
|
|
|
|
okay i am gonna explain what i am trying to do... i am doing a poker game as a project.... we cant have the same card twice. i am putting strings in an array {"11","21"..."131","12","22"..."132"..."}(ex:"34"is the card 3 of clubs, 3 number, 4 kind) . Since we cant have same card twice i used a while loop on each card. but since the while loop is causing the application to freeze with CPU usage 100% i thought i can ,on each card, pick up the card randomly from the an new array which is the old array excluding the names of the cards already picked.
Thanks again.
|
|
|
|
|
Use a list instead of an array, then you can remove the card from the list.
Here is a bit more object oriented approach, instead of using strings...
public enum Kind { Hearts, Clubs, Diamonds, Spades }
public class Card {
private Kind _kind;
private int _value;
public Card(Kind kind, int value) {
_kind = kind;
_value = value;
}
public Kind Kind { get { return _kind; } }
public int Value { get { return _value; } }
public string ValueString {
get {
switch (_value) {
case 1: return "Ace";
case 11: return "Jack";
case 12: return "Queen";
case 13: return "King";
default: return _value.ToString();
}
}
}
public override ToString() {
return this.ValueString + " of " + _kind.ToString();
}
}
public class Deck {
private List<Card> _cards;
private Random _rnd;
public Deck() {
_cards = new List<Card>(52);
for (int i = 1; i <= 13; i++) {
_cards.Add(new Card(Kind.Hearts, i));
_cards.Add(new Card(Kind.Clubs, i));
_cards.Add(new Card(Kind.Diamonds, i));
_cards.Add(new Card(Kind.Spades, i));
}
_rnd = new Random();
}
public Card GetRandom() {
if (_cards.Count == 0) throw new ApplicationException("Deck is empty.");
int pos = _rnd.Next(_cards.Count);
Card card = _cards[pos];
_cards.RemoveAt(pos);
return card;
}
}
Example of usage:
Deck deck = new Deck();
List<Cards> hand = new List<Cards>();
for (int i=0; i<5; i++) {
hand.Add(deck.GetRandom());
}
for (int i=0; i<5; i++) {
Console.WriteLine(hand[i].ToString());
}
Example output:
King of Spades
4 of Diamonds
8 of Clubs
Queen of Hearts
Ace of Spaces
Disclaimer: None of the code here is tested.
---
b { font-weight: normal; }
|
|
|
|
|
Blackocebo wrote: Hello man,
I was impressed by your posts and i really need your help. i am creating a card game ( some kind of poker) to give it to my friend in order to play it for free and no more spend money to play it.
I have recently finished the game but i am facing a problem, when pressing the (DEAL) button sometimes, at some points, the application freezes. The CPU usage reaches 100%. So i want to ask you as an expert what should i do.
[lengthy code not included]
Well, let's look at a part of the code:
num2 = randomnum.Next(1, 14);
kind2 = randomnum.Next(1, 5);
string string2 = num2.ToString() + kind2.ToString();
while (string2 == string1)
{
num2 = randomnum.Next(1, 14);
kind2 = randomnum.Next(1, 5);
}
As you don't change any of the strings inside the loop, that you use in the condition, if you ever enter into the loop, you will never exit.
The same of course applies to the three following loops too.
---
b { font-weight: normal; }
|
|
|
|
|
I want to thank u very very much ...
Respect
|
|
|
|
|
i want to write a applicatin that delete files from a specific directory how can i do this?
can any one show me a sample code to achieve this or point me to the class that can be used to do this
kenny
|
|
|
|
|
Look at the System.IO namespace, particularly the Directory and Files classes. The Directory class as a static GetFiles member that gets a list of files and the File class has a static Delete method that can delete the file.
foreach(string file in Directory.GetFiles([path])<br />
{<br />
File.Delete(file);<br />
}
|
|
|
|
|
How can i manage database transactions between layers? Using Presentation, business and data layers. One solution i found is to declare transaction in the business layer then pass it as parameter to the DAL, but i think that's the worse i can do. Any ideas?
Never argue with an idiot. They drag you down to their level, then beat you with experience. - Dilbert
|
|
|
|
|
That depends on what you're using. If you're .Net 2.0 and all on the same machine, just use the System.Transactions namespace. Open up a TransactionScope and every call that gets made within that will subscribe to that transaction automatically. If you're in .Net 1.1, you could use COM+ (System.EnterpriseServices) and carry the transaction that way. If you're on different machines separated by web services or remoting, it gets a little more complex. In .Net 1.1 and 2.0 you can use TIP (Transaction Internet Protocol), which is a part of COM+ to handle transactions across machines. But an even better solution comes with .Net 3.0 where WCF gives you the ability to carry transactions across services very easily.
I have an article on TIP[^] and an article on WCF[^] that explains how to spread those transactions across machines. Essentially you can start a transaction in the presentation layer and even if the DAL is on another machine, it can subscribe to it.
|
|
|
|
|
ok. Everything is on one machine... i'm going to try with TransactionScope then, and look for some article. Thanks for answer this Dustin, i needed the advice because i'm just starting using layers.
Never argue with an idiot. They drag you down to their level, then beat you with experience. - Dilbert
|
|
|
|
|
Hello,
I have a .MHT file with my C# application, I use WebBrowser control to navigate to it at runtime. Is it true that the user should have outlook installed in order to display the mht file correctly as it's stated on MS site:
NOTE: "Web archive" is available only if Outlook Express is installed. The ability to save a Web page as a Web archive file is provided by the Inetcomm.dll file (the Microsoft Internet Messaging API file), which is installed by Microsoft Outlook Express 5.
http://support.microsoft.com/kb/221787
Please advice. Thanks.
|
|
|
|
|
I take it as gospel, after I tried it without Outlook Express installed. BTW, I think OE is installed by default in every Windows installation since Windows 98. You'd probably have to build a machine from scratch to test this. A Virtual Machine, like VMWare or Microsoft's Virtual PC, is great for stuff like this.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
You might check for the existence of the file Inetcomm.dll
I found mine in C:\WINDOWS\system32\
string path = "%SystemRoot%\system32\Intercomm.dll";
path = System.Environment.ExpandEnvironmentVariables(path);
bool isExist = System.IO.File.Exist(path);
A better way might be to try to load the outlook object as a com object though. Then, if someone decides that library should get merged into another library, or decides to change the filename, or decides that dolphins should rule the earth, then you will still be golden if someone was smart enough to leave the functionality somewhere that the com object can get to it.
|
|
|
|
|
I have a custom control that I has embedded controls.
When I show a messagebox, the messagebox shows up behind the form.
Topmost on the form is set to false.
OpenFileDialog works as it is supposed to, why not messagebox?
Any ideas?
private void button13_Click_1(object sender, EventArgs e)
{
item i = new item();
Button b = new Button();
b.Text = "hello";
b.Click += new EventHandler(b_Click);
subItem s = new subItem("hello");
s.EmbeddedControl = b;
i.SubItems.Add(s);
this.myListView1.Items.Add(i);
}
void b_Click(object sender, EventArgs e)
{
//using (OpenFileDialog dlg = new OpenFileDialog())
//{
// dlg.ShowDialog(this);
//}
MessageBox.Show(this, "hello");
}
|
|
|
|
|
Instead Use MessageBox.ShowDialog(),
if u deliberately want to use MessageBox.Show mean pass the mainform's handle as the first parameter(owner) of Messagebox.Show
|
|
|
|
|
Hey all,
I'm encapsulating the SetWindowsHookEx() function in order to capture all keyboard messages.
I'm raising KeyEventHandler events from my class, so I'm planning to use the KeyEventArgs object, of course.
When capturing keyboard messages I get the key-code for the pressed key.
To instantiate a KeyEventArgs object, I need a System.Windows.Forms.Keys value.
Anybody got an idea as for how to get the appropriate Keys value
without using a large switch statement involving a case for every key on the keyboard??
Thanks in advance,
Shy.
|
|
|
|
|
I think you can just cast them, the Keys enumeration has the appropriate values defined for each character code.
Sig cops got me...
|
|
|
|
|
What about the numpad keys, space bar, etc...?
BTW... Is there any way to print the value of an enum?
Something like:
Keys k;
k = Keys.A;
Console.WriteLine(k.ToString()); The above code prints "A" BTW...
|
|
|
|
|
You could do:
Console.WriteLine("{0}.{1}", k.GetType().Name, k.ToString()); That's the simplest way I think.
Sig cops got me...
|
|
|
|
|
You could do:
Console.WriteLine("{0}.{1}", k.GetType().Name, k.ToString()); That's the simplest way I think.
Sorry forgot to answer your first question. The NumPad keys etc are still characters, just assigned a value outside the normal "visible" range. E.g. Esc is char(13) I think.
Sig cops got me...
|
|
|
|