|
Hello !
I have this code :
Public Class myclass
....
Private cont As MyentEntities
.....
.....
Public Sub New( ByRef cnx As MyentEntities)
.....
cont = cnx
.....
End Sub
Public Sub test
.....
If cont.mytable1.Count=1 then
.....
end if
End Sub
End class
And on my main form :
.......
Dim E1 as MyEntEntities
Dim m1 as myclass
Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs)
Handles Me.Load
E1=New MyentEntities
m1=new MyClass(E1)
........
End Sub
Private sub fill_entity
E1.MyTable1.Tolist
....
End sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs)
Handles Button1.Click
m1.test
End Sub
When I click the button , I get an error :
....the entity of type 'MyTable1' does not exist in the context.
But this entity type is added to E1.
What's wrong ?
Thank you !
|
|
|
|
|
|
Hello !
I'm using vb.net 2013.
I have a situation when I need to pass as parameter some controls like Buttons. checkboxes and also a Bindingsource and an Entity.
Both Byref and Byval are working in my case.
but I'm asking :
- Which of them is faster and consume less memory ?
Thank you !
|
|
|
|
|
That doesn't matter; if byval works, use it.
|
|
|
|
|
PIEBALDconsult wrote: That doesn't matter Performance does not matter?
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Performance is not a reason to choose between those options.
|
|
|
|
|
Hello !
But I'm telling you that both works for me.
Now I want to choose considering performance.
Thank you !
|
|
|
|
|
Not recommended. Use ByVal regardless, because it's correct.
|
|
|
|
|
Hello !
But I'm speaking about passing as argument a button , a checkbox , a BindingSource and a entity.
What about memory consuming , because Byref is passing only a pointer ? ( Or I'm wrong ?)
Thank you !
|
|
|
|
|
dilkonika wrote: Byref is passing only a pointer
It passes the reference by value.
|
|
|
|
|
Ok , let's get only a case :
Passing a bindingsource as Value or as reference , both are consuming the same memory or ?????
thank you !
|
|
|
|
|
In your example, both are passing a pointer to the BindingSource. In the case of a ByRef, your passing the pointer to the object. By passing a reference type ByVal, you're passing a copy of the pointer. Both will allow you to call methods and make changes to properties in the original object. But, ByRef will allow you to replace the original object with a new one. Passing a reference type ByVal will not.
For value types, if you pass them ByVal you are passing a copy of the value. Changing the variable in the called method does nothing to the original passed in value.
If you pass in a value type ByRef, you're passing in a pointer to the value. Any change to the variable containing the value will change the original value in the calling code.
Where you run into a performance hit, albeit as very small one, is when you pass a large structure by value. Each value type in the structure, when passed ByVal, is copied and passed to the target method. For those types, it's probably better to pass a structure ByRef.
Read this[^].
|
|
|
|
|
PIEBALDconsult wrote: Performance is not a reason to choose between those options. Sure it is. What if you are passing huge objects?
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Doesn't matter. Use the right tool. If ByVal works, use it.
|
|
|
|
|
Ok , but at least , can someone give me a response for this question :
Passing a bindingsource as Value or as reference , both are consuming the same memory or ?????
Thank you !
|
|
|
|
|
If your bindingsource is a class (and it probably is) then you're passing a reference byval or byref, so it works out to the same thing.
|
|
|
|
|
I'm not sure what Pieb's going on about, but byref obviously just passes a reference, not a copy of the object. Therefore, if the object is big, byref will be faster.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Pieb is right in that one should use Byval most of the time and only use Byref sparingly, if at all.
I personally do not feel that performance is that much of an issue between the two.
And yes, Byref is passing a reference pointer back to the original variable. Whereas Byval is just passing the value to the called function/routine.
I recommend to use Byref sparingly because if, in the called subroutine, you change the value of the passed variable, what you are actually doing is changing the value in the original variable that you made your call with. See example:
Public sub TryThis
dim X as String
dim Y as String
dim Z as String
X = "Red"
Y = "Green"
Z = "Yellow"
Z = TestFunction(x,y)
debug.print x
debug.print y
debug.print z
end sub
======
Public Function TestFunction(byval Value1 as string, Byref Value2 as string) as string
Value1 = "White"
Value2 = "Blue"
Return Value1
End Function
Results: X = "Red"
Y = "Blue"
Z = "White"
Notice that the value of Y changed even though it was not referenced or accessible by name in the function. This is because Y was represented in the function as Value2 and was passed Byref. The value of X was passed ByVal and did not change even though Value1 was also changed in the function. One should be careful using Byref for this reason. ByVal should be the preferred option in most all cases.
Any time I code a Byref, I always make it clearly known in a comment at every call what is being passed byref and if any value is being returned using the byref variable.
|
|
|
|
|
dbitsch wrote: Pieb is right in that one should use Byval most of the time and only use Byref sparingly, I rarely use ByRef but never heard anyone say it should almost never be used.
dbitsch wrote: because if, in the called subroutine, you change the value of the passed variable, what you are actually doing is changing the value in the original variable that you made your call with. Yes, that IS the definition of ByRef and the reason you would use it.
I had a case years ago where I was passing an object byval, which is the default, to a function that was called repeatedly thousands of times. It turns out ByRef worked much better.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Not quite.
ByVal will NOT make a copy of a reference type. Both ByVal and ByRef will pass a pointer to the object.
The difference is that passing a reference type ByVal makes a copy of the POINTER, not the object.
What's the difference? Passing a reference type ByRef will allow the called method to replace the original object with a new one. Passing a reference type ByVal will not.
|
|
|
|
|
Yes! Neither is making a copy of the original object. ByRef and ByVal both pass a pointer to the object.
|
|
|
|
|
I have a MS Access Database that has 2 tables (for this example)
Table_1 has a Filed named [ID] which is the Primary Key
Table 2 has a field name [tbl1ID]
I want to Create a relationship between Table_2 and Table_1
I tried:
"ALTER TABLE Table_2 ADD CONSTRAINT [tbl1ID] FOREIGN KEY (MyNewID) REFERENCES [Table_1](ID)"
I get the error "Can Not Find Table Or Constraint"
I know Table_1 and Table_2 exists and I know ID is a filed in Table_1 and I know tbl1ID is in Table_2
So can anyone tell me where I went wrong?
|
|
|
|
|
Not sure, but are tbl1ID and MyNewID in the wrong order?
|
|
|
|
|
Well I flip them per your suggestion and I still Get the same error.
|
|
|
|
|
Well It Looks like I had a typo after-all. I had a space in the sable name where it should have been an Underscore.
Thanks.
|
|
|
|