473,406 Members | 2,707 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,406 software developers and data experts.

Switch case statement error: A constant value is expected

Hi all
I have a TreeView in a MDI Apps. When the Nodes is selected it displays a MDI child form.

I want to use a switch statement to determine which node is selected and to load or show the correct MDI child form.

Here is part of the code:

private void treeViewMDI_AfterSelected (object sender, TreeViewEvenArgs e)
{
frmAB frmAB1 = new frmAB ();
frmUDC frmUDC1 = new frmUDC();

switch(e.Node.Text)
{
case frmAB1.Text: // This did not compile
frmAB1.MDIParent=this;
frmAB1.show();
break;

case frmUDC1.Text: //This did not compile
frmUDC1.MDIParent=this;
frmUDC1.show();
break;
}
}


The line of code that did not compile is stated above.

Please assist or point me to right direction to solve this problem.

Marsh
Nov 8 '07 #1
4 19575
r035198x
13,262 8TB
Hi all
I have a TreeView in a MDI Apps. When the Nodes is selected it displays a MDI child form.

I want to use a switch statement to determine which node is selected and to load or show the correct MDI child form.

Here is part of the code:

private void treeViewMDI_AfterSelected (object sender, TreeViewEvenArgs e)
{
frmAB frmAB1 = new frmAB ();
frmUDC frmUDC1 = new frmUDC();

switch(e.Node.Text)
{
case frmAB1.Text: // This did not compile
frmAB1.MDIParent=this;
frmAB1.show();
break;

case frmUDC1.Text: //This did not compile
frmUDC1.MDIParent=this;
frmUDC1.show();
break;
}
}


The line of code that did not compile is stated above.

Please assist or point me to right direction to solve this problem.

Marsh
Like the error message says, you have to use a constant value for your cases. This is to ensure that the compiler can enforce the rule which says that "no two case constants within the same switch". If you use variables then the there is a possibility of breaking the rule.
Nov 8 '07 #2
balabaster
797 Expert 512MB
Like the error message says, you have to use a constant value for your cases. This is to ensure that the compiler can enforce the rule which says that "no two case constants within the same switch". If you use variables then the there is a possibility of breaking the rule.
Hmm...doesn't look like you have this ability in C#. However, in VB we can do:

Expand|Select|Wrap|Line Numbers
  1. Select Case SomeObjectParameter 
  2.   Case Is = SomeSecondObject.Parameter
  3.     'Do Stuff
  4.   Case Is = SomeThirdObject.Parameter
  5.     'Do Stuff
  6.   Case Else
  7.     'Do something else
  8. End Select
Nov 8 '07 #3
r035198x
13,262 8TB
Hmm...doesn't look like you have this ability in C#. However, in VB we can do:

Expand|Select|Wrap|Line Numbers
  1. Select Case SomeObjectParameter 
  2.   Case Is = SomeSecondObject.Parameter
  3.     'Do Stuff
  4.   Case Is = SomeThirdObject.Parameter
  5.     'Do Stuff
  6.   Case Else
  7.     'Do something else
  8. End Select
VB's case is closer to C#'s if else than to the switch.
Nov 9 '07 #4
Plater
7,872 Expert 4TB
Oh VB nad your butchering of standard coding concepts.
That is not a switch/case statement
Nov 9 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Andy | last post by:
Can someone tell me if the following Switch...Case construct is valid? I'm wanting to check for multiple values in the Case statement without explicitly listing each values. So for example, will...
2
by: Guy Noir | last post by:
Hello again. I'm assuming I'm doing something dumb here.... When I try and add a default: case to my switch statement, I get an error that says "Error 1 Syntax error, '(' expected" and the red...
7
by: sam_cit | last post by:
Hi Everyone, In the following code, i have a common action for three switch cases, is there any other better way to write the three values in a single case? Thanks in advance #include...
2
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
New programmer lost!!! I need some help with a switch/Case statement; I need to use OpenFileDialog to open a file, If the file extension is .dat, then do x. If the file extension is .rtf,...
1
by: Cloudbender | last post by:
how to create a program in c that shows your zodiac sign by using switch case statement. heres an example output: 1 - jan. 2 - feb. 3 - mar. 4 - apr. 5 - may 6 -...
2
by: gooner | last post by:
Hey I'm new to linux, and for an assignment I have to make a switch case statement to output microprocessor workload. When the script is run it should output one of the following results: >83 -...
4
by: DaemonCoder | last post by:
I need some suggestions on how to betterer write the following code. I tried the array of arguments, and san through for a match then strcmp but i get massive errors. The code below is the only way i...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
6
HaLo2FrEeEk
by: HaLo2FrEeEk | last post by:
I'm trying to make a switch statement that will handle hotkeys assigned to different functions in my application. The hotkeys will be user-changable, so I've got them saved as application settings. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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,...

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.