473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Syntax for property parameters?

Hi,

I'm learning C# after having learned VB.Net, so I am trying to get a grip on
syntax differenced. In VB.Net I can define a parameterized property like so:

Public Property MyProperty(ByVal myValue As String) As String
Get

End Get
Set

End Set
End Property

I've tried to do the same in C#, but it doesn't seem to want me to:

public String MyProperty (String myValue)
{
get{}
set{}
}

What am I doing wrong?

Thanks.

Kel
Nov 15 '05 #1
3 15324
Hi Kel,

This is how you can write properties in c#

class myclass
{
String myprop;
public String MyProperty ()
{
get
{
return myprop;
}
set
{
myprop = value;

}
}
}

In the set block above, value of the variable "value" will be what you
pass to it

myclass m = new myclass();
m.MyProperty = "Kel"; // the value of variable "value" will be Kel,
which will then be assigned to its priv. variable as you can see above

Did this clear your question ?

Kalpesh
Kel Good wrote:
Hi,

I'm learning C# after having learned VB.Net, so I am trying to get a grip on
syntax differenced. In VB.Net I can define a parameterized property like so:

Public Property MyProperty(ByVal myValue As String) As String
Get

End Get
Set

End Set
End Property

I've tried to do the same in C#, but it doesn't seem to want me to:

public String MyProperty (String myValue)
{
get{}
set{}
}

What am I doing wrong?

Thanks.

Kel


Nov 15 '05 #2
Hi Kel,

This is how you can write properties in c#

class myclass
{
String myprop;
public String MyProperty ()
{
get
{
return myprop;
}
set
{
myprop = value;

}
}
}

In the set block above, value of the variable "value" will be what you
pass to it

myclass m = new myclass();
m.MyProperty = "Kel"; // the value of variable "value" will be Kel,
which will then be assigned to its priv. variable as you can see above

Did this clear your question ?

Kalpesh
Kel Good wrote:
Hi,

I'm learning C# after having learned VB.Net, so I am trying to get a grip on
syntax differenced. In VB.Net I can define a parameterized property like so:

Public Property MyProperty(ByVal myValue As String) As String
Get

End Get
Set

End Set
End Property

I've tried to do the same in C#, but it doesn't seem to want me to:

public String MyProperty (String myValue)
{
get{}
set{}
}

What am I doing wrong?

Thanks.

Kel


Nov 15 '05 #3
Note that in C# properties cannot take parameters, so on the example above,
you need to remove the parenthesis:

string myprop;
public string MyProperty
{
get
{
return myprop;
}
set
{
myprop = value;
}
}

if you need to send parameters you can consider using indexers:

class MyClass
{
string myprop;
public string this [int param1, int param2]
{
get
{
return myprop;
}
set
{
myprop = value;
}
}
}

you can use the indexe in this way:
void method()
{
MyClass mc = new MyClass();
mc[1, 2] = "hello";
}

--
Gabriel Esparza-Romero, Visual C# Team
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Nov 15 '05 #4

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

Similar topics

24
by: deko | last post by:
I'm trying to log error messages and sometimes (no telling when or where) the message contains a string with double quotes. Is there a way get the query to insert the string with the double...
23
by: Marcin Grzębski | last post by:
I red MSDN article of C# 2.0 this week... and i found very strange syntax for properties e.g.: public int MyIntValue { get { // ... } protected set { // ... }
3
by: Julie | last post by:
I'm trying to use data bindings through the control properties, and getting nowhere at light-speed. The online help for data bindings is virtually useless as a tutorial on the subject, and I...
1
by: Gabor | last post by:
In my app I'm using a dll, wich includes an installer class. The install sub call the SetMSDEForApp sub wich two parameters: Public Overrides Sub Install(ByVal stateSaver As...
8
by: Joel Reinford | last post by:
I would like to build a class that has properties which can be accessed by string names or index numbers in the form of MyClass.Item("LastName"). The string names or item index values would be...
1
by: =?UTF-8?B?SmVucyBNw7xsbGVy?= | last post by:
(I also posted this to boost-user) The BGL implementation of breadth-first search uses a dedicated color map. I had the following idea: Some algorithms don't need to distinguish black/gray,...
1
by: Mike P | last post by:
I am building a message body to be sent by email, but I can't seem to get the correct syntax for 2 lines where I am defining the <formand trying to pass parameters and where I am creating a...
7
by: bryant | last post by:
Hi all. I am new to ASP and working in Expression Web. The following query displays the information I need in the gridview for a single record. SELECT "OE_HDR"."ORD_NO", "OE_HDR"."CUST_NAM",...
6
Soniad
by: Soniad | last post by:
Hello, I am excecuting a stored procedure in my ASP page , it has one out parameter (@confirm) . after executing the procedure i want to retreive this out parameter and assign it to variable...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.