|
carbon_golem wrote: JITer will inline code under 32 bytes.
Great point.
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|
|
carbon_golem wrote: it will be injected into every constructor when compiled to IL so it will tend to bloat up classes with multiple constructors.
Even if the constructors are chained with this(...) ?
|
|
|
|
|
No. If you chain constructors, the compiler is smart enough to put members initialized in the class body into the last chain target. I went back through and verified this. Here is some sample code for you to compile and run ILDasm on. Car has chained constructors, where Truck does not. Truck's constructors all get the initialization code for the CultureInfo member, where Car has the initialization for CultureInfo in the constructor that takes 2 args.
using System;
using System.Globalization;
namespace ConstructorChainKata {
class Program {
static void Main(string[] args) {
}
}
public enum Market {
Production,
Concept,
Military,
Custom,
Armored
}
public class Car {
private String manufacturer;
private Market market;
private CultureInfo targetLocale = new CultureInfo("en-US");
public Car(String manuf) :this(manuf, Market.Production) {
}
public Car(String manuf, Market markt) {
manufacturer = manuf;
market = markt;
}
}
public class Truck {
private String manufacturer;
private Market market;
private CultureInfo targetLocale = new CultureInfo("en-US");
public Truck(String manuf) {
manufacturer = manuf;
market = Market.Production;
}
public Truck(String manuf, Market markt) {
manufacturer = manuf;
market = markt;
}
}
}
Scott P
"Simplicity carried to the extreme becomes elegance."
-Jon Franklin
|
|
|
|
|
Yeah, that's what I thought it would do. In fact, if the compiler did not do this, the program would be wrong - field initializers would run as many times as the length of the chain. If some of the field initializers got populated as a result of a static method call with side effects...
|
|
|
|
|
I do use constructor chaining occasionally, where it's appropriate.
My concern here is duplicating logic in each constructor, when the member initialization takes care of it in one place. Since I'm new to .NET, I was concerned there was some drawback to using member initialization that I wasn't aware of.
Software Zen: delete this;
|
|
|
|
|
I understand the concern, but you can still keep the member initialization in one place in your simplest constructor. For example:
public class Class1
{
private int count;
private string status;
private bool flag;
public Class1()
{
this.count = 1;
this.status = "None";
}
public Class1() : this()
{
flag = false;
}
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
[ Blog][ Articles][ Forum Guidelines] Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
|
|
|
|
|
There's a subtle difference if you call virtual methods from your constructor, which is generally not advisable. When generating IL code for the constructor, the C# compiler puts code for all field initializers before code for the constructor's body This means that overridden will see initialized values for field initialized members, but will see default values for others.
class Base
{
protected int x = 42;
protected int y;
public Base()
{
SomeVirtualMethod();
y = 42;
}
protected virtual void SomeVirtualMethod() {}
}
class Derived : Base
{
protected override void SomeVirtualMethod()
{
Console.WriteLine(x);
Console.WriteLine(y);
}
}
prints 42 and 0.
|
|
|
|
|
Gary, in your case, this will do:
class MyClass
{
private int Member;
} Class level members are always initialized to default values. Strings are empty, numbers are 0, and bools are false.
Cheers,
Vıkram.
I don't suffer from insanity, I enjoy every moment of it.
|
|
|
|
|
I'm afraid my example was a little contrived. The actual code involved is a socket interface to a set of services. The interface includes a number of data structures that have to be initialized prior to startup, and it seemed stupid to duplicate the logic (or at least the method call to the logic) in every constructor.
Software Zen: delete this;
|
|
|
|
|
Ah, apologies, I didn't know that.
But you have got some excellent answers I learnt some bits myself, so thanks for posting the question.
Cheers,
Vıkram.
I don't suffer from insanity, I enjoy every moment of it.
|
|
|
|
|
No need to apologize, Vikram; thanks for answering.
Software Zen: delete this;
|
|
|
|
|
Can anyone tell me what would be the appropriate data type to use in C# when reading and writing to binary fields in a Sybase database? Thank you in advance.
Modified:
I have a byte[] populated with 33 values. I'm using a stored proc that has a binary parameter. I'm assigning the parameter the byte[], but I'm only getting 1-byte to write and I'm not even sure it has anything to do with the value that should be written.
modified on Thursday, January 15, 2009 4:08 PM
|
|
|
|
|
You haven't given much information to go on, so the generic answer would be byte[] .
modified on Thursday, January 15, 2009 1:09 PM
|
|
|
|
|
Please see original post. I modified it. Thanks again.
|
|
|
|
|
Without seeing the code you're using to pass the byte array to the database, it's difficult to say what's going wrong.
|
|
|
|
|
////////////////////////////////////////
// SQL
////////////////////////////////////////
CREATE PROCEDURE InsertBinData(
@binData BINARY
)
BEGIN
INSERT INTO Table1 ( BinData ) VALUES @binData
END
////////////////////////////////////////
// C#
////////////////////////////////////////
byte[] data = new byte[33];
// function call to fill byte array here
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Clear();
command.Parameters.AddWithValue( "@binData", data );
command.CommandText = "InsertBinData";
Command.ExecuteNonQuery();
|
|
|
|
|
mobius111001 wrote: byte[] data = new byte[33];
// function call to fill byte array here
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Clear();
command.Parameters.AddWithValue( "@binData", data );
command.CommandText = "InsertBinData";
Command.ExecuteNonQuery();
Don't use AddWithValue . Use the full technique of creating a Parameter object, setting it's database type and direction properly and then assign the value to it and add it to the collection. Here, you're assuming that the object will correctly guess the correct database type to use, when, in fact, it can get it wrong.
|
|
|
|
|
i still only get the byte[0] to write out.
OdbcConnection cn = new OdbcConnection(); // assuming db connection made correction
OdbcCommand cm = new OdbcCommand();
Random r = new Random( ( int )DateTime.Now.Ticks );
OdbcParameter op;
cm.Connection = cn;
byte[] b = new byte[32];
for ( int i = 0; i < b.Length; i++ )
{
b[i] = ( byte )( r.Next() % 256 );
}
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.Clear();
op = cm.Parameters.Add( "@binVal", OdbcType.Binary );
op.Direction = ParameterDirection.Input;
op.Size = b.Length;
op.Value = b;
cm.CommandText = "sp_SaveBinaryVal";
cn.Open();
try
{
cm.ExecuteNonQuery();
}
finally
{
if ( cn.State == ConnectionState.Open )
{
cn.Close();
}
}
|
|
|
|
|
Hi Everyone
I have a rtf file which is simillar to a csv file but here the delimitting character is "|".I want to read the data in rtf file to a datatable. Please some body help me
Thanks in advance
|
|
|
|
|
Hi srinivasadithya,
You can build your own RTF Converter[^] by using the TextConverter class, split any line using String.Split( '|' ) and write the tokens to a System.Data.DataTable .
Cheers,
Jani
|
|
|
|
|
Dear jani
What is the Assembly referance for rtfConverter
could you please elaborate that
Regards
|
|
|
|
|
Hi srinivasadithya,
You have to reference the Assemblies Itenso.Sys.dll , Itenso.Rtf.Parser.dll and Itenso.Rtf.Interpreter.dll . These assemblies are located in the \bin\Debug\ folder.
The sample project Rtf2Raw200x.sln demonstrates how you can extract the text from a RTF file.
Cheers,
Jani
|
|
|
|
|
can somebody explain me Asynchoronous multithreading.
Thanks in Advance...
vikas da
|
|
|
|
|
|
Thank you very much i think i got, what i was looking for ..
vikas da
|
|
|
|