Solved

How do I Define and Use a datarow in a class in VB.NET

Posted on 2014-02-07
5
354 Views
Last Modified: 2014-02-13
I am trying to create a "Search" form.  The user will select a customer on this form, and prior to the form closing, I would like to select a Customer datarow (strongly typed), so that the previous form that was calling this "Search"form would be able to read this datarow.  I can pass standard variables (string, long, etc) just fine as I have done with "Name" in the code below (lines 4-12), but I am not sure how to do this with a strongly typed datarow.  If it isn't possible with a datarow but a datatable would work, I could work with that as well.

Public Class frmCustomerSearch

    'This works fine
    Private m_Name As String = ""
    Friend Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property

    'Would like to do something like the following
    Private m_selectedCustomer As String = ""
    Friend Property selectedCustomer() As myDataset.CustomerRow
        Get
            Return m_selectedCustomer 
        End Get
        Set(ByVal value As myDataset.CustomerRow
            m_selectedCustomer = value
        End Set
    End Property

end Class

Open in new window

0
Comment
Question by:eeyo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39844252
I don't completely understand your question.  Are you trying to pass the myDataSet.CustomerRow into a method as a parameter?
0
 

Author Comment

by:eeyo
ID: 39844285
A parameter would work.  To keep it clean, I would rather not use a global datatable or datarow.  I would like to call frmCustomerSearch from other winforms, as below:

using frm as New frmCustomerSearch
    'Optional: could possibly assign a value to an element of the the typed datarow selectedCustomer ...
    frm.selectedCustomer.CustomerID = 12345

    frm.ShowDialog()

    'do something with the typed datarow selectedCustomer, such as ...
    Messagebox.show(frm.selectedCustomer.PhoneNumber)
end using

Open in new window

0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 100 total points
ID: 39845340
If you want to pass the parameter to the form, you can do it a few ways:

1) Define a public property, and set it.

2) Add a parameter to the constructor, and pass it.
0
 
LVL 20

Accepted Solution

by:
ElrondCT earned 400 total points
ID: 39847988
The strongly typed datarow definition has to be accessible to both forms, so you either need to make it global or make it a Friend definition in the parent form, then qualify the definition in the sub form. So if frmMain is the parent form, and you define myDataSet in frmMain, the property definition in frmCustomerSearch would need to be something like:
    Private m_selectedCustomer As frmMain.myDataset.CustomerRow
    Friend Property selectedCustomer() As frmMain.myDataset.CustomerRow
        Get
            Return m_selectedCustomer 
        End Get
        Set(ByVal value As frmMain.myDataset.CustomerRow
            m_selectedCustomer = value
        End Set
    End Property

Open in new window

However, note that the search that you do in the sub form needs to refer, not to the frmMain definition, but to the instantiated form. The easiest way to refer to it is to have a variable frmOwner in the sub; in the Load event, have the statement

frmOwner = Me.Owner

and in the creation of the sub form in frmMain, before .ShowDialog(), put

frm.Owner = Me

Then in frmCustomerSearch, you would do searches on frmOwner.myDataSet.
0
 

Author Comment

by:eeyo
ID: 39857415
Looking at it now, I feel a little silly that I didn't just try to use myDataset.CustomerRow as in the answer.  Thanks for the tip on using Owner.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question