Solved

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

Posted on 2014-02-07
5
351 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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql server, import complete table, using vb.net 9 51
VB.NET 2008 - SQL Timeout 9 49
Passing data between Forms 3 26
Access Schema 6 45
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 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