|
Hi, Sascha Lefévre.
I have searched the information about Raw Input in your link and MSDN.It's a good suggestion.I will take some days to get it.
Thanks for your help.
|
|
|
|
|
I have a bindingsource bound to an entity.
When the bindingsource has at least one item , I can get the item's data type with :
MyBindingSource.Current.GetType
But how can I do in the case when bindingsource has no items ?
I try this :
MyBindingSource.GetItemProperties(Nothing)(0).ComponentType
But doesn't work.
Thank you !
modified 17-Apr-15 12:15pm.
|
|
|
|
|
Hello !
I'm using entity Framework (Database First ) with a SQL Server Database.
I have a situation where I have an object ( that is an entity object ) , but the type is known only on runtime ).
I need to get the Primary key property name and the Foreign key property name ( for a specific relation)
for example
MyObj (ObId , name , val1)------- ( ObID is primary key )
MyChild (ChlID ,dt , MyObj_ID) --- ( CHLID is primary Key , Myobj_ID is foreign key)
Dim obj1
.....
Get_PK(obj1)
I want that the possible function Get_PK to return a string with the value :
- "ObID" ( if the type of the object is MyOBJ) , or "ChlID" if the object is of the type MyChild.
and the function Get_Parent(obj1,chl1) to return "MyOBj_ID"
But of course it must be a general function that works with every class inside the entity.
Actually I have 2 functions :
Public Function Get_pk(ctx As MyEntities, entity As Object) As String
Dim objectContext = DirectCast(ctx, System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext
Dim t As Type = entity.GetType.BaseType
Dim m As MethodInfo = objectContext.GetType().GetMethod("CreateObjectSet", New Type() {})
Dim generic As MethodInfo = m.MakeGenericMethod(t)
Dim st As Object = generic.Invoke(objectContext, Nothing)
Dim entitySetPI As PropertyInfo = st.GetType().GetProperty("EntitySet")
Dim entitySet As Metadata.Edm.EntitySet = DirectCast(entitySetPI.GetValue(st, Nothing), Metadata.Edm.EntitySet)
Dim keyNames As IEnumerable(Of String) = entitySet.ElementType.KeyMembers.Select(Function(k) k.Name)
Return keyNames(0)
End Function
This is for Primary Key property , and is working , but is is slow ( I'm thinking if Entity framework has a built in method to get this directly ??? )
Public Function Get_FK(ctx As MyEntities, entity As Object, parenttable As String) As String
Dim fk = entity.MetadataWorkspace.GetItems(Of Metadata.Edm.AssociationType)(Metadata.Edm.DataSpace.CSpace).Where(Function(a) a.IsForeignKey)
Dim fkname = fk.Where(Function(x) x.ReferentialConstraints(0).ToRole.Name = parenttable)
Dim refcol = fkname.Select(Function(x) x.ReferentialConstraints(0).FromProperties(0).Name).First()
return refcol
End Function
This is for the foreign key (related with a parenttable that is passed as parameter ). This is not working. I get an error in the first line because entity is a proxy object and MetaDataWorkspace is not a member of proxy types. and I don't know if this function works at all.
What can I do ?
Thank you !
modified 17-Apr-15 12:15pm.
|
|
|
|
|
For the Primary key :
Public Function Get_PK(ctx As MyEntities, entity As Object) As String
Dim objectContext = DirectCast(ctx, System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext
Dim t As Type = entity.GetType.BaseType
Dim m As MethodInfo = objectContext.GetType().GetMethod("CreateObjectSet", New Type() {})
Dim generic As MethodInfo = m.MakeGenericMethod(t)
Dim st As Object = generic.Invoke(objectContext, Nothing)
Dim entitySetPI As PropertyInfo = st.GetType().GetProperty("EntitySet")
Dim entitySet As Metadata.Edm.EntitySet = DirectCast(entitySetPI.GetValue(st, Nothing), Metadata.Edm.EntitySet)
Dim keyNames As IEnumerable(Of String) = entitySet.ElementType.KeyMembers.[Select](Function(k) k.Name)
Return keyNames(0)
End Function
For the Foreign key :
Public Function Get_FK(ctx As MyEntities, entity As Object, parenttable As String) As String
Dim objectCont = DirectCast(ctx, System.Data.Entity.Infrastructure.IObjectContextAdapter).ObjectContext
Dim t As Type = entity.GetType.BaseType
Dim m As MethodInfo = objcont.GetType().GetMethod("CreateObjectSet", New Type() {})
Dim generic As MethodInfo = m.MakeGenericMethod(t)
Dim st As Object = generic.Invoke(objcont, Nothing)
Dim entitySetPI As PropertyInfo = st.GetType().GetProperty("EntitySet")
Dim entitySet As Metadata.Edm.EntitySet = DirectCast(entitySetPI.GetValue(st, Nothing), Metadata.Edm.EntitySet)
Dim elementtype = entitySet.ElementType
Dim entitymember = elementtype.NavigationProperties.Where(Function(t1) t1.ToEndMember.Name = parenttable)
Return entitymember.First.GetDependentProperties.First.Name
End Function
Please give me some reputation , because I found the answer
|
|
|
|
|
In an existing vb.net 2010 application, I want to sum the values contained in column # 5 in a collection called _dt.
The following is the code for how the specific column is accesed when it is to be displayed on the internet :
Dim _dt As DataTable = New DataTable
For i = 0 To _dt.Rows.Count() - 1
_htmlTable.Append("Total # parts: " + _dt.Rows(i)(_dt.Columns(5).ColumnName()).ToString() + " ")
_htmlTable.Append("<BR />")
Next
I basically want a total value for this specific column. Can you show me the code of how I can sum the value that is contained in column #5 of the code that I listed above?
|
|
|
|
|
1) Declare a variable outside your for-loop that has the same type as that column.
2) Inside the for-loop add the value that you already display as string to that variable.
3) After the for-loop append the value of that sum-variable to your StringBuilder.
Unrelated:
Exactly like you access the column in the column-collection of the table by its index:
_dt.Columns(5)
..you can access the column of a row by its index. So your detour via the column-name is completely unneccessary. Instead of:
_dt.Rows(i)(_dt.Columns(5).ColumnName()).ToString()
..you can write:
_dt.Rows(i)(5).ToString()
And: You already use the StringBuilder - then you could use it also to replace the string-concatenation here:
_htmlTable.Append("Total # parts: " + _dt.Rows(i)(5).ToString() + " ")
..like so:
_htmlTable.Append("Total # parts: ").Append(_dt.Rows(i)(5).ToString()).Append(" ")
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
|
You're welcome!
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Hello !
It's possible to create a list of unknown type at compile time ?
Let's suppose the type is in one variable as a string.
And I want to create a list of this type , and of course after I want to add items from this type to the list.
I have found this code on C# , but it seems not work on vb.net :
Type customList = typeof(List<>).MakeGenericType(tempType);
IList objectList = (IList)Activator.CreateInstance(customList);
object o = objectThatContainsListToCopyFrom;
PropertyInfo p = o.GetType().GetProperty("PropertyName");
IEnumerable copyFrom = p.GetValue(o, null);
foreach(object item in copyFrom) objectList.Add(item);
foreach(object item in objectList) { Debug.WriteLine(item.ToString()); }
I just want to create the list , and after to add items like this :
List1.Items.Add(object).
What can I do ?
Thank you !
|
|
|
|
|
That looks like it should work in C#. What problem do you have with the VB-translation?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I don't know c# , so I have used a code translator.
this is the code :
Dim customList As Type = GetType(List(Of )).MakeGenericType(tempType)
Dim objectList As IList = CType(Activator.CreateInstance(customList), IList)
' Copy items from a PropertyInfo list to the object just created
Dim o As Object = objectThatContainsListToCopyFrom
Dim p As PropertyInfo = o.[GetType]().GetProperty("PropertyName")
Dim copyFrom As IEnumerable = p.GetValue(o, Nothing)
For Each item As Object In copyFrom
objectList.Add(item)
Next
' Will throw exceptions if the types don't match.
' Iterate and access the items of "objectList"
' (objectList declared above as non-generic IEnumerable)
For Each item As Object In objectList
Debug.WriteLine(item.ToString())
Next
It shows several errors , but the first is on second line :
Error 103 Too few type arguments to 'System.Collections.Generic.IList(Of T)'
|
|
|
|
|
This works for me:
Option Explicit On
Option Strict On
Imports System.Reflection
Module Module1
Public Class ChildClass
End Class
Public Class SomeClass
Public Property Children As ICollection(Of ChildClass) = New HashSet(Of ChildClass)
End Class
Sub Main()
Dim tempType = GetType(ChildClass)
Dim someClass = New SomeClass
someClass.Children.Add(New ChildClass)
Dim customList As Type = GetType(List(Of )).MakeGenericType(tempType)
Dim objectList As IList = CType(Activator.CreateInstance(customList), IList)
Dim o As Object = someClass
Dim p As PropertyInfo = o.[GetType]().GetProperty("Children")
Dim copyFrom As IEnumerable = CType(p.GetValue(o, Nothing), IEnumerable)
For Each item As Object In copyFrom
objectList.Add(item)
Next
For Each item As Object In objectList
Debug.WriteLine(item.ToString())
Next
End Sub
End Module
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
sorry , but the Ilist type is unknown .
Everytime I get an error that :
Error 103 Too few type arguments to 'System.Collections.Generic.IList(Of T)'
I don't know why ?
|
|
|
|
|
Do you get this error also when running my code on its own in a new console-project?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Yes , Ilist and Ienumerable are unknown and produce a similar error message that I have posted before.
|
|
|
|
|
I wouldn't know why it should be neccessary for you, as it works for me without, but try it with these imports:
Imports System.Collections
Imports System.Collections.Generic
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I add :
Imports System.Collections
and now the errors are resolved.
But I have some problems in runtime
- I want first to initialize an empty list.
Because if I have just the :
dim itemlist as Ilist
( Before calling these 2 lines
Dim customList As Type = GetType(List(Of )).MakeGenericType(GetType(MyClass))
itemlist = CType(Activator.CreateInstance(customList), IList)
How can I check if the list is empty or ?? something similar. But I want a general checkin mode , because the same expression must be correct even when I clear the list and I want to check if empty ( before initializing for a new type ).
( I try to use
If itemlist.Count > 0 Then
but doesn't work.
|
|
|
|
|
I have a hard time understanding that - can you explain it a bit more detailed?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
ok.
I need a way to detect that this list is empty.
But the expression that should do this , must include 2 cases :
- The list is not yet created . ( So it's just declared as mylist as Ilist).
- The list is created ( with your code ) but the items are deleted ( or has no items ).
I try with the code :
If mylist.Count..... but this doesn't work.
|
|
|
|
|
If mylist IsNot Nothing AndAlso mylist.Count > 0 Then
End If
?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I need to write some unified language, for example Russian chars along with some extended ascii chars like 'μ' in the same string and send to COM port.
Private WithEvents ObjPriComm As New System.IO.Ports.SerialPort
Dim StrLocSendDataTemp As String = "тестовую строку μg"
If i use "UTF-8"
ObjPriComm.Encoding = System.Text.Encoding.UTF8
ObjPriComm.Write(StrLocSendData)
Output is "тестовую строку Ãμg"
If i use "iso-8859-1"
ObjPriComm.Encoding = System.Text.Encoding.GetEncoding("iso-8859-1")
ObjPriComm.Write(StrLocSendData)
Output is "???????? ?????? μg"
What should be used to get proper output as "тестовую строку μg"??
can anyone help me out to come out of this issue...
modified 15-Apr-15 8:39am.
|
|
|
|
|
This has nothing to do with using the serial port. Characters will only be displayed correctly if you use the right font in the code that displays them to screen or paper.
|
|
|
|
|
I feel font is not a cause...
Even i can able to see what i am getting in serial port monitor or hyper terminal....
To write russian or thai chars i need to encode to utf-8 but utf 8 is not supporting extendted ascii chars.
Actually i want to know the actual encoder to write both chars
Thanks
|
|
|
|
|
A UTF8 character is just a sequence of bytes, so sending them through a serial port does not change them. As I said before it is only when you try to display them that issues may arise, if you use the wrong character set or font.
|
|
|
|
|
Hello !
I'm using entity framework.
I have a situation when I use the function CallByName like this :
For k=0 to I
CallByName(MyObject, "Child1", CallType.Get).ElementAt(k)
......
Next
But I get an error
Public member 'ElementAt' on type 'HashSet(Of Child1) not found. '
But when I use the standart expression
MyObject.Child1.ElementAt(k)
Everything is ok.
Why CallByName return a HashSet(Of Child1) ? And what can I do in my expression in order to get the expected result ?
Thank you !
|
|
|
|