Initializing Object Variables - Difference Between Methods
Jan 20, 2009
In VB.Net is there any difference between the following three ways of initializing object variables
Method 1
Dim commandObject As SqlCommand
commandObject = New SqlCommand("MadeUpCommand")
Method 2
Dim commandObject As SqlCommand = New SqlCommand("MadeUpCommand")
Method 3
Dim commandObject As New SqlCommand("MadeUpCommand")
Is one more efficient than the others, or are they effectively all the same?
View 3 Replies
ADVERTISEMENT
Jul 10, 2010
I am somewhat new to object oriented programming and am attempting to flatten a Linq object hierarchy by using a shim class.how to initalize a derived class with property values from a base class?I have two objects, a base object with about 100 properties, and a derived object which inherits from the base object and adds a few additional properties beyond those of the base object. My simple constructor creates the derived object, but I am looking for a way to initialize the derived object properties with values from the base object.Right now I am using reflection to iterate over the properties individually and I suspect there may be a better way. The following example shows my shim class constructor for the derived class, and two properties:
newProperty1 - a new string property of the derived class
flattenedProperty2 - a new string property of the derived class, copied from a 2nd-level object of the base class
Code example:
Public Class derivedObj
Inherits baseObj
Private _newProperty1 As String[code].......
Is this the correct constructor approach to flatten the object hierarchy using a shim class? My second question relates to initialization of properties in the derived class. The constructor above creates the derived object, but what is the best way to initialize the derived object properties with values from the base object? The following code uses reflection to iterate over the properties individually, but I suspect there may be a better way.
Code example:
' property names are in the string array fieldNames
'baseObjQuery is an ienumerable of baseObj
'derivedObjList is a list of derivedObj[code].....
Is there a simple way to initialize values for the properties in the derived object based upon the values of the common properties in the base object?
View 7 Replies
Jan 12, 2011
I am trying to write an VBA application in Excel 2010 using "Microsoft Soap Toolkit 3.0" on 64 bit Windows 7 to consume an web service. But i got an error while initializing an object of SoapClient30.
Error message is :: Runtime error '429' :ActiveX component can't create object.
View 2 Replies
Jun 24, 2011
I am working with a vb program, but there is something strange on one of my .vb code pagewhen i put the "dot" afther the object name its dont show the methods availables for this objectbut on other vb code pages i can see it. but in this one no.for exmaplethis is a piece of code: Dim sb As New StringBuilder()
View 3 Replies
Apr 1, 2011
Private Delegate Sub SetControlValueCallBack(ByVal value As Integer)
View 3 Replies
Jul 26, 2011
What is the difference between the following two statements (in terms of memory management):
Dim frm as New MyForm()
frm.Show()
VS
MyForm.Show()
I am originally a C# developer, how does the second one make sense or even compile for that matter in VB.NET? (Show() is not a Shared/Static method) What is happening in the second case?
View 1 Replies
Nov 4, 2011
Whats the difference between these two initialization methods for obj? I've seen both of these, but know know if there is an appropriate time to use one vs the other. I found this post which covers C#, but not sure if the same applies to VB.Net.[code]
View 3 Replies
Sep 21, 2009
Under component services, a COM+ component is used by the company, right-clicking it and choosing 'Activation' tab will show the 'constructor string' that is used for DB server connection by all applications. How can I access it the simplest way possible?
[Code]...
View 1 Replies
May 27, 2010
How should class member variables be used in combination with class methods?Let's say I have a class 'C' with a member variable 'someData'.I call C.getData(), which does not return a value but instead puts data in C.someData. The class that instantiated 'C' first calls C.getData and then uses the data by accessing the member variable C.someData.I call C.getData() in the class that instantiated 'C' which is a function that returns data.I myself prefer the second way. But it also depends on the situation and it's a small difference. Is it 'bad' to have class methods that depend on the classes internal state? What are the best conventions?
View 2 Replies
Mar 24, 2010
I'm not sure I used the right lingo in the subject header. But, here's what I want to do. Let's say I have a class with some variables:
Class XYZ
Public InfoNo1 As Integer
Public InfoStr1 As String = " ".PadRight(200) 'Hahaha
Public InfoStr2 As String = " ".PadRight(100)
End Class
I've been looking around for examples of writing methods that act on variables defined within the class. What I want to do is make it so that doing something like this:
[Code]...
I don't need to know how to write the method, I think I can do that. I need to now how to make the method cause a ".Fixed" option to be added to the class string variables so that assigning a string using that method will cause the variable itself to be equal to the string plus the padding.
View 39 Replies
Jun 26, 2010
I want to know what are classes , objects , a namespace , subroutines,functions,methods,properties and differemce betweem byval and byref in your words.
View 18 Replies
Aug 26, 2010
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim someproc As Process
[Code]...
View 3 Replies
Jun 22, 2009
exact diff b/t Static v/s Shared variables in VB.NET?
View 3 Replies
Jul 22, 2009
give me a good resource that explains the difference between a Private, Public, Shared Functions/Sub/Variables? I normally use Public for Subs/Functions inside of Modules I call from other parts of the program. But I'd like to get more of an understanding of how and when to use them. I want as little as impact to a system that is running my programs as possible, so i guess the key here is I'm trying to just get more proficient in my coding.
View 8 Replies
Mar 10, 2010
I've got 3 forms that have exactly the same functions, so I decided to create an interface.
public Interface IExample
public sub Add()
Public sub Edit()
[code]....
At this point, objfrmExample is now instantiated, even though I've not done a "objfrmExpample = new [what-goes-here?] " and I'm curious as to why.I could possibly guess that because you cannot instantiate an interface variable, then vb.net automatically creates an instance. But thats just a guess. The question is , what is meant by declaring a variable of type Interface, and how does it work?
View 2 Replies
Mar 25, 2010
In the code below (which I just wrote here - it closely mirrors what my actual code is...) the problem is with the line:
Thing.ThingObject.Add(MyObject)
It says: "Object reference not set to an instance of an object" Yes - it is "nothing" before I try to use the Add method so I must be doing something basic and wrong.The object I am trying to add is fine - I can tell you it is correct, it's just that I apparently cannot use the .add method on the Thing.ThingObject until it is properly created - which I have apparently not done.
Private Sub DoThing()
Dim MyObject as New Object
MyObject = MakeMyObject
[code]....
View 2 Replies
Nov 9, 2011
I wonder why it's impossible to create and extension method for Object? I use Excel PIA. The property Cells of Worksheet returns Object (if using with indexes). For example, Worksheet.Cells(1, 1) returns Object type. I know it's a Range object, and I want to cast it to Range. It would be convenient to write following:[code]
View 5 Replies
Feb 8, 2010
Consider the following:
Public Module Extensions
<Extension()> _
Public Sub Initialize(ByRef Target as SomeClass, ByVal SomeParam as Something )
[code].....
Why is Case 1 failing to initialize the object as expected?
View 1 Replies
Aug 3, 2009
I want to create a subclass of a class that has some shared methods. These shared methods all call one specific method. But I can't redefine that specific method in the subclass. If I try to make it overridable in the parent class, it says that shared and overridable can't both be used.
[Code]...
View 5 Replies
Jan 9, 2012
I have the code as below:
Dim xdTest As XDocument = XDocument.Load(GetXMLPath())
Dim objResult = From xe In xdTest.Elements("Some Element") _
Where xe.Element("strName").Value = strInput _
Select xe.Element("intValue").Value
objResult.
I cannot access any extension methods for objResult, like .ToList() or .First(). The file imports System.Linq and the project is targeted to .NET 3.5. How do I get the extension methods back?
View 1 Replies
Feb 3, 2012
When you type sender. IntelliSense lists 5 methods and yet if you type sender.Left or sender.top these will work. Why don't these properties show up as available for use via IntelliSense.
View 2 Replies
Apr 8, 2011
If I create a class in VB called Test,I noticed I can instantiate it like:
Dim test As New Test
or
Dim test As New Test()
What is the difference?
View 2 Replies
Jan 28, 2010
I want to know the difference. I have given 2 lines code.
CODE:
Let me know difference between both datatype.
View 4 Replies
Jan 5, 2012
Is it possible to call a protected method via reflection. I am using this:
Me.GetType.InvokeMember(Stages(CurrentStage),
Reflection.BindingFlags.InvokeMethod,
Nothing,
Me,
Nothing)
Which always calls a method with no params or return. The idea is that the user of my object (transactional processing of business logic) inherits the base class adds in a load of private methods to fire. They then add these method names to a list in the order they would like them fired and the code above will take care of firing them.
It works fine with public methods but not with private or protected methods in the same class (Protected because I have some 'standard' pre built methods to add to the base class). Realistically I could make the methods public and be done with it but my inner nerd wont allow me to do so...
I am assuming this is a security feature. Is there a way to get around this or does anyone have any suggestions on how to proceed but keep my tasty, tasty visibility modifiers in tact?
(NOTE: ITS IN VB.NET but a C# Answer is fine if that is what you are comfortable with).
View 3 Replies
May 23, 2010
Dim MyOrders As Order()
Dim MyOrders2() As Order
View 6 Replies
Nov 25, 2010
I'm using vb10. I need to use a timer for forms to turn off and on and I was going to use a timer control and I noticed there is a timer class. What is the difference? Should I use the timer class instead?
View 5 Replies
Jun 2, 2011
if I created a new object like this Dim sqlconn As New SqlClient.SqlConnection(cs)
and Dim sqlconn = New SqlClient.SqlConnection(cs) Whats the difference?
View 1 Replies
Oct 2, 2009
I've been working in .NET for some time now, but occasionally I still get confused by a disparity between the framework and my prior experience in C++.In .NET, all objects are either value types or reference types. Reference types are allocated on the heap while value types are allocated on the stack (in the current CLR implementation, anyway). I get that. However, at least in VB.NET, you can still define a constructor on a value type.
Public Structure Coordinates
Public x As Integer
Public y As Integer
[code]....
View 3 Replies
Jun 29, 2010
When we can access the public methods via object then why is there need of inheritance.
View 2 Replies
Jun 10, 2012
if you are debugging or releasing a projet , vs writes exe or bin files to the bin and obj folder in a release and debug version, are ther differences bewteen these files with the same name in 4 folders ?, eg myproject.exe
View 3 Replies