|
Object[][] is a jagged array. Perhaps you need Object[,] ??
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Wow, thank you! How is that handled differtly in memory? Why do both of those exist?
Cheers,
Jim
Did I post well? Rate it! Did I post badly? Rate that too!
|
|
|
|
|
Jagged arrays is simply an array of arrays, while a multidimensional array is truly one array with multiple dimensions. Jagged arrays can have different dimensions for each row in the first dimension, for eg, whereas multidimensional arrays always have the same number of dimensions.
int [][] x = new int[2][];
x[0] = new int[5];
x[1] = new int[10];
...
int [,]x = new int[2,10];
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Hi all,
I'm having a problem using class casts. I'm storing an ArrayList inside a DataTable, and when I want to retrieve the list, I get an InvalidCastException. Here is my code:
using System;<br />
using System.Data;<br />
using System.Collections;<br />
<br />
class CastTest {<br />
const string COL = "test";<br />
static void Main() {<br />
Console.WriteLine("CastTest");<br />
ArrayList list = new ArrayList();<br />
string str = "Hello world";<br />
list.Add(str); <br />
Console.WriteLine("arrayList created"); <br />
DataTable table = new DataTable();<br />
DataRow row = table.NewRow();<br />
table.Columns.Add(COL);<br />
table.Rows.Add(row);<br />
row[COL] = list; <br />
Console.WriteLine("table created"); <br />
object resultObj = table.Rows[0][COL]; <br />
Console.WriteLine("resultObj: " + resultObj); <br />
ArrayList resultList = (ArrayList)resultObj; <br />
string resultStr = (string)resultList[0];<br />
Console.WriteLine(resultStr);<br />
}<br />
}
The second last WriteLine statement prints: "resultObj: System.Collections.ArrayList". I seem to be retrieving the ArrayList as an object, but I'm unable to cast it. Why?
Thanks in advance.
Cheers,
Michael
|
|
|
|
|
I ran the above code, and as I suspected, the arraylist is having it's 'tostring()' called as it goes into the table. You don't have an arraylist, you have a string which says 'System.Collections.ArrayList'.
I doubt you can shove arrays into a cell of a table.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
With the help of a workmate, I've resolved the issue. The quirk was in the Add method of the DataColumnCollection class (which holds the cells for the rows). The writers of the method, in their infinite wisdom, convert the input value to a string before adding it. This is default behaviour. It can be overridden by creating a custom DataColumn to hold the type of the input data. I'm including the code which now works.
using System;<br />
using System.Data;<br />
using System.Collections;<br />
class CastTest {<br />
const string COL = "test";<br />
static void Main() {<br />
Console.WriteLine("CastTest");<br />
ArrayList list = new ArrayList();<br />
string str = "Hello world";<br />
list.Add(str); <br />
Console.WriteLine("arrayList created"); <br />
DataTable table = new DataTable();<br />
DataColumn dC = new DataColumn(COL, list.GetType());<br />
table.Columns.Add(dC);<br />
DataRow row = table.NewRow();<br />
table.Rows.Add(row);<br />
row[COL] = list;<br />
Console.WriteLine("table created"); <br />
object resultObj = table.Rows[0][COL]; <br />
Console.WriteLine("resultObj type: " + resultObj.GetType()); <br />
ArrayList resultList = (ArrayList) resultObj; <br />
string resultStr = (string)resultList[0];<br />
Console.WriteLine(resultStr);<br />
}<br />
}
I guess I should be reading the documentation more closely so that I would be more aware of "features" like this
Thanks for the replies. Regards,
Michael
|
|
|
|
|
Can somebody tell me how to escape the semicolan character in C#.
|
|
|
|
|
Use the @ character before the opening double-quote of a string, such as:
string myName = @"My name is ; Ashok Dhamija";
label1.Text = myName;
This will correctly show the string alongwith the semi-colon character. Once you use the @ character as above, there is no need to further add any escape characters in the string.
Edited on 25 April:
The @ character will obviate the need for using the escape character while defining a string with any sort of characters used in it (whatsoever, whether otherwise requiring an escape character or not)including semi-colon (though a semi-colon does not require an escape character as pointed out by leppie in the next message)
|
|
|
|
|
|
Connection.close and Connection.dispose, which one is better?
Can someone tell me which one should I use?
Thanks in advance.
|
|
|
|
|
Assume nothing. If you want to close it, close it. If you want to dispose of it, close it and then dispose it. Better yet, the using keyword will dispose of it for you.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Neither. The methods are used for different purposes entirely.
Calling .Close() will close the connection, but leave the object available so it can be reopened.
Calling .Dispose() will close the connection and destory the object, tagging it for GC.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Destroy the object and tagging it for GC? I don't agree, Dispose can only release managed/unmanaged resources, how does it make it eligible for GC? GC only checks for the reachability of objects, AFAIK and calling Dispose doesn't help in getting it GC'ed.
Generally, Close and Dispose are supposed to do the same thing. What connection object are you talking about? In case it is an SqlConnection, the MSDN doc clearly says "If the SqlConnection goes out of scope, it is not closed. Therefore, you must explicitly close the connection by calling Close or Dispose."
BTW, remember about the other thread where we were discussing about connection pooling. The MSDN doc for the Close method says "It then releases the connection to the connection pool, or closes the connection if connection pooling is disabled.". So obviously, if you use connection pooling, pooled database connections remain open.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
My mistake! RectalCranial Inversion...
In connection pooling, you're right, the connection remains open. But it has it's appropriate uses. A single user application is not one of them. In the case of a Web Site, on the other hand, connection pooling is a beautiful thing, because connections must be recycled quickly to maintain performance for thousands of page fetches and database accesses per second. A single user application simply doesn't need that kind of performance, (and I'll add the obligitory "except in special cases" here.)
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Senthil,
Is connection pooling turned on by default?
On the other hand, use the Close method or the "using" would be the right way, according to MSDN doc. Am I right?
I find something else from MSDN "Dispose method calls Close, and returns the SqlConnection to the connection pool"
It is very confusing.
|
|
|
|
|
Yes, connection pooling is turned on by default.
You don't need to worry about it though, you just call Close or Dispose on the connection, either direcly or through the "using" statement. The framework takes care of putting the object back to the pool (if connection pooling is on) or really closing the connection (if connection pooling is off).
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Hey!
I have the folowing string
"\nHEY\r\nThis Test\r\n\nTest running in OS\r\n\nRE: Test 38"
When adding this to a richTextBox it shold look somthing like this
-- START --
HEY
This Test
Test running in OS
Re: Test 38
-- END OF STRING --
But instead it ends up like this
-- START --
HEY
This
Test
Test runnin
g in OS
RE:
Test 38
-- END OF STRING --
How is this posible?
The string is added by using invoke to the richtext box
<br />
rtIncomeLogg.Invoke(new DelegateAddDataToLogg(addData), new object[] { this, data });<br />
data is the string.
<br />
private void addData(object inSender, string inData)<br />
{<br />
try<br />
{<br />
rtIncomeLogg.AppendText(inData);<br />
<br />
rtIncomeLogg.Focus();<br />
rtIncomeLogg.Select(rtIncomeLogg.TextLength, 1);<br />
rtIncomeLogg.Select();<br />
<br />
}<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show("Error in " + this.ToString() + " - addData : " + ex.Message);<br />
}<br />
}<br />
<br />
Any ide?
Best Regards
SnowJim
|
|
|
|
|
It's because both \r and \n are converted in the richtextbox to a linebreak.
You only need to use either \n or \r. If you use both, as in CRLF, it will convert them into two linebreaks.
|
|
|
|
|
Okay, but if you look at my ex so can you see that a line break acures in the midle of a word with out any /r och /n, how cand this be?
The richtext box is big enouth so it have nothing to do with wraping i think.
Best Regards
Jimmy
|
|
|
|
|
Have trayed to place the string in a file instead but its the same problem.
Pleas help!
BestRegards
SnowJim
|
|
|
|
|
it is fixed, thanks anyway.
Apperently i was using writeline when i was writing to my stream, and this writeline is adding \n on the end of the data.
Best Regards
SnowJim
|
|
|
|
|
|
trying to match a pattern for an application that I working on for my self and can't seem to grasp this pattern in the regex fashion
so the pattern I am trying to match
kbfi ksea sea bfi
([a-zA-Z0-9]{4})|([a-zA-Z0-9]{3}) sorta works but it doesn't work if I put in the (\s) to catch the space so it splits incorrectly. Pattern should match something with 3-4 letters like above anything more than 3-4 letters without a space should be ignored so no garbage like this
asdfkjasdf aodf
Also it should be able to match a single set without spaces
kbfi
should be detected as well. As for correctness of the actual data in the string it will be verified against data stored in db tables
Thx
Morgan
|
|
|
|
|
|
I am trying to convert over a Crystal Reports report from using a database to using a dataset. That part seems to have gone along just fine. I followed the instruction Microsoft gives for doing that and also for creating an XSD dataset (which is how I was able to convert the report). I'm sure to most of you, the solution to my problem is simple.
Here's my problem: I don't know how to get data into that dataset. I can't use the standard method of creating an OdbcDAtaAdapter and doing a Fill into the DataSet. I've tried, it doesn't work. I keep getting the following error if I try:
Fill: SelectCommand.Connection property has not been initialized.
Here's the command that generates the error:
daDalyORHED.Fill(dsDalyData1, "orhed");
Here are the relevent parts of the code:
daDalyORHED = new OdbcDataAdapter("SELECT * FROM PUB.orhed orhed WHERE OrdNum = '" + strOrdNum + "'", connDaly);
connDaly = new OdbcConnection("DRIVER=MERANT 3.60 32-BIT Progress SQL92 v9.1D;UID=userid;HOST=siifpa001;PORT=apluslvsv;DB=apluslv;PWD=password");
dsDalyData dsDalyData1 = new dsDalyData();
Obviously, dsDalyData is the name of my XSD dataset.
Can anybody please help me get my DataSet populated? It's kinda useless without that.
--In a world without fences, who needs Gates?
|
|
|
|