|
The (0, 20) cannot be random, and if it was it would be pointless.
int n1 = rnd.Next(0, 51);
int n2 = rnd.Next(0, 51);
n1+n2 would never be greater than 100, and the lowest would be 0, but the answer could be anything inbetween.
As for storing the answerm you can put it in the tag, and check it later
Random rnd = new Random();<br />
<br />
foreach(Control label in this.Controls)<br />
{<br />
if(label.GetType().ToString() == "System.Windows.Forms.Label")<br />
{<br />
int n1 = rnd.Next(0, 20);<br />
int n2 = rnd.Next(0, 20);<br />
int answer = n1+n2;<br />
<br />
label.Tag = answer;<br />
label.Text = n1 + " + " + n2 + " =";<br />
}<br />
}<br />
You'd have to make sure that each text box was referencing the right label though, if you keep all the default names the textBox1 should reference label1
To get the answer back to check it simply:
int answer = (int)label1.Tag;
|
|
|
|
|
Hi The Undefeated,
Thanks for your reply!
I'll just make a few extra variables than
Thanks for your time and help The Undefeated!!!!!
|
|
|
|
|
The Undefeated,
How do i reference the textbox to the right labels?
|
|
|
|
|
The Undefeated wrote:
foreach(Control label in this.Controls)
{
if(label.GetType().ToString() == "System.Windows.Forms.Label")
{
label.Text = "Hello";
}
}
I recommend not to use a string comparison to determine the type of an object. Use typeof() instead.
foreach(Control label in this.Controls)
{
if( label.GetType() == typeof( System.Windows.Forms.Label ) )
{
label.Text = "Hello";
}
}
Regards,
Tim
|
|
|
|
|
Hi Tim,
Thanks for your reply!
I've changed that in to:
if(label is Label)
Regards,
Yustme
|
|
|
|
|
I'm just trying to send a string to another PC, i got my IP, set up the stuff, and it didn't work. Then i realised that im on a router so the IP address is actually wrong
So, i can only think of two things, sending it to the MAC address, or getting some other IP for the computer.
I know quite a bit about computers, but my knowledge of networking is err, lacking... so im not exactly sure what to do.
Thanks in advance, i'll keep searching.
EDIT: My IP is '192.168.1.3' - but of course, that will only work for computers connected to my router won't it? (Its also subject to change)
-- modified at 8:30 Sunday 15th October, 2006
|
|
|
|
|
I doesn't matter what Your IP is, nor that it is on a private network. You need to send to the IP of the OTHER computer. The router's IP should be your computers gateway address, then the router will route the message correctly.
We need to graduate from the ridiculous notion that greed is some kind of elixir for capitalism - it's the downfall of capitalism. Self-interest, maybe, but self-interest run amok does not serve anyone. The core value of conscious capitalism is enlightened self-interest.
Patricia Aburdene
|
|
|
|
|
Aww, im still confused, or at least somebody is.
My computer, and the computer downstairs, both have the same IP (when checked at whatsmyip.org or whatever) except for the IP on the network, downstairs has 192.168.1.2 because it connected before i did.
So if i wanted to send something to that, what would i put? (Well I could put in the IP on our network, but what if i was somewhere else completely?)
|
|
|
|
|
Hello
Just as a guidline, use System.Net.NetworkInformation namespace, and use IPGlobalProperties.GetActiveTcpConnections() to get the activve Tcp/Ip connections. Now foreach connection use Dns.Resolve() to get the Hostname, and compare it with the computer's name of the other computer.
Regards
|
|
|
|
|
Greetings,
I just need to have a progressbar that updates from a seperate thread that acts as a timer to show a countdown to refresh, and then refresh a datagrid with data, how would i make thread safe calls to the progressbar? because at debug i receive the exception
Message="Cross-thread operation not valid: Control 'pbarAutoRefresh' accessed from a thread other than the thread it was created on."
i have searched all over, some of the examples i've seen have delegates, if i need to create a delegate for the progressbar class what would the delegate class be?
Thanks in advance.
|
|
|
|
|
For void methods without any parameters you can use System.Windows.Forms.MethodInvoker :
pbarAutoRefresh.Invoke(new MethodInvoker(pbarAutoRefresh.PerformStep));
____________________________________
There is no proof for this sentence.
|
|
|
|
|
Thank you, working now
|
|
|
|
|
Hi,
I am trying to use XML file as an config file. The structure of the xml file is quite simple. It needs to have only
5 fields. This file stores information about 3 websites - so ideally structure should be like
<root>
<config>
<website 1="">
<url>test
<delay>23
<username>23
<password>32423
<website 2="">
<url>test
<delay>23
<username>23
<password>32423
I am using following code to create this Xml schema and xml file - but having some trouble (which i don't know WHY -
and I seek your guidence...)
public void create_Config_Table(string service_name, string url, string delay,
string freq, string username, string password)
{
string filename = "config.xml";
FileStream fs;
XmlTextWriter xtw;
XmlTextReader xtr;
string msg = "";
//create the table and then populate it
DataTable ret = new DataTable(service_name);
//Create the DataSet
DataSet ds = new DataSet("AutoMessage");
//first check if config.xml exists or not
//If it already exists then we don't have to recreate the XML schema for it
if( ! File.Exists(filename) )
{
//ret.Columns.Add("Service", Type.GetType("System.String"));
ret.Columns.Add("URL",Type.GetType("System.String"));
ret.Columns.Add("Delay", Type.GetType("System.String"));
ret.Columns.Add("Frequency",Type.GetType("System.String"));
ret.Columns.Add("Username",Type.GetType("System.String"));
ret.Columns.Add("Password",Type.GetType("System.String"));
//Now populate the data table
DataRow row = ret.NewRow();
//row["Service"] = service_name;
row["URL"] = url;
row["Delay"] = delay;
row["Frequency"] = freq;
row["Username"] = username;
row["Password"] = password;
ret.Rows.Add(row);
ds.Tables.Add(ret);
msg = "Config File Created";
}
else
{
//Read the XML File
try
{
fs = new FileStream(filename, FileMode.Open);
xtr = new XmlTextReader(fs);
ds.ReadXml(xtr);
xtr.Close();
DataTable dt = ds.Tables[0];
DataRow row = dt.NewRow();
//row["Service"] = service_name;
row["URL"] = url;
row["Delay"] = delay;
row["Frequency"] = freq;
row["Username"] = username;
row["Password"] = password;
dt.Rows.Add(row);
ds.Tables.Remove(ds.Tables[0].TableName);
ds.Tables.Add(dt);
msg = "Config File Updated";
}
catch(Exception ex) { Console.Error.WriteLine(ex.ToString() ) ; }
fs = null;
}
try
{
fs= new FileStream(filename, FileMode.Append,FileAccess.Write);
xtw = new XmlTextWriter(fs, System.Text.Encoding.Unicode);
ds.WriteXml(xtw);
xtw.Close();
Console.Write(msg);
}
catch(Exception ex) { Console.Error.WriteLine(ex.ToString() ); }
}
Logic here is that if file doesn't exists - it will create the xml schema and the xml file
if it does EXISTS - then it is read into an dataset - new values are appeneded to contents of this dataset it is
written back as a xml file.
- Major problem is that it creates a mal-formed XML (multiple root elements)
-- Any Suggestions
THanx in advance
|
|
|
|
|
Looks like your getting multiple rootelements due to appending of file insteaded of edititing existing xmlstructure.
I've found XmlDocument to be a nice way of modifying xmlstructures.
<br />
public void create_Config_Table(string service_name, string url, string delay, string freq, string username, string password)<br />
{<br />
XmlDocument doc = new XmlDocument();<br />
<br />
if(!File.Exists(_filename))<br />
doc.LoadXml("<'rootname'><\'rootname'>");<br />
else<br />
{<br />
try<br />
{<br />
doc.Load(_filename);<br />
}<br />
catch(Exception ex)<br />
{<br />
Console.Error.WriteLine(ex.ToString());<br />
doc.LoadXml("<'rootname'><\'rootname'>");<br />
}<br />
}<br />
<br />
XmlNode rootNode = doc.DocumentElement;<br />
<br />
rootNode.AppendChild(createTestNode(doc, url, delay, freq, username, password));<br />
<br />
doc.Save(_filename);<br />
}<br />
<br />
<br />
XmlNode createTestNode(XmlDocument doc, string url, string delay, string freq, string username, string password)<br />
{<br />
XmlNode test_node = doc.CreateElement("test");<br />
<br />
XmlNode urlNode = doc.CreateElement("url");<br />
urlNode.InnerText = url;<br />
<br />
XmlNode delayNode = doc.CreateElement("delay");<br />
delayNode.InnerText = delay;<br />
<br />
XmlNode freqNode = doc.CreateElement("freq");<br />
freqNode.InnerText = freq;<br />
<br />
XmlNode usernameNode = doc.CreateElement("username");<br />
usernameNode.InnerText = username;<br />
<br />
XmlNode passwordNode = doc.CreateElement("password");<br />
passwordNode.InnerText = password;<br />
<br />
test_node.AppendChild(urlNode);<br />
test_node.AppendChild(delayNode);<br />
test_node.AppendChild(freqNode);<br />
test_node.AppendChild(usernameNode);<br />
test_node.AppendChild(passwordNode);<br />
<br />
return test_node;<br />
}<br />
Hope this might help you out or make the xmlparsing a bit easier
-Larantz-
for those about to code, we salute you
http://www.tellus-software.com
|
|
|
|
|
I'm having to work around with pinvoke but i´m having difficults in finding a good way to see a list of prototypes procedures in a win32 dll file. I have tried to work around with link.exe (same as dumplbi.exe) but they are not good enough.
Does anyone know a good site that describes the main windows dlls functions, such as kernel and user32?
|
|
|
|
|
MSDN[^] contains documentation for all API functions.
The easiest way to find the signature of a specific method is to google for "msdn" and the methods's name, i.e
"msdn CreateWindowEx[^]".
____________________________________
There is no proof for this sentence.
|
|
|
|
|
There is a utility called Depends that will tell you the exported function names in the .DLL, but nothing will tell you the parameters that those functions are expecting. This has to be spelled out in the documentation for the function.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
I'm trying to get a multi dim array into the constructor of an attribute.
A regular array works fine
[MyAttribute(new string[] { "", "" })]
but [RegisterCommandFlag(new string[,] { { "", "" }, { "", "" } })] generates a compile error of:
an attribute argument must be a constant expression, typeof expression or array creation expression
anyone know a way to get this type of data in?
|
|
|
|
|
I am trying to make a program where I read information from a Microsoft Access database. What do I need to do? I want to click on a button and a new window with information opens. The name on hte button and the information in the new window will come from the Access database.
|
|
|
|
|
I am tring to read in a massive file and its contents into an array like structure, so i have:
struct allInfo
{
public byte rating;
public ushort movieId;
public uint id;
}
public static ArrayList createMasterArray()
{
Console.WriteLine("Reading the contents from the file(quickFileLite.csv)");
StreamReader s = File.OpenText("D:\\www\\data\\quickFileLite.csv");
int entryCounter = 0;
allInfo values;
string read;
ArrayList masterArray = new ArrayList();
string[] split;
split = new string[3];
uint id;
ushort movieId;
byte rating;
char[] splitter = {','};
int loc=0;
while ((read = s.ReadLine()) != null)
{
split = read.Split(splitter);
id = uint.Parse(split[0]);
movieId = ushort.Parse(split[1]);
rating = byte.Parse(split[2]);
loc = entryCounter-1;
values.id = id;
values.movieId = movieId;
values.rating = rating;
masterArray.Add(values);
entryCounter++;
split = null;
}
s.Close();
return masterArray;
}
As it is right now at about 50 million (about half the file) entries to the ArrayList, it takes up about 1.077GB. Does anyone see any way that I could optimize the code to have as small a memory footprint as possible?
|
|
|
|
|
Hello
Your stucture is 7 bytes long. That means that 50 million records should take about 350Mb of memory. Still not effecient. Such amount shouldn't be placed in memory all at once. Maybe you got 512Mb or even 1Gb memory and 2-4Gb of virtual memory or so, but many users may not. Even if they have such memory they won't appreciate all this memory going to your program all of a sudden -specially if they know more about memory and what goes behind the scenes-. At least you could notify them that this process will consume large amounts of memory, and they should close all open programs. Otherwise use temorary files to store output as it's parsed.
Anyway, for your issue. As I said you should be using only 350Mb. The rest is wasted because you use ArrayList, and not arrays. ArrayList are complex reference types, and contain more than just the data you give them.
Two suggestions:
1- Make it unsafe code, and use pointers to make up a Linked list.
2- Use array, and dynamically allocate more as you need -not easy and a waste of processing time-. After allocation you'd have to copy the old array into the new one. This will take more time if your GrowthBy factor is small.
They bound to be more suggestions, but I'd go for the linked list.
Regards
|
|
|
|
|
Look at the StructLayoutAttribute, specifically the Pack parameter. Setting that to 1/2 may help. Also rearranging the struct can help with the alignment. I cant recall the exact details, but a look in the EMCA spec will tell you.
Another optimization is to specify an initial size for the ArrayList, given you can estimate the number of lines given the input size.
The ideal option would be to use an array of the struct, if you can be sure you will have enough space (and not waste to much of it).
Further more, you can likely truncate the ranges of the fields, eg surely rating cant have 256 different values. What about id? What are the limits of those numbers? It depends on the input. Finally you pack them into a 32-bit sized structure (aka int/BitVector32).
Please note all/most of these optimizations are to optimize memory usage, and not performance increase!
|
|
|
|
|
There is mainly two things that waste space in your construction:
1. The ArrayList will grow by a certain percent each time it's full, so it will allocate larger and larger arrays. After a while the arrays will be too large to fit in the regular heap, so they will be allocated as large objects, and the space allocated for large objects never shrink, even if the objects in it are garbage collected.
2. As you are storing structures in an array list, each structure will be boxed inside an object. The metadata that the objects use to keep track of your structure is larger than the structure itself, so you will end up storing more metadata than actual data.
To get around those two problems, you can store the structures in small typed arrays (arrays of allInfo), and store the arrays in an ArrayList. That would make the ArrayList significantly smaller, and the structures doesn't need to be boxed.
Make sure that the arrays are smaller than 85000 bytes, so that they don't end up as large objects. 1000 items is a good round figure which is large enough to keep the overhead low and still way below the large objects threshold.
---
b { font-weight: normal; }
|
|
|
|
|
I am also having trouble with assigning the array to the array list:
while (((read = s.ReadLine()) != null)&&(entryCounter <= 100480507))//100480507
{
//Console.WriteLine(entryCounter+"\n");
split = read.Split(splitter);
id = uint.Parse(split[0]);
movieId = ushort.Parse(split[1]);
rating = byte.Parse(split[2]);
values.id = id;
values.movieId =movieId;
values.rating =rating;
loc = entryCounter-1;
masterArray.Insert(entryCounter,values);
split=null;
entryCounter++;
}
after thats done all:
Console.WriteLine("ID:"+((allInfo[])masterArray[20])[0].id+"\n");
Console.WriteLine("ID:"+((allInfo[])masterArray[21])[0].id+"\n");
Console.WriteLine("ID:"+((allInfo[])masterArray[22])[0].id+"\n");
Console.WriteLine("ID:"+((allInfo[])masterArray[23])[0].id+"\n");
all produce the same id. I checked and they should be different, so do the arrays in the array list point to the array?
|
|
|
|
|
You haven't shown enough of your code for me to say anything about what might be wrong. Are you creating separate arrays at all?
Here is a short example of creating and accessing an ArrayList containing arrays of integers:
ArrayList list = new ArrayList();
int[] array = new int[2];
list.Add(array);
array[0] = 4;
array[1] = 7;
array = new int[2];
list.Add(array);
array[0] = 42;
array[1] = -1;
int v0 = ((int[])list[0])[0];
int v1 = ((int[])list[0])[1];
int v2 = ((int[])list[1])[0];
int v3 = ((int[])list[1])[1];
---
b { font-weight: normal; }
|
|
|
|
|