|
Hi,
I'm quite new to c# and OOP in general.
I have a situation where I am parsing an xml file and creating objects based on what I find there. What I want to do is name any object or struct I create using the name attribute from my element. I have assigned the current name attribute to a string variable. How do I know use that to name my object to the value of that string?
Thanks for any help.
Neil
tsd
|
|
|
|
|
You can't do that, and even if you could, how would you use a variable that you don't know exists?
Use a collection to store your objects, like:
Dictionary<string, MyClass> items = new Dictionary<string, MyClass>();
items.Add(name, new MyClass(something));
MyClass item = items[name];
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Here is a simple question for you C# Gods. How do you pass arrays -- specifically, an array of integers of unknown length -- as arguments to methods in C#?
|
|
|
|
|
Xarzu wrote: How do you pass arrays -- specifically, an array of integers of unknown length -- as arguments to methods in C#?
Use the params keyword.
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
Why can't you just pass an int[] ? Personally, I'd use a List<int>
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Just do something like
void MyFunction(int[] myarray)
{ }
and call it like this
int[] arr = {2,4,6};
MyFunction(arr);
You can also use the params keyword which allow you to pass the elements as parameter as well as the former way.
Let's say something like
void MyFunction(params int[] myarray)
{ }
call it either
int[] arr = {2,4,6};
MyFunction(arr);
or
MyFunction(2,4,6);
|
|
|
|
|
An array is just an ordinary object. Just pass the reference:
int[] someArray = new int[] { 1, 2, 3 };
someMethod(someArray);
public void someMethod(int[] arrayArgument) {
Console.WriteLine(arrayArgument.Length.ToString());
}
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Hi Guy
last two replay are cleared your question.
that's correct way.
Thanks
Cheers
RRave
MCTS,MCPD
|
|
|
|
|
I have the following code:
if ( (m_myThread.ThreadState & System.Threading.ThreadState.Running) == 0)
{
DoSomething();
}
else
{
DoSomethingElse();
}
The code above results in the compiler claiming that the code in the else block is unreachable. if I change the if line to this:
if ( m_myThread.ThreadState & System.Threading.ThreadState.Running == 0)
I get the following compiler *error*:
Operator '&' cannot be applied to operands of type 'System.Threading.ThreadState' and 'bool'
What's going on (in both cases)?
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
modified on Friday, February 8, 2008 5:16 AM
|
|
|
|
|
John Simmons / outlaw programmer wrote: What's going on (in both cases)?
Operator Precedence
led mike
|
|
|
|
|
The code you're using is straight out of MSDN, so I don't see why it shouldn't work. What if you break it down and use the debugger to see what the result is ? I mean, what if you & the two values into a variable first, then check it's value ? That way you can have a look at what is going on.
I'd also try swapping it around to this:
if ( (m_myThread.ThreadState & System.Threading.ThreadState.Running) == System.Threading.ThreadState.Running)
and then swap the two method calls, obviously.
John Simmons / outlaw programmer wrote: I get the following compiler *error*:
In the second case it's an order of operations error, the == has precedence, so it works out to
if ( m_myThread.ThreadState & (System.Threading.ThreadState.Running == 0))
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Actually, according to MSDN, Running is 0, so if you want to check for ThreadState.Running, you have to do it like this:
if ((myThread.ThreadState & (System.Threading.ThreadState.Stopped | System.Threading.ThreadState.Unstarted)) != 0)
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
modified on Friday, February 8, 2008 5:16 AM
|
|
|
|
|
OK - the idea of a bit mask with a 0 value is so retarded, it didn't even occur to me
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
That was my thought too when I found the answer.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
The value for System.Threading.ThreadState.Running is 0. 0 & anything is zero. Zero always causing a PITA when doing bitwise comparisons causing a special case. In fact, I do not think you can bitwise a 0. In the set of all real numbers the | on 0 will always return a result. Notice in the documentation on the MSDN, however, that running is a exclusivespecial case that is not bitwiseable with any other case.
The proper code would be
if(myThread.ThreadState == System.Threading.ThreadState.Running){
}
else if(myThread.ThreadState & System.Threading.ThreadState.AnyOtherState == AnyOtherState){
}
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
Hi,
I havn't dealt much with floating numbers / calculation since recently.
Let me explain my issue:
I have the following 3 values declared:
float overallTotalAmps = 0;<br />
int voltage = 230;<br />
int overTotalWatt = 126;
on which I do the following calcluation:
overallTotalAmps = float.Parse((overallTotalWatt / voltage).ToString());
The value of overallTotalAmps always returns 0.00 which should be 0.55 (126 / 230).
Am I using the incorect data type here or perhaps if someone could just point out to me what it is I'm doing wrong here?
Thanks.
R
|
|
|
|
|
You need to cast your int values to float values before you divide them.
|
|
|
|
|
float overallTotalAmps = 0;
int voltage = 230;
int overTotalWatt = 126;
overallTotalAmps = (float)overTotalWatt / (float)voltage;
led mike
|
|
|
|
|
Great stuff!!
Thanks to both of you that replied.
It works perfectly!!!
|
|
|
|
|
Hi,
adding to the other replies:
many problem domains are continuous by nature; if so, you should not use integers at all.
The simplest approach is to make overallTotalAmps , voltage, overTotalWatt, ... all float or double.
That way you don't have to cast, and won't have any rounding errors that may go unnoticed.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
I know you've already got an answer, but I was a bit baffled by the use of ToString and float.parse so I thought I'd give you a rundown of what that actually does
overallTotalWatt / voltage
=> (int)126 / (int)230
=> 0 (since we're dividing ints)
0.ToString() => "0"
float.Parse("0") => 0.00
|
|
|
|
|
Anyone that ToString's a number to parse should be dragged outside and shot.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
Hi Guys. I am having problems with writing data to an Access DB. I keep getting error message "Syntax error in INSERT INTO statement". This is when I try and add a new row to the data table. See below code. The section where it stops is highlighted below.
I'm stumped. Have tried a number of things and nothing works.
public partial class logon : Form
{
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\database.mdb");
OleDbDataAdapter m_daDataAdapter;
DataTable m_dtPassword = new DataTable();
public logon()
{
InitializeComponent();
}
private void btnCreateUser_Click(object sender, EventArgs e)
{
conn.Open();
m_daDataAdapter =
new OleDbDataAdapter("Select * From [Callcentre]", conn);
OleDbCommandBuilder m_cbCommandbuilder =
new OleDbCommandBuilder(m_daDataAdapter);
m_daDataAdapter.Fill(m_dtPassword);
DataRow drNewRow = m_dtPassword.NewRow();
drNewRow["UserName"] = txtboxUserName.Text;
drNewRow["Password"] = cipherText;
m_dtPassword.Rows.Add(drNewRow);
<code>m_daDataAdapter.Update(m_dtPassword);</code>
m_rowPosition = m_dtPassword.Rows.Count - 1;
}
}
|
|
|
|
|
Kwagga wrote: writing data to an Access DB. I keep getting error message "Syntax error in INSERT INTO statement".
This is the C# forum. Try the SQL / ADO / ADO.NET forum
led mike
|
|
|
|
|
led mike wrote: This is the C# forum
Well, I am wrting C#. Thought you might know but clearly not. Thanks anyway.
|
|
|
|