473,804 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How make this code more generic?

From a Winform I am calling the sub routine that follows in a module

Public Sub PopulateWithSta tes(ByVal myform As Contracts, ByVal FieldName As
String)
Select Case FieldName
Case "txtST"
myform.txtST.It ems.Clear()
myform.txtST.It ems.Add("CA")
myform.txtST.It ems.Add("WA")
End Select
End Sub

This works but what I would really like to do is not have to write a case
statement with all the states for each field that has a different name. I
did it this way only because I knew it would work but I would really like to
pass the name of the field involved by value in the Sub routine definition
but I don't know how. Something like

Public Sub PopulateWithSta tes(ByVal myform As Contracts, ByVal FieldName As
FieldObject)

MyForm.FieldObj ect.Items.clear ()
myForm.FieldObj ect.Add("CA") etc.....

Could someone advise me on how to do this?


Nov 20 '05
13 1270
"Fergus Cooney" <fi******@tesco .net> wrote in news:#Sjs1$dkDH A.2328
@TK2MSFTNGP10.p hx.gbl:
Hiya Woody,

Public Sub PopulateWithSta tes _
(ByVal myform As Form1, ByVal sListBox As String)
Dim oControl As ListBox
Dim oListBox As ListBox
For Each oControl In myform.Controls
If oControl.Name = sListBox Then
oListBox = DirectCast (oControl, ListBox)
Exit For
End If
Next
If oListBox Is Nothing Then _
Return

oListBox.Items. Clear
oListBox.Items. AddRange (New String() _
{"CA", "WA", "FL"})
End Sub

Regards,
Fergus


Shouldn't the second line be:

Dim oControl As Control '????

The For Each will puke on any control that's not a ListBox

Chris
Nov 20 '05 #11
Hi Chris,

ROFL, uurgh puke all over my For Each loop.

Doh! Yes, of course, it should be Control.

I tested it on a Form containing nothing but listboxes. No puke there -
but only because I was feeding it vanilla icecream rather than 10 pints of
lager and five straight whiskies followed by chicken vindaloo and a double
kebab.

Regards,
Fergus
Nov 20 '05 #12
Thank you. This worked for me.

Public Sub PopulateWithSta tes(ByVal FieldName As System.Object)
FieldName.items .Clear()
FieldName.Items .Add("CA") etc....
This was simple and straight forward and something I understand. Some of
the other solutions were frankly over my head but thank you to those that
responded.

Woody
Nov 20 '05 #13
Hi Woody,

Did my suggestion works for you?
If you have any related question, please feel free to let me know.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
X-Tomcat-ID: 218518442
References: <eM************ **@TK2MSFTNGP12 .phx.gbl> <8p************ ********@speake asy.net>
<#A************ **@TK2MSFTNGP12 .phx.gbl>MIME-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
From: v-******@online.m icrosoft.com (Peter Huang [MSFT])
Organization : Microsoft
Date: Tue, 14 Oct 2003 08:57:29 GMT
Subject: Re: How make this code more generic?
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vb
Message-ID: <kY************ **@cpmsftngxa06 .phx.gbl>
Newsgroups: microsoft.publi c.dotnet.langua ges.vb
Lines: 114
Path: cpmsftngxa06.ph x.gbl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vb:146484
NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122

Hi Woody,

It seems you want to addressing WinForm Controls by Name.
You need to maintain a HashTable for the relationship between the index andthe name.
So that you can get the index from its name, and then by using the
form.controls( index) to access the control.

http://groups.google.com/groups?q=Fi...N&lr=&ie=UTF-8 &oe=UTF-8&selm=u72StMXi BHA.1552%40tkms ftngp02&rnum=1
Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------


Nov 20 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1474
by: Sammy | last post by:
Hello all Is there a good online resource that discusses C++ equivalents for windows code? Not a simple subject of course... but perhaps a good place to start with the simpler parts. Thx :o)
9
1244
by: Justin Shen | last post by:
you can define two class with same name but having different generic parameters in one assembly. As below: class Gen<T> { } class Gen<T1,T2> { }
1
1135
by: shapper | last post by:
Hello, I created a user control (.ascx) with a property as follows: Private _Messages As Generic.List(Of String) Public Property Messages() As Generic.List(Of String) Get Return _Messages End Get Set(ByVal value As Generic.List(Of String))
0
1215
by: shapper | last post by:
Hello, I want to create an ordered list where each list item can have various controls: <ol> <li>textbox, label, ...</li> ... </ol>
7
2458
by: =?Utf-8?B?QWxleGFuZGVy?= | last post by:
Hi! I want to learn C# in the near future. But for now, I would be more than happy if someone could translate this short c# source code into c++. (I searched the web for c++ equivalents but after one hour i am too frustrated, and so i hope you can help me) using System.IO; ... SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass(); string filename; foreach
4
2021
by: =?Utf-8?B?SGF5U2VlZA==?= | last post by:
Is there some way to use Generics in dynamic code using the Type.GetType("MyClassName") as an argument? List<Type.GetType("MyClassName") oList = new List<Type.GetType("MyClassName") > .... or is there a way to declare a generic using strings that hold class
37
2200
by: Hilton | last post by:
Hi, for (int i = 0; i < list.Count; i++) has a hidden performance hit; i.e. list.Count gets evaluated each time, so we write something like: int listCount = list.Count; for (int i = 0; i < listCount; i++)
3
2716
by: Christof Warlich | last post by:
Hi, I just need a specialization for only one member function of a template class with _many_ members. Do I really have to duplicate the source code for all the members, i.e. for those that do not need to be specialized? E.g. in the example below, I'd like to avoid to redefine member B::g(): #include <stdio.h> template<int x, typename T, short yclass B {
10
6472
by: lpinho | last post by:
Hi all, I have a class (named for the example myObject) that can be of several types (int, string, float, etc), instead of using a object to define it's type I used a generic. public class MyObject<T: ChangeObject { ... public MyObject(string name, T value)
12
3542
by: Howard Swope | last post by:
This problem has been bugging me for a while. I have created a collection class and implemented it in a C# library. If I inherit from this class in another C# assembly and it works, but if I inherit from this class in a C++ / CLI assembly it won't compile. This indicates a problem in the CLR. Can anyone shed some light on this. The class follows: public abstract class DictionaryBase<TKey,TValue: IDictionary<TKey,TValue>,
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9593
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10088
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.