|
Hi!
I have to add an object to an entity, but the name is not hard-coded, so I have to use the AddObject(name,obj) method instead of AddToX(obj) .
Having only obj, how to elegantly obtain name?
The following two approaches work, but they are not very elegant.
1.
_context.AddObject(_table.CommandText.Substring(1,_table.CommandText.Length-2), item);
2.
_context.AddObject(((IQueryable) _table).ElementType.Name, item);
EDIT: I came up with another idea:
_context.AddObject(typeof(T).Name, item);
Is there a better solution? If not, which of these is better?
Thanks.
Greetings - Jacek
modified on Saturday, June 5, 2010 10:56 AM
|
|
|
|
|
This make no sense. You have an entity object you should know what type it is, it's your code, right? Unless you have a method such as this, which is a poor design to begin with.
Add(object obj)
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Mark Nischalke wrote: Add(object obj)
No, but I have a method such as this:
Add(T obj)
in a generic class.
It would be cool if an entity object had an Add method. But it does not. Instead of this, the objectContext.AddToX(X entity) method must be used, where X is a name of an entity.
However, if you look into assembly, all these AddToX methods are implemented in the following fashion:
public void AddToX(X x)
{
base.AddObject("X", x);
}
So, calling AddObject(name, object) directly is not as bad as it looks like.
I think I don't exactly understand how all this works. The properties objectContext.X represent tables, doesn't they? Why there is no Add method in them? If they had Add(T item) method then my problem would be nonexistant. Now I have to use sick statements like typeof(T).Name to get it working.
Greetings - Jacek
modified on Saturday, June 5, 2010 6:36 PM
|
|
|
|
|
Jacek Gajek wrote: I think I don't exactly understand how all this works.
Exactly.
IMO trying to make a generic method such as you are trying is fruitless and unnecessary. Don't you think Microsoft would have implemented such method already if it was such a good idea?
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Mark Nischalke wrote: Exactly.
IMO trying to make a generic method such as you are trying is fruitless and unnecessary. Don't you think Microsoft would have implemented such method already if it was such a good idea?
Alright, but the statement does not solve my problem.
My intension was to automate Insert and Delete commands for ListBox/ListView controls. Now I don't have to handle a KeyDown event for each of them. Instead I use
EventManager.RegisterClassHandler(typeof(ListBox), KeyDownEvent, new KeyEventHandler(ListBox_KeyDown));
private static void ListBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
var listBox = sender as ListBox;
if (listBox != null)
{
object item = listBox.SelectedItem;
if (item != null)
{
var list = listBox.ItemsSource as IList;
if (list != null)
list.Remove(item);
}
}
}
}
As ItemsSource I set
class ObservableTable<T> : ObservableCollection<T> where T : class
{
private readonly ObjectQuery<T> _table;
private readonly ObjectContext _context;
public ObservableTable(ObjectQuery<T> table, ObjectContext context) : base(table)
{
_table = table;
_context = context;
}
public ObjectQuery<T> Table
{
get { return _table; }
}
protected override void InsertItem(int index, T item)
{
base.InsertItem(index, item);
_context.AddObject(typeof(T).Name, item);
}
protected override void RemoveItem(int index)
{
T item = this[index];
base.RemoveItem(index);
_context.DeleteObject(item);
}
}
Now, if a user presses delete key in any ListBox then the current item it is marked for deletion without writing a single line of code.
I don't know how to achieve this level of automation in other (better?) way.
Greetings - Jacek
|
|
|
|
|
Nice idea but I prefer more a loosely coupled architecture. The same thing can be achieved without tying the listbox directly to the datacontext.
IMO this is not a good design. What if the user changed their mind and did not want to insert the item? You are forcing them to delete the object rather cancel the action.
This solution also cannot be used with disconnected objects since you are relying on the context to track the objects for addition and deletion. At point do you commit the changes? You are also not taking into account possible conflicts, such as primary key violations.
Not helping to solve the problem you are having? Correct, because IMO the design is flawed and thus there is no problem to solve.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Should I leave that idea and handle KeyDown event separately for each control which I want to support item deletion? Will it be a good design?
I am very new to WPF.
Greetings - Jacek
|
|
|
|
|
how to handle Server Not found exception at client side in .net remoting.
when server is not running.
currently i am handling socket exception, but each remoting type
error gives socket exception, Is there any specific exception
generated by .net when server is not running?
|
|
|
|
|
Intellisense will tell you what exceptions could be generated by a given method. If there is no apparent exception for "server not found", then just handle a standard Exception to catch everything not explicitly defined:
try
{
}
catch (InvalidOperationException e1)
{
}
catch (ThisOtherException e2)
{
}
catch (Exception e3)
{
}
.45 ACP - because shooting twice is just silly ----- "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." - J. Jystad, 2001
|
|
|
|
|
Hi,
I am bit confusing about output of fallowing variable 'a' which produce '-1' as result.
sbyte a =unchecked((sbyte)255);
I have not cleared yet why this store -1
plz clarify me.
no knowledge in .net
|
|
|
|
|
sbyte (signed byte) has a range from -128 to 127.
Because you used unchecked no overflow is detected. So, 255 is signed -1 .
Cheers
I don't like my signature at all
|
|
|
|
|
The sign is represented by the MSb (most significant bit).
Binary Hex byte sbyte
00000000 0x00 0 0
...
01111111 0x7F 127 127
10000000 0x80 128 -128
...
11111111 0xFF 255 - 1
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
modified on Saturday, June 5, 2010 9:09 AM
|
|
|
|
|
Because a sbyte is a "short byte", meaning it's max value is half of what a normal byte would hold. In your case, your value is causing an interger overflow condition, and the value becomes -1.
.45 ACP - because shooting twice is just silly ----- "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." - J. Jystad, 2001
|
|
|
|
|
I thought the 's' stood for signed rather than short as it's still 8 bits so no shorter.
Either way the answer to the OP is the same and correct
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
You're right, however if we are going to be picky, maybe you'd better fix the mistake in
10000000 0x80 128 -127
too?
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
|
Long ago Data General used to build 36-bit machines, where an integer by default was 36 bits; they also had instructions that treated registers holding five 7-bit "bytes" plus a single flag. They used that for representing text, based on ASCII characters, which requires 7 bits. That could rightfully be called short bytes.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
How to handle bit lengths for types values that don't require all of them or require more is still an issue in some situations.
In my MIDI exploits I'm working with 16bit PICs at the moment. The choice between the complexity and therefore instruction cycle overhead of storing 2 bytes in one word (made more complex as packets can be variable lengths so won't always be word aligned) and the waste of scarce resources by ignoring 8 bits for every byte I need to buffer is one I'm still debating.
MS[^] use one 32bit int (all packets, except SysEx which is easy to identify, are 1 to 3 bytes) and 'waste' between 8 and 24 bits each time. Fine on a PC but not efficient useage of RAM on a µC with only 2KB RAM when requiring 4 individual buffers.
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
I tend to first take care of whatever resource is most scarce. On miniature systems that would be memory most of the time. So decide on a clever data scheme, then come up with efficient accessor functions, even if that takes some ugly code and hacks. Maybe create a stream-like object?
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
the 100...00 bit pattern happens to be the only one you can't represent the absolute value of; and lots of run-time libraries don't treat it well, e.g. an sprintf-like function turning a signed integer to its decimal string representation wants to do:
if (value) {
emit('-');
value = -value;
}
which isn't correct, as value may still be negative yielding unexpected quotient/remainder values later on.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
I saw that too. That led me to revisit the two's-complement calculation and I found this[^].
"the universe is twos-complement"
Cheers
I don't like my signature at all
|
|
|
|
|
Hello:
I have a MDIParent Form called ICEPack and many MDIChild Forms. For now I am working on the Forms called FormGeneral and FormProduct. When I get these working, then all the rest will be simple. I have a Class called ClassControl where I put all my Controls.
For example... The TextBox events: In ClassControl. This way all my Forms use the same code.
while (TheControlEvent != null)
{
TheEvent = TheControlEvent.GetType().Name;
switch (TheEvent)
{
#region TextBox
case "TextBox":
TheControlEvent.Enter += new EventHandler(TextBoxEnter);
TheControlEvent.Leave += new EventHandler(TextBoxLeave);
TheControlEvent.MouseHover += new EventHandler(TextBoxMouseHover);
TheControlEvent.MouseLeave += new EventHandler(TextBoxMouseLeave);
break;
#endregion
... other code
Before the 'while' I add the following:
TheForm.Enter += new EventHandler(FormEnter);
So there will be only one routine for all Form Enter.
Currently in the MDIChild I have:
public void FormGeneral_Enter(object sender, EventArgs e)
{
FormICEPack.RunAs = RunAs;
FormICEPack.dataGridViewRecord.DataSource = DataTableRecord;
FormICEPack.dataGridViewRecord.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
}
And would like to put this in the ClassControl:
private void FormEnter(object sender, EventArgs e)
{
// Original code from FormGeneral & FormProduct
//FormICEPack.RunAs = RunAs;
//FormICEPack.dataGridViewRecord.DataSource = DataTableRecord;
//FormICEPack.dataGridViewRecord.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
// New code???
//...
Form TheParentForm = Form.ActiveForm; // Get The Parent Form
TheChildForm = TheParentForm.ActiveMdiChild.Name.ToString(); // Get The Child Form
Assembly oAssembly = Assembly.GetEntryAssembly();
Form oForm = (Form)oAssembly.CreateInstance("ICEPack." + TheChildForm + "()");
String RunAs = (String)oForm.Controls.Find("RunAs", true)[0].ToString(); //<<< ERROR
}
|
|
|
|
|
You can either make the control public (not good practice), or create a public property that returns the value of a control in the form.
.45 ACP - because shooting twice is just silly ----- "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." - J. Jystad, 2001
|
|
|
|
|
Hello:
If you look at my code... the part that does not work is:
String RunAs = (String)oForm.Controls.Find("RunAs", true)[0].ToString(); // ERROR
Ok, seems you are saying that I should NOT do it this way.
At the point at:
Form TheParentForm = Form.ActiveForm; // Get The Parent Form
TheChildForm = TheParentForm.ActiveMdiChild.Name.ToString(); // Get The Child Form
"TheChildForm" is the good value of the name of the Form. I need to get the value of the string RunAs" in the Form "TheChildForm".
Can you show me an example of code to do it?
|
|
|
|
|
Hello every one.
First of all I am sorry which put this question here, cause it is not relevant to this part. frankly, I couldn't find any proper forum.
I wana write and database application which run on Iphone. I don't know who to start Iphone programming. does any one any article or any experience and help me in this matter.
What do I need to start an Iphone application programming. which environmnet I can write the codes.
Please help me!!!!!
|
|
|
|